ASP.NET C# 必须声明标量变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9584217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:00:15  来源:igfitidea点击:

ASP.NET C# Must declare the scalar variable

c#asp.net

提问by PatrickJames

I am trying to populate a GridView using a method called PopulateGrid() (below) but keep getting the same server error "Must Declare the scalar variable "@QUALID".

我正在尝试使用名为 PopulateGrid()(如下)的方法填充 GridView,但不断收到相同的服务器错误“必须声明标量变量“@QUALID”。

public void PopulateGrid()
    {
        String val = TextBox2.Text;

        String sql = "SELECT QLEVELNAME FROM Qual_Levels WHERE QUALID=@QUALID";
        SqlCommand cmd = new SqlCommand(sql,
           new SqlConnection(ConfigurationManager.ConnectionStrings["RecruitmentDBConnString"].ConnectionString));

        cmd.Parameters.Add(new SqlParameter("QUALID", val));

        cmd.Connection.Open();


        SqlDataAdapter da = new SqlDataAdapter(sql, cmd.Connection);

        DataSet ds = new DataSet();
        da.Fill(ds, "Qual_Levels");


        SelectionGrid.DataSource = ds;
        SelectionGrid.DataBind();


        ds.Dispose();
        da.Dispose();
        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }

The GridView is being declared like so..

GridView 是这样声明的..

<asp:GridView ID="SelectionGrid"
            autogeneratecolumns="False" 
            runat="server" CellPadding="4" 
            ForeColor="#333333" GridLines="None" DataKeyNames="QUALID">

            <Columns>
                <asp:BoundField DataField="QLEVELNAME" HeaderText="Level Name" 
                    ReadOnly="True" SortExpression="name" />
            </Columns>
</asp:GridView>

After trying countless things and trawling through the forums I keep coming up against the same error.

在尝试了无数的事情并浏览了论坛之后,我不断遇到同样的错误。

Server Error in '/' Application.

Must declare the scalar variable "@QUALID".

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@QUALID".

Source Error:

Line 282: DataSet ds = new DataSet();

Line 283: da.Fill(ds, "Qual_Levels");

“/”应用程序中的服务器错误。

必须声明标量变量“@QUALID”。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:必须声明标量变量“@QUALID”。

源错误:

第 282 行:DataSet ds = new DataSet();

第 283 行:da.Fill(ds, "Qual_Levels");

If anyone can shed any light on the situation I would be really grateful!

如果有人能对这种情况有所了解,我将不胜感激!

回答by Paddy

This:

这个:

cmd.Parameters.Add(new SqlParameter("QUALID", val));

should be this:

应该是这样的:

cmd.Parameters.Add(new SqlParameter("@QUALID", val));


Sorry, typed too quick, try:

抱歉,输入太快,请尝试:

cmd.Parameters.AddWithValue("@QUALID", val);


OK, you have a slightly more fundamental issue in your code. You create a command object, but then you pass the SQL string and the connection for the command into your dataadapter, where it will execute your sql string with no parameters on it's connection.

好的,您的代码中有一个更基本的问题。您创建了一个命令对象,然后您将 SQL 字符串和命令的连接传递到您的数据适配器中,在那里它将执行您的 sql 字符串,而它的连接上没有参数。

I haven't used dataadapters too much, but I think you need to set the parameters on the select command of your adapter.

我没有过多使用dataadapters,但我认为您需要在适配器的select命令上设置参数。

回答by Brian Dishaw

Try adding the @ to your sql param like so

尝试将 @ 添加到您的 sql 参数中

 cmd.Parameters.Add(new SqlParameter("@QUALID", val));

回答by MCSI

You are missing the "@" where you add the parameter:

您缺少添加参数的“@”:

SqlParameter("@QUALID", val)

回答by Rune FS

change

改变

cmd.Parameters.Add(new SqlParameter("QUALID", val));

to either

要么

cmd.Parameters.Add(new SqlParameter("@QUALID", val));

or

或者

cmd.Parameters.Add("@QUALID", SqlDbType.WhatFitsYourDB).Value = val; 

and you should be good to go. Your problem is that you are missing a '@' in the paramter name

你应该很高兴去。您的问题是参数名称中缺少“@”

回答by Tom

String val = TextBox2.Text;

String sql = "SELECT QLEVELNAME FROM Qual_Levels WHERE QUALID=@QUALID";
SqlCommand cmd = new SqlCommand(sql, new SqlConnection(ConfigurationManager.ConnectionStrings["RecruitmentDBConnString"].ConnectionString));
SqlDataAdapter da = new SqlDataAdapter(sql, cmd.Connection);
DataSet ds = new DataSet();
cmd.Parameters.Add(new SqlParameter("@QUALID", val));

da.SelectCommand = cmd;
cmd.Connection.Open();

da.Fill(ds, "Qual_Levels");


SelectionGrid.DataSource = ds;
SelectionGrid.DataBind();

ds.Dispose();
da.Dispose();
cmd.Connection.Close();
cmd.Connection.Dispose();

use dis one it will work...(da.selectcommand = cmd;)

使用 dis one 它会起作用...( da.selectcommand = cmd;)

回答by Crusoe

If using dropdown list you need to add selected value inside :

如果使用下拉列表,您需要在里面添加选定的值:

SelectedValue='<%# Bind ("QUALID") %>'