How to Implemented WCF Service Insert and Fetch Records from Database in Web Application.


In this Article, I am going to explain about how to Implemented WCF Service Insert and Fetch Records from Database in Web Application.I have Implemented WCF Service Insert and Fetch Records from Database in Web Application.I have post source given below

How to Implemented WCF Service Insert and Fetch Records from Database in Web Application



Here I have paste the code for Entry the Insert Records aspx Page

<form id="form1" runat="server">
EmpNo :<asp:TextBox ID="TxtEmpNo" runat ="server"></asp:TextBox>
EmpName :<asp:TextBox ID="TxtEmpName" runat ="server"></asp:TextBox>
<asp:Button ID="BtSubmit" runat ="server" Text ="Submit" OnClick="BtSubmit_Click" />
<asp:Label ID="lblSucess" runat ="server" ></asp:Label>

<asp:GridView ID="Grd1" runat ="server"></asp:GridView>
</form>


How to Call WCF Service and Fetch Records From dataBase using linq Object Bind the Gridview


EmployeeDetails.Service1Client obj;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
obj = new EmployeeDetails.Service1Client();
Grd1.DataSource = obj.GetEmployees();
Grd1.DataBind();
}
}


How to Call WCF Service and Save Records From DataBase using linq Object and Bind the Gridview

protected void BtSubmit_Click(object sender, EventArgs e)
{
EmployeeDetails.Employee objcls=new EmployeeDetails.Employee();
obj = new EmployeeDetails.Service1Client();
objcls.EmpNo=TxtEmpNo.Text ;
objcls.EmpName=TxtEmpName.Text;
bool result = obj.InsertEmployee(objcls);
if (result == true)
{
lblSucess.Text = "Saved Data";
}
obj = new EmployeeDetails.Service1Client();
Grd1.DataSource = obj.GetEmployees();
Grd1.DataBind();
}


How to Implemented WCF Service Class Object

[DataContract]
public class Employee
{
[DataMember]
public int Id;
[DataMember]
public string EmpName;
[DataMember]
public string EmpNo;
}


How to Implemented WCF Service Interface Class

[ServiceContract]
public interface IService1
{
[OperationContract]
bool InsertEmployee(Employee obj);
[OperationContract]
List GetEmployees();
}


How to Establish the Connection In WCF SERVICE SVC File


SqlConnection sqlcon = new SqlConnection("data Source=PC-NAME;Initial Catalog=Test;Integrated Security=True;");
public bool InsertEmployee(Employee obj)
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("Insert into TblEmp (EmpNo,EmpName) values('"+ obj.EmpNo +"','"+ obj.EmpName +"')", sqlcon);
int result= sqlcmd.ExecuteNonQuery();
return true;
}


How to Get the Records from Databse in WCF File

public List GetEmployees()
{
List employeelist = new List();

using (SqlConnection con = new SqlConnection("data Source=PC-NAME;Initial Catalog=Test;Integrated Security=True;"))
{
using (SqlCommand cmd = new SqlCommand("select * from TblEmp", con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
Employee emplist = new Employee();
while (sdr.Read())
{
emplist.Add(new Employee());
emplist.Id = Convert.ToInt32(sdr["Id"]);
emplist.EmpNo = sdr["EmpNo"].ToString();
emplist.EmpName = sdr["EmpName"].ToString();
employeelist.Add(emplist);
}
}
}
}
return employeelist;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: