【问题标题】:I want to edit my table in grid view我想在网格视图中编辑我的表格
【发布时间】:2016-10-21 08:26:55
【问题描述】:

当我尝试编辑我的网格视图时,我收到以下错误

必须声明标量变量 "@s_id" 否则在更新时我得到一个空值,请帮助我。 如有错误请指正。

这是我的 aspx 文件

STANDARD.ASPX

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

<!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 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 %>" 
            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_NAME">
        </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 %>"
        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]=@s_id where [std_id]=@std_id">    
       <UpdateParameters>
      <asp:Parameter Type="Int16" Name="class" />
      <asp:Parameter Type="int16" Name="s_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 %>"
    SelectCommand="SELECT DISTINCT [S_NAME] FROM [SCHOOL]">
    </asp:SqlDataSource>
    </form>
</body>
</html>



***THIS IS MY STANDARD.ASPX.CS PROGRAM***
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 Rough1
{
    public partial class STANDARDS : 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=MANAGEMENT;
                                                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");

        }



    }
}

【问题讨论】:

  • 请分享您的表结构
  • @mehman bashirov 嗨,这是我的学校和标准​​表结构 CREATE TABLE [dbo].[SCHOOL]( [S_ID] [int] IDENTITY (1000,1) NOT NULL, [ S_NAME] [varchar](255) NULL, [NO_OF_STANDARD] [int] NULL,)
  • @mehman bashirov 标准 CREATE TABLE [dbo].[STANDARDS]( [STD_ID] [int] IDENTITY (100,1) NOT NULL, [CLASS] [int] NULL, [ S_ID] [int] NULL,
  • @mehman bashirov 注意:在 school 表中,S_ID 是主键,而在 standards 表中,STD_ID 是主键,S_ID 是外键

标签: c# mysql asp.net linq optimization


【解决方案1】:

GridView的DataKeyNames属性设置为STD_ID,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" />

【讨论】:

  • 感谢您的回复,但我仍然收到以下错误 **UPDATE 语句与 FOREIGN KEY 约束“FK__STANDARDS__S_ID__182C9B23”冲突。冲突发生在数据库“MANAGEMENT”、表“dbo.SCHOOL”、列“S_ID”中。该语句已终止。 **
  • 这不是同一个错误!这意味着更新语句现在正在工作,但您有外键违规
  • 你是对的,对于这个 外键违规你能帮我做什么
猜你喜欢
  • 2012-08-23
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多