IDBService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IDBService" in both code and config file together.
[ServiceContract]
public interface IDBService
{
[OperationContract]
List<Emp> getAllEmps();
[OperationContract]
void insertEmp(Emp empobj);
[OperationContract]
void updateEmp(Emp empobj);
[OperationContract]
List<Emp> getOneEmps(Emp empobj);
}
[DataContract]
public class Emp
{
int _empno;
string _ename;
double _sal;
int _deptno;
[DataMember]
public int empno
{
get { return _empno; }
set { _empno = value; }
}
[DataMember]
public string ename
{
get { return _ename; }
set { _ename = value; }
}
[DataMember]
public int deptno
{
get { return _deptno; }
set { _deptno = value; }
}
[DataMember]
public double sal
{
get { return _sal; }
set { _sal = value; }
}
}
DBService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "DBService" in code, svc and config file together.
public class DBService : IDBService
{
#region IDBService Members
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
SqlCommand cmd = new SqlCommand();
public List<Emp> getAllEmps()
{
cmd.CommandText = "select * from empinfo";
cmd.Connection = con;
SqlDataReader reader;
List<Emp> emplist = new List<Emp>();
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Emp empobj = new Emp();
empobj.empno = int.Parse(reader["empno"].ToString());
empobj.ename = reader["ename"].ToString();
empobj.sal = double.Parse(reader["sal"].ToString());
empobj.deptno = int.Parse(reader["deptno"].ToString());
emplist.Add(empobj);
}
con.Close();
return emplist;
}
public List<Emp> getOneEmps(Emp empobj)
{
cmd.CommandText="select * from empinfo where empno=@empno";
cmd.Connection=con;
cmd.Parameters.AddWithValue("@empno",empobj.empno);
SqlDataReader reader;
List<Emp> emplist=new List<Emp>();
con.Open();
reader=cmd.ExecuteReader();
while(reader.Read())
{
Emp empobj1 = new Emp();
empobj1.empno = int.Parse(reader["empno"].ToString());
empobj1.ename = reader["ename"].ToString();
empobj1.sal = double.Parse(reader["sal"].ToString());
empobj1.deptno = int.Parse(reader["deptno"].ToString());
emplist.Add(empobj1);
}
con.Close();
return emplist;
}
public void insertEmp(Emp empobj)
{
cmd.CommandText = "insert into empinfo (empno,ename,sal,deptno) values (@empno,@ename,@sal,@deptno)";
cmd.Parameters.AddWithValue("@empno", empobj.empno);
cmd.Parameters.AddWithValue("@ename", empobj.ename);
cmd.Parameters.AddWithValue("@sal", empobj.sal);
cmd.Parameters.AddWithValue("@deptno", empobj.deptno);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void updateEmp(Emp empobj)
{
cmd.CommandText = "update empinfo set ename=@ename,sal=@sal,deptno=deptno where empno=@empno";
cmd.Parameters.AddWithValue("@empno", empobj.empno);
cmd.Parameters.AddWithValue("@ename", empobj.ename);
cmd.Parameters.AddWithValue("@sal", empobj.sal);
cmd.Parameters.AddWithValue("@deptno", empobj.deptno);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
#endregion
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Welcome to WCF DataConract!
</h2>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="true" onselectedindexchanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="empno" HeaderText="EmpNo" SortExpression="empno" />
<asp:BoundField DataField="ename" HeaderText="EmpName" SortExpression="ename" />
<asp:BoundField DataField="sal" HeaderText="Esal" SortExpression="sal" />
<asp:BoundField DataField="deptno" HeaderText="EmpDeptNo" SortExpression="deptno" />
</Columns>
</asp:GridView>
</p>
<p align="center">
<asp:Button ID="btnInsert" runat="server" Text="Insert"
onclick="btnInsert_Click" />
</p>
<p align="center">
<asp:Button ID="btnUpdate" runat="server" Text="Update"
onclick="btnUpdate_Click" />
</p>
<p align="center">
<asp:Button ID="btnGet" runat="server" Text="Get"
onclick="btnget_Click" />
</p>
<p>
<table style="width: 100%;">
<tr>
<td class="style1">
Emp No :</td>
<td>
<asp:TextBox ID="txtEmpNo" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Emp Name :</td>
<td>
<asp:TextBox ID="txtEmpNmae" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Em Sal :</td>
<td>
<asp:TextBox ID="txtEmpSal" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Emp Dept No :</td>
<td>
<asp:TextBox ID="txtEmpDeptno" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
</table>
</p>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
ServiceReference1.DBServiceClient client = new ServiceReference1.DBServiceClient();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Show();
}
}
private void Show()
{
IList<Emp> empobj = new List<Emp>();
empobj = client.getAllEmps();
GridView1.DataSource = empobj;
GridView1.DataBind();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
ServiceReference1.Emp empobj = new ServiceReference1.Emp();
empobj.empno = int.Parse(txtEmpNo.Text);
empobj.ename = txtEmpNmae.Text;
empobj.sal = double.Parse(txtEmpSal.Text);
empobj.deptno = int.Parse(txtEmpDeptno.Text);
client.insertEmp(empobj);
txtEmpDeptno.Text = "";
txtEmpNmae.Text = "";
txtEmpNo.Text = "";
txtEmpSal.Text = "";
Show();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
ServiceReference1.Emp empobj = new ServiceReference1.Emp();
empobj.empno = int.Parse(txtEmpNo.Text);
empobj.ename = txtEmpNmae.Text;
empobj.sal = double.Parse(txtEmpSal.Text);
empobj.deptno = int.Parse(txtEmpDeptno.Text);
client.updateEmp(empobj);
txtEmpDeptno.Text = "";
txtEmpNmae.Text = "";
txtEmpNo.Text = "";
txtEmpSal.Text = "";
Show();
}
protected void btnget_Click(object sender, EventArgs e)
{
IList<Emp> empobj = new List<Emp>();
ServiceReference1.Emp empopb1 = new ServiceReference1.Emp();
empopb1.empno = int.Parse(txtEmpNo.Text);
empobj=client.getOneEmps(empopb1);
txtEmpNmae.Text = empobj[0].ename.ToString();
txtEmpSal.Text = empobj[0].sal.ToString();
txtEmpDeptno.Text = empobj[0].deptno.ToString();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
txtEmpNo.Text = GridView1.SelectedRow.Cells[1].Text;
txtEmpNmae.Text = GridView1.SelectedRow.Cells[2].Text;
txtEmpSal.Text = GridView1.SelectedRow.Cells[3].Text;
txtEmpDeptno.Text = GridView1.SelectedRow.Cells[4].Text;
}
}
No comments:
Post a Comment