- Back to Home »
- Windows store apps C#/XAML »
- Displaying data from sql server database in windows store app using webservice ado.net c# code
Posted by :
Sudhir Chekuri
Friday, 18 October 2013
SQL Server Database Table
WebService code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
/// <summary>
/// Summary description for myhotelappwebservice
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class myhotelappwebservice : System.Web.Services.WebService {
public myhotelappwebservice () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public List<object> allhotels()
{
SqlConnection con = new SqlConnection("Data Source=2MPYL1S\\SQLSERVER;Initial Catalog=allhotelsinfodatabase;Integrated Security=True");
string selectquery = "select * from allhotelsdetails";
SqlCommand cmd = new SqlCommand(selectquery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int count = ds.Tables[0].Rows.Count;
//arraylist used to store whole table data , each item = row
List<object> a=new List<object> ();
for(int i=0;i<ds.Tables[0].Rows.Count ;i++ )
{
string s = ds.Tables[0].Rows[i][0].ToString()+","+ds.Tables[0].Rows[i][1].ToString()+","+ds.Tables[0].Rows[i][2].ToString()+","+ds.Tables[0].Rows[i][3].ToString()+","+ds.Tables[0].Rows[i][4].ToString()+","+ds.Tables[0].Rows[i][5].ToString()+","+ds.Tables[0].Rows[i][6].ToString();
a.Add(s);
}
return a;
}
}
Output from web service on browser
<?xml version="1.0" encoding="UTF-8"?>
-<ArrayOfAnyType xmlns="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><anyType xsi:type="xsd:string">kanishka,india,andhra pradesh,kadapa,newrtcbusstand,oppositetobusstop,7842119930</anyType><anyType xsi:type="xsd:string">tajkrishna,india,andhra pradesh,hyderabad,banjarahills,begumpet,405869956</anyType><anyType xsi:type="xsd:string">harsha mess,india,andhra pradesh,hyderabad,sr nagar,main road,8977336653</anyType><anyType xsi:type="xsd:string">greenland,india,andhra pradesh,hyderabad,begumpet,lalbungalow,9618326260</anyType></ArrayOfAnyType>
xaml code in windows store app
<Page
x:Class="All_hotels.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:All_hotels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="#F2F2F2" >
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Text="My Hotels" Foreground="Blue" FontWeight="Bold" Grid.Row="0" FontSize="60" FontFamily="segoui" Margin="437,10,63,10"/>
<TextBlock Text="At Your Place" Foreground="Blue" FontStyle="Italic" FontFamily="segoui" FontSize="25" Margin="675,80,-675,-8" Grid.RowSpan="2" />
</Grid>
<Button x:Name="btn_search" Grid.Row="2" BorderBrush="Black" FontWeight="Bold" Content="Search" Foreground="White" FontFamily="segoui" FontSize="20" Grid.Row="5" Grid.ColumnSpan="2" Margin="250,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Click="btn_search_Click_1"/>
<!--list view for showing hotels addresses-->
<Grid x:Name="listviewofhotels" Grid.Row="1" Background="#1589FF" Visibility="Collapsed" >
<ListView x:Name="listofhotels" Margin="120,5,20,10" Height="auto" Width="auto" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Hotels List At Your Area" FontSize="30" FontWeight="Bold" FontFamily="segoui" Foreground="White" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="200,0,0,0"/>
<TextBlock Text="Hotel Name:" FontSize="20" FontWeight="Bold" Foreground="White" FontFamily="segoui" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_hotelname" Text="{Binding hotelname}" Foreground="White" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>
<TextBlock Text="Country:" FontSize="20" FontWeight="Bold" Foreground="White" FontFamily="segoui" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_country" Text="{Binding country}" Foreground="white" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2" Grid.Column="1"/>
<TextBlock Text="State:" FontWeight="Bold" FontSize="20" Foreground="White" FontFamily="segoui" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_state" Text="{Binding state}" Foreground="white" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="3" Grid.Column="1"/>
<TextBlock Text="District:" FontWeight="Bold" FontSize="20" Foreground="White" FontFamily="segoui" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_district" Text="{Binding district}" Foreground="white" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="4" Grid.Column="1"/>
<TextBlock Text="Area:" FontWeight="Bold" FontSize="20" Foreground="White" FontFamily="segoui" Grid.Row="5" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_Area" Text="{Binding area}" Foreground="white" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="5" Grid.Column="1"/>
<TextBlock Text="street:" FontWeight="Bold" FontSize="20" Foreground="White" FontFamily="segoui" Grid.Row="6" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_street" Text="{Binding street}" Foreground="white" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="6" Grid.Column="1"/>
<TextBlock Text="Contact No:" FontWeight="Bold" FontSize="20" Foreground="White" FontFamily="segoui" Grid.Row="7" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="250,20,0,20"/>
<TextBlock x:Name="txtb_contactno" Text="{Binding contactno}" Foreground="white" FontSize="20" FontFamily="segoui" Height="auto" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="7" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Grid>
<Page.BottomAppBar>
<AppBar x:Name="bottomappbar" Padding="10,10,10,0">
<Grid>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<Button x:Name="btn_home" Margin="300,10,0,0" Click="btn_home_Click_1" Style="{StaticResource HomeAppBarButtonStyle }"/>
<Button x:Name="btn_user" Margin="50,10,0,0" Click="btn_user_Click_1" Style="{StaticResource SearchAppBarButtonStyle}"/>
<Button x:Name="btn_hotel" Margin="50,10,0,0" Click="btn_hotel_Click_1" Style="{StaticResource SaveAppBarButtonStyle}" />
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
</Page>
c# code in mainpage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Windows;
using All_hotels.myhotelappwebservice;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace All_hotels
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
grid_myhotelinfo.Visibility = Visibility.Visible;
}
//user search information enabled on button click user
private void btn_user_Click_1(object sender, RoutedEventArgs e)
{
if (grid_hotelinfo.Visibility == Visibility.Visible)
{
grid_hotelinfo.Visibility = Visibility.Collapsed;
grid_userinfo.Visibility = Visibility.Visible;
}
else if(grid_myhotelinfo.Visibility==Visibility.Visible)
{
grid_myhotelinfo.Visibility = Visibility.Collapsed;
grid_userinfo.Visibility = Visibility.Visible;
}
else
grid_userinfo.Visibility = Visibility.Visible;
}
//hotel information enabled on button click hotel
private void btn_hotel_Click_1(object sender, RoutedEventArgs e)
{
if (grid_userinfo.Visibility == Visibility.Visible)
{
grid_userinfo.Visibility = Visibility.Collapsed;
grid_hotelinfo.Visibility = Visibility.Visible;
}
else if(grid_myhotelinfo.Visibility==Visibility.Visible)
{
grid_myhotelinfo.Visibility=Visibility.Collapsed;
grid_hotelinfo.Visibility = Visibility.Visible;
}
else
grid_hotelinfo.Visibility = Visibility.Visible;
}
//my hotel app information details will be displayed on clicking home button
private void btn_home_Click_1(object sender, RoutedEventArgs e)
{
if (grid_hotelinfo.Visibility == Visibility.Visible)
{
grid_hotelinfo.Visibility = Visibility.Collapsed;
grid_myhotelinfo.Visibility = Visibility.Visible;
}
else if (grid_userinfo.Visibility == Visibility.Visible)
{
grid_userinfo.Visibility = Visibility.Collapsed;
grid_myhotelinfo.Visibility = Visibility.Visible;
}
else
grid_myhotelinfo.Visibility = Visibility.Visible;
}
//hotel information will be saved in database
private void btn_savehotelinfo_Click_1(object sender, RoutedEventArgs e)
{
string hotelname = txt_hotelname.Text,
country = txt_country.Text,
state = txt_state.Text,
district = txt_district.Text,
Area = txt_Area.Text,
street = txt_street.Text;
long contactno=Convert.ToInt64(txt_contactno.Text);
myhotelappwebserviceSoapClient obj = new myhotelappwebserviceSoapClient();
obj.savehotelinfoAsync(hotelname,country,state,district,Area,street,contactno);
}
//user searched hotels list in that area will be displayed
private async void btn_search_Click_1(object sender, RoutedEventArgs e)
{
object[] a;
if (grid_userinfo.Visibility == Visibility.Visible)
{
grid_userinfo.Visibility = Visibility.Collapsed;
listviewofhotels.Visibility = Visibility.Visible;
}
myhotelappwebserviceSoapClient obj = new myhotelappwebserviceSoapClient();
a = await obj.allhotelsAsync();
string[] rowarr = new string[a.Length];
for (int i = 0; i < a.Length; i++)
{
string s = a[i].ToString();
rowarr = s.Split(',');
//rowarr contains a row data
for (int j = 0; j < rowarr.Length; )
{
Hotels h = new Hotels();
h.hotelname = rowarr[j];
j++;
h.country = rowarr[j];
j++;
h.state = rowarr[j];
j++;
h.district = rowarr[j];
j++;
h.area = rowarr[j];
j++;
h.street = rowarr[j];
j++;
h.contactno = rowarr[j].ToString();
j++;
listofhotels.Items.Add(h);
}
}
}
}
public class Hotels
{
public string hotelname { set; get; }
public string country { set; get; }
public string state { set; get; }
public string district { set; get; }
public string area { set; get; }
public string street { set; get; }
public string contactno { set; get; }
}
}