Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help, on how to use linq in Asp.net in inserting, updating, deleting data in the database.....
Posted

Hope you are using EF..

1. For insert you no need to go for LINQ,
just take instance object of table from Context and add table object.

2.Update operation..
C#
protected void btnUpdate_Click(object sender, EventArgs e)
{
    int ContactID = Convert.ToInt32(Request.QueryString["ContactID"]); //This is contact reference ID, i am taking it from QS..
    ContactContext ctxContact = new ContactContext();
    tblContact Contact = ctxContact.tblContacts.SingleOrDefault(ID => ID.ID == ContactID); //Here ID is primary key of the table, This is for update reference.
    Contact.Name = txtName.Text;
    Contact.Address = txtAddress.Text;
    Contact.PhoneNo = Convert.ToInt64(txtPhone.Text);
    if (objDB.SaveChanges() == 1)    //Save method will return 1 if success..
    {
        //Success alert..
    }
}


3.Delete operation..
C#
protected void btnDelete_RowCommand(object sender, EventArgs e)
{
    int ContactID = Convert.ToInt32(Request.QueryString["ContactID"]); //This is contact reference ID, i am taking it from QS..
    dbTestEntities objDB = new dbTestEntities();
    tblContact obj=objDB.tblContacts.SingleOrDefault(ID=>ID.ID==intContactID);
    objDB.tblContacts.DeleteObject(obj);
    if(objDB.SaveChanges()==1)    //Save method will return 1 if success..
    {
        //Success alert..
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900