none
alert('必须声明变量 '@ClassID'。') RRS feed

  • 问题

  • 前台代码为:
    <table class="style12">
                <tr>
                    <td class="style13">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 学院:&nbsp;</td>
                    <td class="style13">
                        &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                            DataSourceID="SqlDataSource1" DataTextField="SchoolName"
                            DataValueField="SchoolID" Height="22px" Width="145px">
                        </asp:DropDownList>
                        &nbsp;</td>
                    <td class="style14">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 系别:&nbsp;</td>
                    <td>
                        &nbsp;<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
                            DataSourceID="SqlDataSource2" DataTextField="DepartName"
                            DataValueField="DepartID" Height="22px" Width="94px">
                        </asp:DropDownList>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style13">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 专业:&nbsp;</td>
                    <td class="style13">
                        &nbsp;<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
                            DataSourceID="SqlDataSource3" DataTextField="SpecialtyName"
                            DataValueField="SpecialtyID" Height="22px" Width="94px">
                        </asp:DropDownList>
                        &nbsp;</td>
                    <td class="style14">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 年级:&nbsp;</td>
                    <td>
                        &nbsp;<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True"
                            DataSourceID="SqlDataSource4" DataTextField="GradeName"
                            DataValueField="GradeID" Height="22px" Width="94px">
                        </asp:DropDownList>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style13">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 班级名称:&nbsp;</td>
                    <td class="style13">
                        &nbsp;<asp:TextBox ID="tbxClassName" runat="server" Width="130px"></asp:TextBox>
                        &nbsp;</td>
                    <td class="style14">
    &nbsp;&nbsp; 班级编号:&nbsp;</td>
                    <td>
                        &nbsp;<asp:TextBox ID="tbxClassID" runat="server" Width="94px"></asp:TextBox>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style13" colspan="4">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Label ID="lblmessage" runat="server" Text="Label"></asp:Label>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style13">
                        &nbsp;</td>
                    <td class="style13">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="增加" />
                        &nbsp;</td>
                    <td class="style14">
                        &nbsp;&nbsp;
                        <asp:Button ID="btnReset" runat="server" Text="重置" />
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
            </table>
    后台代码如下:
    protected void btnAdd_Click(object sender, EventArgs e)
        {
            string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            string SqlStrInsert = @"insert into ClassTable(ClassID,ClassName,SpecialtyID,GradeID)
                                  values(@ClassID,@ClassName,@SpecialtyID,@GradeID)";
            string SqlSelect = "select * from ClassTable where ClassID=@ClassID";
            SqlConnection conn = new SqlConnection(connStr);                                         //创建连接对象
            SqlCommand cmd = new SqlCommand(SqlSelect, conn);                                        //查询学号是否存在
            SqlCommand cmdInsert = new SqlCommand(SqlStrInsert, conn);
            cmdInsert.Parameters.Add("@ClassID", SqlDbType.VarChar, 50);
            cmdInsert.Parameters.Add("@ClassName", SqlDbType.VarChar, 50);
            cmdInsert.Parameters.Add("@SpecialtyID", SqlDbType.VarChar, 50);
            cmdInsert.Parameters.Add("@GradeID", SqlDbType.VarChar, 50);
            cmdInsert.Parameters["@ClassID"].Value = this.tbxClassID.Text;
            cmdInsert.Parameters["@ClassName"].Value = this.tbxClassName.Text;
            cmdInsert.Parameters["@SpecialtyID"].Value = this.DropDownList3.SelectedValue;
            cmdInsert.Parameters["@GradeID"].Value = this.DropDownList4.SelectedValue;
            try
            {
                conn.Open();                                                                         //打开连接
                SqlDataReader sdr = cmd.ExecuteReader();                                             //执行查询
                if (sdr.Read())                                                                      //如果该班级已经存在
                {
                    lblmessage.Text = "您输入班级编号已经存在,请输入其他班级编号";
                    sdr.Close();
                }
                else
                {
                    sdr.Close();
                    try
                    {
                        if (conn.State.ToString() == "Closed")                                        //如果连接关闭,打开连接
                            conn.Open();
                        cmdInsert.ExecuteNonQuery();                                                  //执行查询
                    }
                    catch (Exception ex)                                                              //异常处理
                    {
                        Response.Write("数据库错误,错误原因:" + ex.Message);
                        Response.End();
                    }
                    lblmessage.Text = "数据增加成功!";
                }
            }
            catch (Exception ee)
            {
                Response.Write("<sript language=javascript>alert('" + ee.Message.ToString() + "')</script>");
            }
            finally
            {
                cmd.Dispose();
                cmdInsert.Dispose();
                if (conn.State.ToString() == "Open")                                                     //如果连接打开,关闭连接
                    conn.Close();
            }
        }

    在调试的过程中,生成成功,但是出现了必须声明变量 '@ClassID'的提示,我想知道,错在哪里,麻烦这位前辈了!
    2010年2月3日 15:21

答案

  •       string SqlSelect = "select * from ClassTable where ClassID=@ClassID";
            SqlConnection conn = new SqlConnection(connStr);                                         //创建连接对象
            SqlCommand cmd = new SqlCommand(SqlSelect, conn);   
    这个sqlcommand对象没有声明参数对象添加
    2010年2月3日 17:18

全部回复

  •       string SqlSelect = "select * from ClassTable where ClassID=@ClassID";
            SqlConnection conn = new SqlConnection(connStr);                                         //创建连接对象
            SqlCommand cmd = new SqlCommand(SqlSelect, conn);   
    这个sqlcommand对象没有声明参数对象添加
    2010年2月3日 17:18
  •       string SqlSelect = "select * from ClassTable where ClassID=@ClassID";
            SqlConnection conn = new SqlConnection(connStr);                                         //创建连接对象
            SqlCommand cmd = new SqlCommand(SqlSelect, conn);   
    这个sqlcommand对象没有声明参数对象添加

    恩,是的,谢谢了!
    2010年2月4日 4:20