0

这是我想要做的,我正在使用 LinQ 数据上下文将新记录插入数据库,由于某种原因,我的 linq 显示此错误“LinqDataSource 'AirAsiaDC' 没有要插入的值。检查'values'字典是否包含价值观。” 奇怪的部分,当我检查我之前尝试插入的记录时,它就在那里。我在这里做错了什么?

顺便说一句,这是我的代码:

protected void DetailsView1_ItemCommand(object sender, 
                                        DetailsViewCommandEventArgs e)
{
    AirAsiaDCDataContext Linq = new AirAsiaDCDataContext();

    try
    {
        if (e.CommandName == "Edit")
        {
            TextBox tbname = (TextBox)DetailsView1.FindControl("DeptName");
            department dpt;

            if ((tbname.Text != null) && (tbname.Text != ""))
            {
                dpt = Linq.departments.Single(d => d.departmentcode == code);
                dpt.departmentname = tbname.Text;
                dpt.updateby = "hendra";
                dpt.lastupdate = DateTime.Now;
                Linq.SubmitChanges();
                Response.Redirect("Department.aspx");
            }
            else
            {
                divError.Visible = true;
                lblError.Text = "Department name can not be empty";
            }
        }
        else if (e.CommandName == "Insert")
        {
            TextBox tbcode = (TextBox)DetailsView1.FindControl("DeptCode");
            TextBox tbname = (TextBox)DetailsView1.FindControl("DeptName");
            department dpt = new department();

            if (((tbcode.Text != null) && (tbcode.Text != "")) 
             && ((tbname.Text != null) && (tbname.Text != "")))
            {
                dpt.departmentcode = tbcode.Text;
                dpt.departmentname = tbname.Text;
                dpt.createby = "hendra";
                dpt.createdate = DateTime.Now;
                dpt.updateby = "hendra";
                dpt.lastupdate = DateTime.Now;
                Linq.departments.InsertOnSubmit(dpt);
                Linq.SubmitChanges();
            }
            else if ((tbcode.Text == null) || (tbcode.Text == ""))
            {
                divError.Visible = true;
                lblError.Text = "Department code can not be empty";
            }
            else if ((tbname.Text == null) || (tbname.Text == ""))
            {
                divError.Visible = true;
                lblError.Text = "Department name can not be empty";
            }
        }
    }
    catch (Exception ex)
    {
        divError.Visible = true;
        lblError.Text = ex.Message.ToString();
    }
}

我什至尝试使用 try 和 catch 但什么也没得到,我的 linq 继续显示该错误并将我扔到错误屏幕。

4

0 回答 0