简体   繁体   中英

I want to edit my table in grid view

While I try to edit my grid view, I am getting the following error

Must declare the scalar variable "@s_id" or else while updating I am getting a null value please help me. If am wrong please correct me.

This is my aspx file

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");

        }



    }
}

The DataKeyNames property of the GridView is set to STD_ID , the update parameter for the ID value must have the same name :

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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