Tuesday, June 22, 2010

connect silverlight with ms sql server

1. File->New Silverlight Application  and name, say “silverlight_wcf_sql”
2. Check the checkbox to host the silverlight application in a new web application “silverlight_wcf_sql.Web”
3. Silverlight page design
xaml_design
4. Add a new WCF Service and name it sql_wcf.svc
5. Add a method say “GetName()” like

[OperationContract]
public string GetName()
{
//refer attached file
}

OperationContract attribute above the method name indicates that this method can be called from a WCF client.
  • Refer the sample project attached.
6. Right click the Silverlight Application “silverlight_wcf_sql” and add Service Reference. Add “sql_wcf.svc” as Service Reference and name it “Silverlight_wcf_service_reference”
7. Go to MainPage.xaml and double click the button to generate BtnClickMe_Click event and type the code

private void BtnClickMe_Click(object sender, RoutedEventArgs e)
{
BtnClickMe.IsEnabled = false;
Silverlight_wcf_service_reference.sql_wcfClient myclient = new Silverlight_wcf_service_reference.sql_wcfClient();
myclient.GetNameCompleted += new EventHandler(myclient_GetNameCompleted);
myclient.GetNameAsync();
txtBlkSQL.Text = "Receiving data...";
LbMessage.Content = "http://www.rodriguesjax.blogspot.com";
}


void myclient_GetNameCompleted(object sender, Silverlight_wcf_service_reference.GetNameCompletedEventArgs e)
{
txtBlkSQL.Text = (string)e.Result;
BtnClickMe.IsEnabled = true;
}


Click here to download silverlight_wcf_sql.rar
use visual studio 2010

No comments:

Post a Comment