Step 1:
Create a Database in sqlserver
create database customers
use customers
create table customerdetails
(
id int,
name varchar(20),
city varchar(20),
phone varchar(20)
)
Step 2:
Creating window service
Open Visual studio 2010 àSilverlight applicationàselect c# àclick okàRight Click the SolutionàaddàWindow service
Copy and Paste this code in the window service
//This is for inserting into the database
string myConnectionString = "Data Source=RAJIV\\SQLEXPRESS;Initial Catalog=directory;Integrated Security=True";
[WebMethod]
public void savedir(Person person)
{
using (SqlConnection con = new SqlConnection(myConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "savedirectory";
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = person.id;
cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = person.name;
cmd.Parameters.Add("@city", System.Data.SqlDbType.VarChar).Value = person.city;
cmd.Parameters.Add("@phone", System.Data.SqlDbType.VarChar).Value = person.phoneno;
con.Open();
cmd.ExecuteScalar();
}
}
}
//This is for Updating in the database
[WebMethod]
public void updatedir(int id, string name, string city, string phone)
{
SqlConnection con = new SqlConnection(myConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "updatedirectory";
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = name;
cmd.Parameters.Add("@city", System.Data.SqlDbType.VarChar).Value = city;
cmd.Parameters.Add("@phone", System.Data.SqlDbType.VarChar).Value = phone;
con.Open();
cmd.ExecuteScalar();
}
//This is for Deleting in the database
[WebMethod]
public void deletedir(int id)
{
SqlConnection con = new SqlConnection(myConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "deletedirectory";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
con.Open();
cmd.ExecuteScalar();
}
Save it in the following name “loginservice”
Step 3:
Within the Reference add the service created
Add this namespace:
using masterpage.loginservice;
Within the Main Page.xaml page copy and paste the given code
<Grid x:Name="LayoutRoot" Background="AntiqueWhite">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="80"/>
<RowDefinition Height="400"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="350"/>
</Grid.ColumnDefinitions>
<dataInput:Label x:Name="lblid" Grid.Row="0" FontWeight="Bold"
Grid.Column="0" Content="Phone Id:"
HorizontalAlignment="Right" Foreground="Chocolate"/>
<dataInput:Label x:Name="lblname" Grid.Row="1" Grid.Column="0"
Content="Name:" FontWeight="Bold"
Foreground="Chocolate" HorizontalAlignment="Right"/>
<dataInput:Label x:Name="lblcity" Grid.Row="2" Grid.Column="0" Content="City:"
FontWeight ="Bold" Foreground="Chocolate"
HorizontalAlignment="Right"/>
<dataInput:Label x:Name="lblphoneno" Grid.Row="3" Grid.Column="0" Foreground="Chocolate"
FontWeight="Bold" Content="Phone No:" HorizontalAlignment="Right"/>
<TextBox x:Name="txtid" Grid.Row="0" Grid.Column="1"
Height="22" Width="150" HorizontalAlignment="Center"/>
<TextBox x:Name="txtname" Grid.Row="1" Grid.Column="1"
Height="22" Width="150" HorizontalAlignment="Center"/>
<TextBox x:Name="txtcity" Grid.Row="2" Grid.Column="1"
Height="22" Width="150" HorizontalAlignment="Center"/>
<TextBox x:Name="txtphone" Grid.Row="3" Grid.Column="1"
Height="22" Width="150" HorizontalAlignment="Center"/>
<Button x:Name="btnsave" Grid.Row="4" Grid.Column="0"
Content="Save" FontWeight="Bold" Height="30" Width="100"
HorizontalAlignment="Left" Click="btnsave_Click"/>
<Button x:Name="btncancel" Grid.Row="4" Grid.Column="1" Margin="210,20,20,20"
Content="Clear" FontWeight="Bold" Height="30" Width="100" Click="btncancel_Click"/>
<Button x:Name="btnupdate" Grid.Row="4" Grid.Column="1" Margin="5,20,20,20"
Content="Update" FontWeight="Bold" Height="30" Width="100" HorizontalAlignment="Left" Click="btnupdate_Click" />
<Button x:Name="btndelete" Grid.Row="4" Grid.Column="1" Margin="110,20,20,20"
Content="Delete" FontWeight="Bold" Height="30" Width="100"
HorizontalAlignment="Left" Click="btndelete_Click" />
<data:DataGrid x:Name="grdPerson" Grid.Row="5" Grid.Column="1" Width="200"
AutoGenerateColumns="False" Background="Ivory" Margin="80,8,70,0"
Height="200" VerticalAlignment="Top" d:LayoutOverrides="Height" CellEditEnded="grdPerson_CellEditEnded"/>
</Grid>
Within the Mainpage.xaml.cs page
InitializeComponent();
grdPerson.Columns.Add(new DataGridTextColumn
{
Header = "ID",
Binding = new System.Windows.Data.Binding("id")
});
grdPerson.Columns.Add(new DataGridTextColumn
{
Header = "Name",
Binding = new System.Windows.Data.Binding("name")
});
grdPerson.Columns.Add(new DataGridTextColumn
{
Header = "City",
Binding = new System.Windows.Data.Binding("city")
});
grdPerson.Columns.Add(new DataGridTextColumn
{
Header = "Phone No",
Binding = new System.Windows.Data.Binding("phoneno"),
MaxWidth=150
});
load();
}
private void load()
{
loginservice.Trial_loginserviceSoapClient cli = new Trial_loginserviceSoapClient();
cli.getdirCompleted += new EventHandler<getdirCompletedEventArgs>(cli_getdirCompleted);
cli.getdirAsync();
}
// Executes when the user navigates to this page.
public void cli_getdirCompleted(object sender, loginservice.getdirCompletedEventArgs e)
{
grdPerson.ItemsSource = e.Result;
}
private void btnsave_Click(object sender, RoutedEventArgs e)
{
loginservice.Trial_loginserviceSoapClient cli = new Trial_loginserviceSoapClient();
Person person=new Person();
person.id = int.Parse(txtid.Text.ToString());
person.name = txtname.Text.ToString();
person.city = txtcity.Text.ToString();
person.phoneno = txtphone.Text.ToString();
cli.savedirAsync(person);
MessageBox.Show("Inserted successfully");
clear();
load();
}
private void btncancel_Click(object sender, RoutedEventArgs e)
{
txtid.Text = "";
txtname.Text = "";
txtcity.Text = "";
txtphone.Text = "";
}
public void clear()
{
txtid.Text = "";
txtname.Text = "";
txtcity.Text = "";
txtphone.Text = "";
}
private void btndelete_Click(object sender, RoutedEventArgs e)
{
loginservice.Trial_loginserviceSoapClient cli = new Trial_loginserviceSoapClient();
int id = Convert.ToInt32(txtid.Text.ToString());
cli.deletedirAsync(id);
clear();
load();
MessageBox.Show("Deleted successfully");
}
private void btnupdate_Click(object sender, RoutedEventArgs e)
{
loginservice.Trial_loginserviceSoapClient cli = new Trial_loginserviceSoapClient();
int id = Convert.ToInt32(txtid.Text.ToString());
string name = txtname.Text.ToString();
string city = txtcity.Text.ToString();
string phone = txtphone.Text.ToString();
cli.updatedirAsync(id, name, city, phone);
clear();
load();
MessageBox.Show("Updated successfully");
}
Step 4:
Run the application