Как обновить мои данные в виде сетки
Заявление Update конфликтует с ограничением внешнего ключа "ФК__стандарты__С_код__4AB81AF0". Конфликт произошел в базе данных "SAMPLE_1", таблице " dbo.Школа", колонка "S_ID". Заявление было прекращено.
ПОЖАЛУЙСТА, ИСПРАВЬТЕ ЕГО, ЕСЛИ Я ОШИБАЮСЬ ,Я НОВИЧОК В РАБОТЕ, ТАК ЧТО Я МОГУ УЗНАТЬ ЕГО
МОЯ ЗАДАЧА ТАКОВА
ПРОСТО ХОЧУ ОБНОВИТЬ S_NAME В FRONTEND И ИДЕНТИФИКАТОР S_NAME ДОЛЖЕН БЫТЬ СОХРАНЕН В BACKEND ALL IN STANDARDS.ASPX
Что я уже пробовал:
ЭТИ АР МОЙ КОД
Школа.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:TextBox ID="TextBox1" runat="server"> <br /> <br /> <br /> <br /> <asp:Label ID="Label2" runat="server" Text="NO_OF_STANDARDS"> <asp:TextBox ID="TextBox2" runat="server"> <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:SqlDataSource ID="sql1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>" SelectCommand="SELECT S_ID,S_NAME,NO_OF_STANDARD FROM [SCHOOL]"> <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:TextBox ID="TextBox1" runat="server"> <br /> <br /> <br /> SCHOOL NAME <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="S_NAME" DataValueField="S_ID"> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>" SelectCommand="SELECT [S_ID], [S_NAME] FROM [SCHOOL]"> <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 HeaderText="class" DataField="class" SortExpression="class"> <asp:TemplateField HeaderText="S_NAME" SortExpression="S_ID"> <edititemtemplate> <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Sql3" DataTextField="S_NAME" DataValueField="S_ID"> </edititemtemplate> <itemtemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("S_NAME") %>'> </itemtemplate> </columns> <asp:SqlDataSource ID="sql2" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices_1 %>" SelectCommand="select STANDARDS.STD_ID,STANDARDS.CLASS,SCHOOL.S_NAME from SCHOOL left join STANDARDS 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="S_ID" /> </updateparameters> <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 [S_ID],[S_NAME] FROM [SCHOOL]" > <br /> <br /> <br /> <br /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="back" /> </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"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("HOME_1.aspx"); } } }
СТРУКТУРА ТАБЛИЦЫ SQL
CREATE TABLE SCHOOL( [S_ID] [int] IDENTITY (1000,1) NOT NULL PRIMARY KEY , [S_NAME] [varchar](255) NOT NULL, [NO_OF_STANDARD] [int] NOT NULL, ) CREATE TABLE STANDARDS( [STD_ID] [int] IDENTITY (100,1) NOT NULL PRIMARY KEY, [CLASS] [int]NOT NULL, [S_ID] [int] NOT NULL FOREIGN KEY REFERENCES SCHOOL(S_ID), )