SQL to LINQ

CRUD OPERATION :

First we need to add one dbml file from new items i.e .dbml file (DataBase Markup Language file)

in which we need to add all the tables from database.

create an object of DataContext dbobj and also create an object of EmpDetail table.

Code:

DataClasses1DataContext dbobj = new DataClasses1DataContext();
EmpDetail empobj = new EmpDetail();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

//ShowData will display a data into Gridview.
ShowData();
}

}
private void ShowData()
{
var mydata = from c in dbobj.EmpDetails select c;
gvEmpData.DataSource = mydata;
gvEmpData.DataBind();
}
//insert name and address on button_Click event
protected void InsertData_Click(object sender, EventArgs e)
{
empobj.Name = txtName.Text;
empobj.Address = txtAddress.Text;

dbobj.EmpDetails.InsertOnSubmit(empobj);
            dbobj.SubmitChanges();
txtName.Text = “”;
txtAddress.Text = “”;
ShowData();

}

protected void gvEmpData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmpData.PageIndex = e.NewPageIndex;
ShowData();
}

protected void gvEmpData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvEmpData.EditIndex = -1;
ShowData();
}

protected void gvEmpData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

//int no = Convert.ToInt32(gvEmpData.Rows[e.RowIndex].Cells[3].Text);

int idno = (int)gvEmpData.DataKeys[e.RowIndex].Value;

//   var delete = from s in ds.employee_masters
//               where s.id == idno
//               select s;

// LAMBDA QUERY
var delete = dbobj.EmpDetails.Where(n => n.Id == idno);
dbobj.EmpDetails.DeleteAllOnSubmit(delete);
dbobj.SubmitChanges();
ShowData();

}

protected void gvEmpData_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmpData.EditIndex = e.NewEditIndex;
//gvEmpData.Columns[0].Visible=false;
//To Unedit Id Cloumn
gvEmpData.HeaderRow.Cells[0].Visible = false;
ShowData();
}

protected void gvEmpData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

empobj = dbobj.EmpDetails.Where(n => n.Id == (int)gvEmpData.DataKeys[e.RowIndex].Value).Single();

empobj.Name = ((TextBox)gvEmpData.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
empobj.Address = ((TextBox)gvEmpData.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

dbobj.SubmitChanges();
gvEmpData.EditIndex = -1;
ShowData();
}

 

2 thoughts on “SQL to LINQ

  1. For binding LIstView we need to use property called “ItemsSource ”
    for e.g
    var mydata = from c in dbobj.CompanyDatas select c;
    listView1.ItemsSource = mydata;
    listView1.SelectedIndex = 0;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s