Thursday, August 4, 2011

Accessing Databases In Silverlight Through WCF service.

That is how the solution explorer looks...



=================================================================
XAML
=================================================================
here is how the xaml looks like....



=================================================================
XAML.cs
=================================================================

public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void BtnSubmit_Click(object sender, RoutedEventArgs e)
{
var obj = new ServiceReference1.Service1Client();
obj.ValidateUsersCompleted +=new EventHandler(obj_ValidateUsersCompleted);
obj.ValidateUsersAsync(TxtUser.Text, TxtPass.Password);
}

public void obj_ValidateUsersCompleted(object sender, LoginPage.ServiceReference1.ValidateUsersCompletedEventArgs e)
{
if (e.Result == 1)
{
ErrorTxt.Text = "Loging Successfull!! You are being redirected!";
LoginSuccess success = new LoginSuccess();
success.Show();
}
else if(e.Result<=0)
{
ErrorTxt.Text="Incorrect Username or Password";
}
}


}

=============================================================
CREATING WCF SERVICE
=============================================================

1.Right click the web project and click "Add" and "New Item".
2.From Templates select the "Silverlight-Enabled WCF Service" and name the file.
3. add methods to the WCF service as follows...

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public int ValidateUsers(string UserName, string Password)
{
int count;
string connection = ("Data Source=JTL_045;Initial Catalog=shiva;User Id=sa;Password=dev2005");
SqlConnection conn = new SqlConnection(connection);
conn.Open();
SqlCommand comm = new SqlCommand("[Login_Authentication]", conn);
SqlParameter para = new SqlParameter("@UserName", UserName);
comm.Parameters.Add(para);
SqlParameter para1 = new SqlParameter("@Password", Password);
comm.Parameters.Add(para1);
comm.CommandType = CommandType.StoredProcedure;
count = (int)comm.ExecuteScalar();
return count;
conn.Close();
// Add more operations here and mark them with [OperationContract]
}
}

====================================================================
1.after creating wcf service, add service reference to the silverlight application and name it.
2.then create client for the service and call the methods in the service using the service client.
3.obj_ValidateUsersCompleted is the completed method,that is this method will be executing after the method in wcf service is being executed.its an optional one!!
4.this is how the silverlight can access databases.

see the screenshots of the out below!!!







Download the source code here

3 comments:

  1. Hi,
    nice article, but source code cannot be downloaded, it asks for credentials. It is possible to upload it to any public site for filesharing?
    Thanks

    ReplyDelete
  2. sorry buddy. now its available to everybody. sorry for the late replying too.

    ReplyDelete