How to Implemented in Web Application for Insert,Update,delete, Search Using Linq To Sql
In this Article, I am going to explain about Implemented Insert,Update,delete, Search Using Linq To Sql .I have implemented Implemented Insert,Update,delete, Search Using Linq To Sql for ASP.net web application .I have post source given below
How to Implemented Insert,Update,delete, Search Using Linq To Sql
Client Side
Here i paste the code for aspx page for Insert,Update,delete,Search with linq . We design the aspx page like this for entry purpose
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Search Text <asp:TextBox ID="TxtSearch" runat="server"></asp:TextBox>
<br />
Name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Empno
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="bt1" runat ="server" Text ="Save" OnClick="bt1_Click" />
<asp:Button ID="bt2" runat ="server" Text ="GetData" OnClick="b2_Click" />
<asp:Button ID="BtSearch" runat ="server" Text ="Search" OnClick="BtSearch_Click" />
<asp:Button ID="BtUpdate" runat ="server" Text ="Update" OnClick="BtUpdate_Click" />
<asp:Button ID="BtDelete" runat ="server" Text ="Delete" OnClick="BtDelete_Click" />
<br />
<asp:GridView ID="Grd1" runat ="server"></asp:GridView>
</form>
</body>
</html> Server Side
This code for invoked the pageload situation load the records fetch from the linq object
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetEmpDetails();
}
}
This code for invoked the Button Click event Call the GetEmpDetails Method for fetch the Records from the linq object
protected void b2_Click(object sender, EventArgs e)
{
GetEmpDetails();
}
This Method GetEmpDetails Method for fetch the Records from the linq object
public void GetEmpDetails()
{
DbClassDataContext db = new DbClassDataContext();
var empDetails = from emplist in db.GetTable
Grd1.DataSource = empDetails;
Grd1.DataBind();
}
This Method Save the Records and Call the Method GetEmpDetails for fetch the Records from the linq object
protected void bt1_Click(object sender, EventArgs e)
{
DbClassDataContext db = new DbClassDataContext();
TblEmp emplst = new TblEmp();
emplst.EmpName = TextBox1.Text ;
emplst.EmpNo = TextBox2.Text;
db.TblEmps.InsertOnSubmit(emplst);
db.SubmitChanges();
GetEmpDetails();
}
This Method Update the Records and Call the Method GetEmpDetails for fetch the Records from the linq object
protected void BtUpdate_Click(object sender, EventArgs e)
{
DbClassDataContext db = new DbClassDataContext();
TblEmp emplist = db.TblEmps.Single(a => a.Id == Convert.ToInt32(TxtSearch.Text));
emplist.EmpNo =TextBox1.Text ;
emplist.EmpName = TextBox2.Text;
db.SubmitChanges();
GetEmpDetails();
}
This Method Delete the Records and Call the Method GetEmpDetails for fetch the Records from the linq object
protected void BtDelete_Click(object sender, EventArgs e)
{
DbClassDataContext db = new DbClassDataContext();
TblEmp emplist = db.TblEmps.Single(a => a.Id == Convert.ToInt32(TxtSearch.Text));
db.TblEmps.DeleteOnSubmit(emplist);
db.SubmitChanges();
GetEmpDetails();
}
This Method Search the Records and Call the Method GetEmpDetails for fetch the Records from the linq object
protected void BtSearch_Click(object sender, EventArgs e)
{
DbClassDataContext db = new DbClassDataContext();
TblEmp emplist = db.TblEmps.Single(a => a.Id == Convert.ToInt32(TxtSearch.Text));
TextBox1.Text=emplist.EmpNo;
TextBox2.Text=emplist.EmpName;
}