Menu

Linq operation

First declare  DataClassesDataContext. and create dbml file.


DataClassesDataContext db = new DataClassesDataContext();

here simple example to insert record in user master table.

first to drag and drop your table in dbml file.

Get Record.

int id = 1 ;//pass user id here;
User user = (from ga in db.Users
where ga.id == id
select ga).FirstOrDefault();

txtUserName.Text = user.UserName.ToString();

Insert Record.


User objUser = new User();
objUser.UserName = txtUserName.Text;
db.Users.InsertOnSubmit(objUser);
db.SubmitChanges();
if (objUser.id > 0)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Msg", "alert('Added');", true);
}

Updated Record.


User user = (from P in db.Users
where P.id == Convert.ToInt32(ViewState["EditId"])
select P).FirstOrDefault();
user.UserName = txtUserName.Text.ToString();
db.SubmitChanges();
ViewState.Remove("EditId");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Msg", "alert('Updated');", true);

Delete Record.

int id = Convert.ToInt32(((Label)grdlist.Rows[e.RowIndex].Cells[0].FindControl("lblId")).Text);

User userDelete = (from P in db.Users
where P.id == id
select P).FirstOrDefault();
if (userDelete.id > 0)
{
db.Users.DeleteOnSubmit(userDelete);
db.SubmitChanges();
bindGrid();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Msg", "alert('Deleted');", true);

}
else
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Msg", "alert('Sorry you can't Delete');", true);
}

No comments

Leave a Reply


1 + 1 =

Categories