简体   繁体   English

UPDATE语句与FOREIGN KEY约束冲突

[英]The UPDATE statement conflicted with the FOREIGN KEY constraint

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__STANDARDS__S_ID__38996AB5". UPDATE语句与FOREIGN KEY约束“ FK__STANDARDS__S_ID__38996AB5”冲突。 The conflict occurred in database "SAMPLE_1", table "dbo.SCHOOL", column 'S_ID'. 数据库“ SAMPLE_1”的表“ dbo.SCHOOL”的列“ S_ID”中发生了冲突。 The statement has been terminated. 该语句已终止。 THIS IS MY ERROR PLS HELP ME 这是我的错误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 TABLE STRUCTURE** SCHOOL TABLE ** SQL表结构** 学校表

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

STANDARDS TABLE 标准表

*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),
)*

Foreign key error means you're trying to insert a value that is not present in the referenced table. 外键错误表示您正在尝试插入引用表中不存在的值。

There's actually an error in your code: 您的代码中实际上存在错误:

DataTextField="S_NAME" DataValueField="S_NAME"

should be 应该

DataTextField="S_NAME" DataValueField="S_ID"

In the aspx-file 在aspx文件中

You have a typo error. 您输入错误。

In your code replace 在您的代码中替换

[S_ID]=@STD_ID

By 通过

[STD_ID]=@STD_ID

In your update command. 在您的更新命令中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM