c# - 我正在使用 linq 语句更新数据库中的数据

标签 c# mysql asp.net linq

我正在使用 LINQ 语句更新数据库中的数据。首先用户将搜索姓名,详细信息将显示在 gridview 中,然后用户将选择一名员工,数据将显示在相关文本框和下拉列表中。

当我在两个下拉列表中选择数据时,它工作正常并且数据库得到更新,如果我只选择一个下拉列表,它显示错误

"The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Emp_Location". The conflict occurred in database "EmpDept", table "dbo.Location", column 'LocationID'."

//更新按钮

using (DataClassesDataContext dc = new DataClassesDataContext())
{
    Emp employee = dc.Emps.Single(emp => emp.EmpID == id);
    employee.EmpFirstName = txtFirstName.Text;
    employee.EmpLastName = txtLastName.Text;
    employee.MgrID = int.Parse(txtMgrID.Text);
    employee.BirthDate = Convert.ToDateTime(txtDOB.Text);
    employee.Email = txtEmail.Text;
    employee.Mobile = txtMobile.Text;
    employee.LocationID = ddlLocation.SelectedIndex;
    employee.Salary = int.Parse(txtSalary.Text);
    employee.DeptID = ddlDepartment.SelectedIndex;
    //dc.Emps.InsertOnSubmit(employee);
    dc.SubmitChanges();

    ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Record Updated Successfully!!!')", true);
    clearFields();
}

// GridView

    protected void gvSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ddlDepartment.Items.RemoveAt(0);
            ddlLocation.Items.RemoveAt(0);

            string ddlDept = gvSearch.SelectedRow.Cells[9].Text;
            string ddlLoc = gvSearch.SelectedRow.Cells[10].Text;

            ListItem deptRemove = ddlDepartment.Items.FindByText(ddlDept);
            ddlDepartment.Items.Remove(deptRemove);
            ListItem locRemove = ddlLocation.Items.FindByText(ddlLoc);
            ddlLocation.Items.Remove(locRemove);

            txtEmpID.Text = gvSearch.SelectedRow.Cells[1].Text;
            txtFirstName.Text = gvSearch.SelectedRow.Cells[2].Text;
            txtLastName.Text = gvSearch.SelectedRow.Cells[3].Text;
            txtMgrID.Text = gvSearch.SelectedRow.Cells[4].Text;
            txtDOB.Text = gvSearch.SelectedRow.Cells[5].Text;
            txtEmail.Text = gvSearch.SelectedRow.Cells[6].Text;
            txtMobile.Text = gvSearch.SelectedRow.Cells[7].Text;
            txtSalary.Text = gvSearch.SelectedRow.Cells[8].Text;
            ddlDepartment.Items.Insert(0, new ListItem(ddlDept));
            ddlLocation.Items.Insert(0, new ListItem(ddlLoc));

        }
        catch
        { 

        }
    }

//下拉列表

 private void BindDDL()
    {
        try
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var location = from l in dc.Locations
                          select l;
                ddlLocation.DataSource = location;
                ddlLocation.DataValueField = "LocationID";
                ddlLocation.DataTextField = "LocationName";
                ddlLocation.DataBind();
                ddlLocation.Items.Insert(0, new ListItem("--Select Location--"));

                var department = from d in dc.Depts
                          select d;
                ddlDepartment.DataSource = department;
                ddlDepartment.DataValueField = "DeptID";
                ddlDepartment.DataTextField = "DeptName";
                ddlDepartment.DataBind();
                ddlDepartment.Items.Insert(0, new ListItem("--Select Department--"));
            }
        }

Database Error after changing SelectedIndex to SelectedValue

最佳答案

您已经使用了 SelectedIndex ,而是使用 SelectedValue :

employee.LocationID = int.Parse(ddlLocation.SelectedValue);

同样适用于部门:

employee.DeptID = int.Parse(ddlDepartment.SelectedValue);

您很幸运,没有与 selected-index 匹配的 ID,因此您得到了这个错误而不是不一致。外键约束有用的另一个原因。

关于c# - 我正在使用 linq 语句更新数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340687/

相关文章:

c# - 如何修复错误 "Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly"

C# - 有没有理由不实例化一个类并在一行代码中使用它?

c# - 使用证书调用服务:Error - "Keyset does not exist"

javascript - location.href 的回调函数

mysql - 添加了额外下划线的表名称

c# - 是否有一种标准方法可以将 .NET 字符串编码为 JavaScript 字符串以便在 MS Ajax 中使用?

c# - 为 Entity Framework 制作一个全局过滤器

mysql - 对于使用部分组合键的删除,InnoDB 是否会锁定整个表?

mysql - mysql的另一个区别和限制

c# - 使用 C# 中的代码隐藏按钮单击时创建 Javascript 警报