I am getting this error while inserting data using linq. In this i have a table which has two columns one is identity column and another is nullable marquee txt. Please provide solution.
My Code is here .
protected void Button1_Click(object sender, EventArgs e)
{
try
{
Marquee existing = context.Marquees.FirstOrDefault();
if (existing != null)
{
existing.marquee1 = txtMarquee.Text;
context.SubmitChanges();
lblmsg.Text = "Data Saved Successfully";
generalFunction.goGreen(lblmsg);
}
else
{
Marquee newMarquee = new Marquee();
newMarquee.marquee1 = txtMarquee.Text;
context.Marquees.InsertOnSubmit(existing);
context.SubmitChanges();
lblmsg.Text = "Data Saved Successfully";
generalFunction.goGreen(lblmsg);
}
}
catch (Exception ee)
{
lblmsg.Text = "Error: " + ee.Message;
}
}
You really need to give more information. A little hard to tell because of the lack of information, but you’ve created
newMarqueebut then you’ve passedexistinginto theInsertOnSubmitfunction. I don’t know whereexistingcame from since its not in the code, but perhaps that’s null and that’s why you’re getting this error?You say you edited your code, but the problem still exists in what you’re showing: In the “else” section, existing is null, and so when you pass it into InsertOnSubmit it’s probably throwing an error. InsertOnSubmit will throw that exact error when that is null. You want that line to be
context.Marquees.InsertOnSubmit(newMarquee);instead.