错误:值不能为空.参数名称:实体

本文关键字:实体 参数 不能 错误 | 更新日期: 2023-09-27 18:33:22

我在使用 linq 插入数据时收到此错误。在此我有一个表,其中有两列,一列是标识列,另一列是可为空的选框 txt。请提供解决方案。我的代码在这里。

   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;
        }
    }

错误:值不能为空.参数名称:实体

你真的需要提供更多信息。由于缺乏信息,有点难以分辨,但是您已经创建了newMarquee但随后existing传递到InsertOnSubmit函数中。我不知道existing来自哪里,因为它不在代码中,但也许这是空的,这就是您收到此错误的原因?

说您编辑了代码,但问题仍然存在于您显示的内容中:在"else"部分中,存在为空,因此当您将其传递到 InsertOnSubmit 时,它可能会抛出错误。当该错误为空时,InsertOnSubmit将抛出该确切的错误。您希望context.Marquees.InsertOnSubmit(newMarquee);该行。