Using simple LINQ query and LINQ to sql class.
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select linq to sql classes.Name as Exercise.
Select your database, tables, stored procedures if required.
Right click on <Projects>.web Project and select
add new item
select silverlight Tab and select silverlight enabled wcf service.Name as WCFexample.
Add the following code to get data from table
[OperationContract]
List<nvmsl_ClientUser> GetUsers()
{
//Creating object for Exercise.dbml.cs class's ExerciseDataContext
ExerciseDataContext dc = new ExerciseDataContext();
var users = from user in dc.nvmsl_ClientUsers select user;
return users.ToList();
}
Test the webservice:
To test the web service, reight click on service and select view in browser.
Copy the link.
Open visual studio 2010 command prompt from visual studio 2010 tools.
Type wcftestclient and enter.
One window will open, right click on My Project services from the right side of the window.
Select add service
Paste the link and click add.
Select the required function and select invoke.
Testing completed successfully.
Adding service reference.
Right click on <project> select add service reference.
Select discover.
Select required service and add namespace.
Add a silver light page
-> And add datagrid control name it as userdatagrid, ser Autogenerated column property = True.
->And add the following code in page to bind data to userdatagrid.
public <Pageconstructor>()
{
InitializeComponent();
WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
ws.GetUsersCompleted += new EventHandler<WCFexampleService.GetUsersCompletedEventArgs>(ws_GetUsersCompleted);
ws.GetUsersAsync();
}
void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
{
UserdataGrid.ItemsSource = e.Result;
}
Run the application.
Using ADO.Net Entity Data Model, simple LINQ Query.
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Right click on <Projects>.web Project and select
add new item
select silverlight Tab and select silverlight enabled wcf service.Name as WCFexample.
Add the following code to get data from table
[OperationContract]
List<nvmsl_ClientUsers> GetUsers()
{
NitsWebSLEntities dc = new NitsWebSLEntities();
var users = from user in dc.nvmsl_ClientUsers select user;
return users.ToList();
}
Test the webservice:
To test the web service, reight click on service and select view in browser.
Copy the link.
Open visual studio 2010 command prompt from visual studio 2010 tools.
Type wcftestclient and enter.
One window will open, right click on My Project services from the right side of the window.
Select add service
Paste the link and click add.
Select the required function and select invoke.
Testing completed successfully.
Adding service reference.
Right click on <project> select add service reference.
Select discover.
Select required service and add namespace.
Add a silver light page
-> And add datagrid control name it as userdatagrid, ser Autogenerated column property = True.
->And add the following code in page to bind data to userdatagrid.
public <Pageconstructor>()
{
InitializeComponent();
WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
ws.GetUsersCompleted += new EventHandler<WCFexampleService.GetUsersCompletedEventArgs>(ws_GetUsersCompleted);
ws.GetUsersAsync();
}
void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
{
UserdataGrid.ItemsSource = e.Result;
}
Using ADO.Net Entity Data Model, Stored procedure.( Returns a collection of : Entities.)
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Create a stored procedure named Tnvmsl_GetClientUsers.
CREATE PROCEDURE [dbo].[Tnvmsl_GetClientUsers]
AS
BEGIN
select * from dbo.nvmsl_ClientUsers
END
Right click on WCFexampleModel.edmx select model browser.
From the left corner Tree view select Tnvmsl_GetClientUsers Procedure in stored procedures in NitsWebSLModel.Store.
Right click on Tnvmsl_GetClientUsersselect Add function Import.
Select Returns a collection of : Entities.
Add the following code in the service
[OperationContract]
List<nvmsl_ClientUsers> Get_ClientUsers()
{
NitsWebSLEntities dc = new NitsWebSLEntities();
return dc.Tnvmsl_GetClientUsers().ToList();
}
Add the following code in Silverlight page.
WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
ws.Get_ClientUsersCompleted += new EventHandler<WCFexampleService.Get_ClientUsersCompletedEventArgs>(ws_Get_ClientUsersCompleted);
ws.Get_ClientUsersAsync();
void ws_Get_ClientUsersCompleted(object sender, WCFexampleService.Get_ClientUsersCompletedEventArgs e)
{
UserdataGrid.ItemsSource = e.Result;
}
Using ADO.Net Entity Data Model, Stored procedure.( Returns a collection of : Scalars.)
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Create a stored procedure named Tnvmsl_GetUserName.
ALTER PROCEDURE [dbo].[Tnvmsl_GetUserName]
(@UserId int)
AS
BEGIN
select UserName from dbo.nvmsl_ClientUsers where UserId=@UserId
END
Right click on WCFexampleModel.edmx select model browser.
From the left corner Tree view select Tnvmsl_GetUserName Procedure in stored procedures in NitsWebSLModel.Store.
Right click on Tnvmsl_GetUserName Add function Import.
Select Returns a collection of : Scalars
Add the following code in the service
[OperationContract]
List<string> Get_UserNameByUserID(int id)
{
NitsWebSLEntities dc = new NitsWebSLEntities();
return dc.Tnvmsl_GetUserName(id).ToList();
}
Add the following code in Silverlight page.
WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
ws.Get_UserNameByUserIDCompleted += new EventHandler<WCFexampleService.Get_UserNameByUserIDCompletedEventArgs>(ws_Get_UserNameByUserIDCompleted);
ws.Get_UserNameByUserIDAsync(1);
void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
{
UserdataGrid.ItemsSource = e.Result;
}
Using ADO.Net Entity Data Model, Stored procedure.( Returns a collection of : Complex Types.)
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Create a stored procedure named nvmsl_GetClient.
ALTER PROCEDURE [dbo].[nvmsl_GetClient]
@ClientID int,
@Active bit
AS
BEGIN
select c.ClientID,cu.UserName,cu.Password,cu.Email,cu.PasswordQuestion,cu.PasswordAnswer from dbo.nvmsl_ClientUsers cu,dbo.nvm_Client c where c.ClientID=@ClientID and c.Active=@Active and c.ClientID=cu.ClientID
END
Right click on WCFexampleModel.edmx select model browser.
From the left corner Tree view select nvmsl_GetClient Procedure in stored procedures in NitsWebSLModel.Store.
Right click on nvmsl_GetClient Add function Import.
Select Returns a collection of : Complex
Click the button Get column information.
And click the button Create new complex types.
You will get a name “nvmsl_GetClient_Result” after clicking the above button.
Add the following code in the service
[OperationContract]
List<nvmsl_GetClient_Result> Get_Clients(int id, bool status)
{
NitsWebSLEntities dc = new NitsWebSLEntities();
return dc.nvmsl_GetClient(id, status).ToList();
}
Add the following code in Silverlight page.
WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
ws.Get_ClientsCompleted += new EventHandler<WCFexampleService.Get_ClientsCompletedEventArgs>(ws_Get_ClientsCompleted);
ws.Get_ClientsAsync(6,true);
void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
{
UserdataGrid.ItemsSource = e.Result;
}