[ServiceContract]
public interface IService1
{
public interface IService1
{
[OperationContract]
Employee[] GetEmployeesList();
Employee[] GetEmployeesList();
[OperationContract]
Employees[] GetEmployeesListID(Int32 empId);
}
Employees[] GetEmployeesListID(Int32 empId);
}
[DataContract]
public class Employee
{
public Int32 _employee_ID;
public string _firstName;
public string _lastName;
[DataMember]
public Int32 Employee_ID
{
get { return _employee_ID; }
set { _employee_ID = value; }
}
[DataMember]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
[DataMember]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
public class Employee
{
public Int32 _employee_ID;
public string _firstName;
public string _lastName;
[DataMember]
public Int32 Employee_ID
{
get { return _employee_ID; }
set { _employee_ID = value; }
}
[DataMember]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
[DataMember]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
[DataContract]
public class Employees
{
public class Employees
{
public string _firstName;
public string _lastName;
public string _lastName;
[DataMember]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
[DataMember]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
[DataMember]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
3) Open the “Service1.svc.cs” and add the below code.
using System.Data.SqlClient;
using System.Data;
using System.Data;
public class Service1 : IService1
{
Employee[] IService1.GetEmployeesList()
{
{
Employee[] IService1.GetEmployeesList()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());
SqlCommand com = new SqlCommand(“select userid,firstname,lastname from Users order by userid”, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);
int RCount = dt.Rows.Count;
Employee[] arrEmp = new Employee[RCount];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
arrEmp[i] = new Employee();
arrEmp[i]._employee_ID = Convert.ToInt32(dr["userid"]);
arrEmp[i]._firstName = dr["firstname"].ToString();
arrEmp[i]._lastName = dr["lastname"].ToString();
i++;
DataTable dt = new DataTable();
da.Fill(dt);
int RCount = dt.Rows.Count;
Employee[] arrEmp = new Employee[RCount];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
arrEmp[i] = new Employee();
arrEmp[i]._employee_ID = Convert.ToInt32(dr["userid"]);
arrEmp[i]._firstName = dr["firstname"].ToString();
arrEmp[i]._lastName = dr["lastname"].ToString();
i++;
}
return arrEmp;
return arrEmp;
}
Employees[] IService1.GetEmployeesListID(Int32 empId)
{
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());
SqlCommand com = new SqlCommand(“select firstname,lastname from Users where userid=” + empId , con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);
int RCount = dt.Rows.Count;
Employees[] arrEmps = new Employees[RCount];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
arrEmps[i] = new Employees();
arrEmps[i]._firstName = dr["firstname"].ToString();
arrEmps[i]._lastName = dr["lastname"].ToString();
i++;
DataTable dt = new DataTable();
da.Fill(dt);
int RCount = dt.Rows.Count;
Employees[] arrEmps = new Employees[RCount];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
arrEmps[i] = new Employees();
arrEmps[i]._firstName = dr["firstname"].ToString();
arrEmps[i]._lastName = dr["lastname"].ToString();
i++;
}
return arrEmps;
return arrEmps;
}
}
}
4) Now add the connectionstring in web.config file
<appSettings>
<add key=”ConnectionString” value=”server=ip/servername;database=DBname;uid=usename;password=password;”/>
</appSettings>
<add key=”ConnectionString” value=”server=ip/servername;database=DBname;uid=usename;password=password;”/>
</appSettings>
5) add the endpoint in Web.config, i.e just below the
tag, Add client tag and add the ebdpoint, Then paste the url you have copied from the browser in endpoint “address” and Build the project.
tag, Add client tag and add the ebdpoint, Then paste the url you have copied from the browser in endpoint “address” and Build the project.
6) Run your project, you will get a page as below, Now copy the URL in the browser.
7) Now, Open Visual studio 2008/2010 –> New Projet, Select Visual C# –> web, then select ASP.NET Web Application(I have renamed the name as “Tester”).
8) Click on solution name and then go to “Add Service Reference” one window will open, paste the previously copied link to the address bar and press GO. Then
the service will appear in service section, then press OK. The service reference will appear in solution explorer as below:
9) In default.aspx page, add GridView1, below code b/w the form tags / or add the controls manually from tool box.
<asp:GridView ID=”GridView1″ runat=”server”></asp:GridView>
<asp:Label ID=”Label1″ runat=”server” Text=”Search”></asp:Label>
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox>Please enter User ID
<asp:GridView ID=”GridView2″ runat=”server”></asp:GridView>
<asp:Button ID=”Button1″ runat=”server” Text=”Click” onclick=”Button1_Click” />
<asp:Label ID=”Label1″ runat=”server” Text=”Search”></asp:Label>
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox>Please enter User ID
<asp:GridView ID=”GridView2″ runat=”server”></asp:GridView>
<asp:Button ID=”Button1″ runat=”server” Text=”Click” onclick=”Button1_Click” />
10) In default.aspx.cs, add the below code,
using System.Web.UI.MobileControls;
using tester.ServiceReference1;
using tester.ServiceReference1;
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client objsrv = new tester.ServiceReference1.Service1Client();
objsrv.Open();
{
ServiceReference1.Service1Client objsrv = new tester.ServiceReference1.Service1Client();
objsrv.Open();
GridView1.DataSource = objsrv.GetEmployeesList();
GridView1.DataBind();
Response.Write(“<br/>”);
GridView2.DataSource = objsrv.GetEmployeesListID(Convert.ToInt32(TextBox1.Text));
GridView2.DataBind();
}
GridView1.DataBind();
Response.Write(“<br/>”);
GridView2.DataSource = objsrv.GetEmployeesListID(Convert.ToInt32(TextBox1.Text));
GridView2.DataBind();
}
11) Now, save and build the file and run the project. If everything goes fine, then the output will be like this:
Hope you will enjouy this, cheers