当我尝试编辑网格视图时,我收到以下错误
必须声明标量变量" @ 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>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<br />
SCHOOL NAME
<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");
}
}
}
答案 0 :(得分:0)
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" />