Beginning LINQ to SQL Using Visual Studio 2008 - CRUD operations using the LINQ to SQL application (without LINQ designer): Source in C#
(Page 6 of 6 )
The following is the source code in C# 2008 for the UI designed in the previous sections:
using System.Data.Linq;
namespace SampleCS
{
public partial class CRUDSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.lblMsg.Text = string.Empty;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
try
{
DataContext db = new DataContext(new System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"));
//getting table
Table<Emp> tblEmps = db.GetTable<Emp>();
//getting an existing row
Emp objEmp = tblEmps.SingleOrDefault(p => p.empno == Int32.Parse( this.txtEmpno.Text ));
if (objEmp!=null )
{
this.txtEname.Text = objEmp.ename;
this.txtSal.Text = Convert.ToString( objEmp.sal) ;
this.txtDeptno.Text = Convert.ToString(objEmp.deptno) ;
}
else
this.lblMsg.Text = "Employee not found";
}
catch (Exception ex)
{
this.lblMsg.Text = ex.Message;
}
}
protected void btnSelectStar_Click(object sender, EventArgs e)
{
DataContext db = new DataContext(new System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"));
Table<Emp> tblEmps = db.GetTable<Emp>();
this.GridView1.DataSource = tblEmps;
this.GridView1.DataBind();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
try
{
DataContext db = new DataContext(new System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"));
//getting table
Table<Emp> tblEmps = db.GetTable<Emp>();
//adding a new row
Emp rEmp = new Emp {empno = Int32.Parse(this.txtEmpno.Text), ename = this.txtEname.Text, sal = Double.Parse(this.txtSal.Text), deptno = Int32.Parse(this.txtDeptno.Text)};
tblEmps.InsertOnSubmit(rEmp);
//saving rows added
db.SubmitChanges();
this.lblMsg.Text = "Added Successfully";
}
catch (Exception ex)
{
this.lblMsg.Text = ex.Message;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
DataContext db = new DataContext(new System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"));
//getting table
Table<Emp> tblEmps = db.GetTable<Emp>();
//getting an existing row
Emp objEmp = tblEmps.SingleOrDefault(p => p.empno == Int32.Parse(this.txtEmpno.Text));
if (objEmp != null)
{
//modifying the row
objEmp.ename=this.txtEname.Text ;
objEmp.sal = Double.Parse(this.txtSal.Text);
objEmp.deptno = Int32.Parse(this.txtDeptno.Text);
//saving rows modified
db.SubmitChanges();
this.lblMsg.Text = "Updated Successfully";
}
else
this.lblMsg.Text = "Employee not found";
}
catch (Exception ex)
{
this.lblMsg.Text = ex.Message;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
DataContext db = new DataContext(new System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"));
//getting table
Table<Emp> tblEmps = db.GetTable<Emp>();
//getting an existing row
Emp objEmp = tblEmps.SingleOrDefault(p => p.empno == Int32.Parse(this.txtEmpno.Text));
if (objEmp != null)
{
//delete the row
tblEmps.DeleteOnSubmit(objEmp);
//saving rows modified
db.SubmitChanges();
this.lblMsg.Text = "Deleted Successfully";
}
else
this.lblMsg.Text = "Employee not found";
}
catch (Exception ex)
{
this.lblMsg.Text = ex.Message;
}
}
}
}
My upcoming articles will focus further in-depth on "LINQ to SQL" (including designers). I hope you enjoyed this article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |