Posted by : Sudhir Chekuri Friday, 25 October 2013

In this example i created a Web api controller named as ConnectController contains a method named as get. It retreives data from SQL Server database table using ADO.NET Code.

Code in WebAPI Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;

namespace ConnectWebAPI.Controllers
{
    public class ConnectController : ApiController
    {
        SqlConnection con = new SqlConnection("Data Source=sudhir-pc;Initial Catalog=Connect;Integrated Security=True");

        // GET api/<controller>
        public DataTable  Get()
        {
            SqlCommand cmd = new SqlCommand("select * from tbl_register",con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
}}

WebAPI Url and Output


Data

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<DataTable xmlns="http://schemas.datacontract.org/2004/07/System.Data">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="emailid" type="xs:string" minOccurs="0"/>
<xs:element name="pwd" type="xs:string" minOccurs="0"/>
<xs:element name="phoneno" type="xs:string" minOccurs="0"/>
<xs:element name="stat" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<id>1</id>
<name>sudhir</name>
<emailid>sudhir@gmail.com</emailid>
<pwd>sudhir</pwd>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<id>2</id>
<name>raj</name>
<emailid>raj@gmail.com</emailid>
<pwd>raju</pwd>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataTable>

Code in windows store app

private async void BtRegister_Click(object sender, RoutedEventArgs e)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri("http://localhost:7239/");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            // Limit the max buffer size for the response so we don't get overwhelmed
            httpClient.MaxResponseContentBufferSize = 256000;

            var response = await httpClient.GetAsync("api/Connect");
            response.EnsureSuccessStatusCode(); // Throw on error code.

            var users = await response.Content.ReadAsStringAsync();
            MessageDialog m = new MessageDialog(users);
            m.ShowAsync();        
        }

Output data in Message dialog



Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -