UPDATE语句与FOREIGN KEY约束冲突

时间:2016-06-20 11:22:05

标签: c# mysql asp.net sql-server linq

UPDATE语句与FOREIGN KEY约束冲突" FK__STANDARDS__S_ID__38996AB5"。冲突发生在数据库" SAMPLE_1",表" dbo.SCHOOL",列' S_ID'中。 该声明已被终止。这是我的错误PLS帮助我

SCHOOL.ASPX

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SCHOOL_1.aspx.cs" Inherits="WebApplication1.SCHOOL_1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="SCHOOL NAME"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="NO_OF_STANDARDS"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="SUBMIT" />
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" Height="190px" 
             Width="266px" 
            DataSourceID="sql1">
        </asp:GridView>
        <asp:SqlDataSource ID="sql1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>"
        SelectCommand="SELECT S_ID,S_NAME,NO_OF_STANDARD FROM [SCHOOL]">
        </asp:SqlDataSource>
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Back" />
        <br />
        <br />


    </div>
    </form>
</body>
</html>

SCHOOL.ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class SCHOOL_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=CSI60-PC\SQLEXPRESS;Initial Catalog=SAMPLE_1;Integrated Security=True");


            SqlCommand cmd = new SqlCommand("insert into school values( '" + TextBox1.Text + "' ,'" + TextBox2.Text + "')", con);

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, "school");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("HOME_1.aspx");
        }
    }
}

STANDARDS.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="STANDARDS_1.aspx.cs" Inherits="WebApplication1.STANDARDS_1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="CLASS"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        SCHOOL NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList1" runat="server" 
              DataSourceID="SqlDataSource1" DataTextField="S_NAME" DataValueField="S_ID"> 

        </asp:DropDownList>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>" 
            SelectCommand="SELECT [S_ID], [S_NAME] FROM [SCHOOL]">

        </asp:SqlDataSource>


        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" Height="214px" Width="255px" DataSourceID="sql2" 
         AutoGenerateColumns="false" AutoGenerateEditButton="true"
         AllowSorting="True" AllowPaging="True" DataKeyNames="STD_ID" >

         <Columns>
         <asp:BoundField ReadOnly="True" HeaderText="std_id"
        DataField="std_id" SortExpression="std_id"></asp:BoundField>
        <asp:BoundField HeaderText="class" DataField="class"
        SortExpression="class"></asp:BoundField>
        <asp:TemplateField HeaderText="S_NAME" SortExpression="S_NAME">
        <EditItemTemplate>
        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Sql3" DataTextField="S_NAME" DataValueField="S_ID">
        </asp:DropDownList>
        </EditItemTemplate>
     <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("S_NAME") %>'></asp:Label>
    </ItemTemplate>   
        </asp:TemplateField>   
         </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="sql2" runat="server"
        ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>"
        SelectCommand="select STANDARDS.STD_ID,STANDARDS.CLASS,SCHOOL.S_NAME
                           from STANDARDS
                           left join SCHOOL
                           on STANDARDS.S_ID=SCHOOL.S_ID"

       UpdateCommand="update [STANDARDS] set [CLASS]=@CLASS,[S_ID]=@STD_ID where [STD_ID]=@STD_ID">    
       <UpdateParameters>
      <asp:Parameter Type="Int16" Name="CLASS" />
      <asp:Parameter Type="Int16" Name="STD_ID" />


       </UpdateParameters>  
        </asp:SqlDataSource>
        <br />
        <br />
        <br />
        <br />

        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="SUBMIT" />

    </div>
    <asp:SqlDataSource ID="Sql3" runat="server"
    ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>"
    SelectCommand="SELECT DISTINCT [S_NAME] FROM [SCHOOL]">
    </asp:SqlDataSource>
    </form>
</body>
</html>

STANDARDS.ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class STANDARDS_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=CSI60-PC\SQLEXPRESS;Initial Catalog=SAMPLE_1;Integrated Security=True");


            SqlCommand cmd = new SqlCommand("insert into STANDARDS values( '" + TextBox1.Text + "' ,'" + DropDownList1.SelectedValue + "')", con);

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, "STANDARDS");

        }
    }
}

** SQL表结构** SCHOOL TABLE

*CREATE TABLE SCHOOL(
[S_ID] [int] IDENTITY (1000,1)  NOT NULL PRIMARY KEY ,
[S_NAME] [varchar](255) NULL,
[NO_OF_STANDARD] [int] NULL,
)*

标准表

*CREATE TABLE STANDARDS(
[STD_ID] [int] IDENTITY (100,1) NOT NULL PRIMARY KEY,
[CLASS] [int] NULL,
[S_ID] [int] NULL FOREIGN KEY REFERENCES SCHOOL(S_ID),
)*

2 个答案:

答案 0 :(得分:0)

外键错误意味着您尝试插入引用表中不存在的值。

您的代码中确实存在错误:

DataTextField="S_NAME" DataValueField="S_NAME"

应该是

DataTextField="S_NAME" DataValueField="S_ID"

在aspx文件中

答案 1 :(得分:0)

您有拼写错误。

在您的代码中替换

[S_ID]=@STD_ID

通过

[STD_ID]=@STD_ID

在您的更新命令中。