I am using this code to update table record using linq. Please tell me what to do as code is executing but no changes are being made to database. Code is as follows:
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
bool temp = false; ;
nsmainDataContext stc = new nsmainDataContext();
nodeinfo tbl = stc.nodeinfos.Where(m => m.nodeid == Convert.ToInt32(DropDownList1.SelectedValue)).SingleOrDefault();
if(RadioButtonList1.SelectedItem==null)
{
Label3.Visible = true;
Label3.Text = "Please select a valid choice";
}
else
{
if(RadioButtonList1.SelectedIndex==1)
{
temp = true;
tbl.nodetstatus = temp;
}
else
if(RadioButtonList1.SelectedIndex==2)
{
temp = false;
tbl.nodetstatus = temp;
}
tbl.statuschangelog = System.DateTime.Now.ToString();
//stc.SubmitChanges();
Label3.Visible = true;
Label3.Text = "Status of node " + DropDownList1.SelectedValue + " successfully changed";
}
}


Ramesh MaruthiRank #937 · 2.2K reputationPosted Nov 17, 2014, 4:03 PM EST
after changing the values you need to save the changes of the context
try
{
stc.savechanges();
}
catch(Exception ex)
{
throw ex;
}
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Nov 17, 2014, 9:02 PM EST
it will save the change to database. without using this method whatever change you made will not reflect to database.
Ananth Padmanabham vemula98 reputationPosted Jan 2, 2015, 12:39 AM EST
stc.SubmitChanges();
it will save the change to database.
Suraj SahooRank #382 · 6.6K reputationPosted Jan 12, 2015, 10:48 PM EST
In the query you have posted, your context i.e. stc would be having the context for each entities like the nodeinfoes for nodeinfo table.
So you need to use
stc.nodeinfoes.AddOrUpdate(tbl);//nodeinfoes or any table context entity you will be updating
stc.SaveChanges();
This will then search for the primary key in the tbl object and then update if already present else will add an object with these records.
I hope you got this.
Please post back your queries if any.
Thanks