Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 插入时System.Data.SqlClient.SqlException生成与外键的冲突_C#_Asp.net_Sql Server_Sqlexception - Fatal编程技术网

C# 插入时System.Data.SqlClient.SqlException生成与外键的冲突

C# 插入时System.Data.SqlClient.SqlException生成与外键的冲突,c#,asp.net,sql-server,sqlexception,C#,Asp.net,Sql Server,Sqlexception,我有这张桌子 当我尝试插入新的货物时,出现一个异常 INSERT语句与外键约束“FK_Cargo_Rol”冲突。冲突出现在数据库“Banco”表“dbo.Rol”列“idRol”中。 声明已终止。 这是我的代码ASPX.CS public partial class Public_Cargo_CrearCargo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPos

我有这张桌子

当我尝试插入新的
货物时,出现一个异常

INSERT语句与外键约束“FK_Cargo_Rol”冲突。冲突出现在数据库“Banco”表“dbo.Rol”列“idRol”中。 声明已终止。

这是我的代码ASPX.CS

public partial class Public_Cargo_CrearCargo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
        string query1 = @"SELECT * FROM Formula";
        string query2 = @"SELECT * FROM Rol";

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Banco"].ToString()))
        {
            SqlCommand comand = new SqlCommand(query1, con);
            SqlCommand comand2 = new SqlCommand(query2, con);

            DataSet ds = new DataSet();
            DataSet ds2 = new DataSet();

            con.Open();
            var adapter = new SqlDataAdapter(comand);
            var adapter2 = new SqlDataAdapter(comand2);

            adapter.Fill(ds, "Formula");
            adapter2.Fill(ds2, "Rol");

            Select1.DataSource = ds;
            Select1.DataTextField = "Formula";
            Select1.DataValueField = "idFormula";
            Select1.DataBind();

            Select2.DataSource = ds2;
            Select2.DataTextField = "Nombre_Rol";
            Select2.DataValueField = "idRol";
            Select2.DataBind();


        }
    }
}

protected void docreatecargo(object sender, EventArgs e)
{
    string query = @"INSERT INTO Cargo (Nombre, idFormula, idRol) VALUES (@nombre, @idFormula, @idRol)";

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Banco"].ToString()))
    {
        SqlCommand comand = new SqlCommand(query, con);
        comand.Parameters.AddWithValue("@idFormula", Select1.SelectedIndex.ToString());
        comand.Parameters.AddWithValue("@idRol", Select2.SelectedIndex.ToString());
        comand.Parameters.AddWithValue("@nombre", cargo.Text);
        con.Open();
        Console.Write("idformula :" + Select1.SelectedIndex.ToString() + "," + "id Rol :" + Select2.SelectedIndex.ToString() + "," + "nombre :" + cargo.Text);
        comand.ExecuteScalar();

        Response.Redirect("~/Public/Cargo/Cargos.aspx");

    }
}
}

这是我的网页

<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div class="container">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Cargo</label>
                <asp:TextBox runat="server" ID="cargo" CssClass="form-control" required="true"></asp:TextBox>
            </div>
            <div class="form-group">
                <label>Formula asociada</label>
                 <select id="Select1" runat="server" name="D1"> </select>
            </div>
            <div class="form-group">
                <label>Rol asociada</label>
                 <select id="Select2" runat="server" name="D2"> </select>
            </div>
            <div class="form-group">
                <asp:Button ID="btn_login" OnClick="docreatecargo" CssClass="btn btn-primary btn-lg btn-block" Text="Guardar" runat="server"/>
            </div>

        </div>
    </div>
</div>

货物
asociada公式
罗尔阿索西亚


如何修复它???

由于错误状态,如果主键表中不存在FK表中的值,则FK表中不能有值。

对于以下代码行:

comand.Parameters.AddWithValue("@idFormula", Select1.SelectedIndex.ToString());
comand.Parameters.AddWithValue("@idRol", Select2.SelectedIndex.ToString());
[controlId]。SelectedIndex返回的是绑定源中选定项的索引,而不是数据库中的相应id

comand.Parameters.AddWithValue("@idFormula", Select1.Value.ToString());
comand.Parameters.AddWithValue("@idRol", Select2.Value.ToString());

您正试图在
货物表
中插入
Rol表
中不存在的值。如果在引用表中应该存在的
外键列中插入一个值,您可能会尝试在Cargo表的idRol列中插入Rol表的idRol列中不存在的值。@Pradeep奇怪的是,“select tag”是通过查询数据库中的所有´´´Rol´来完成的。'Rol asociada管理员'