ASP.NET C#必须声明标量变量 [英] ASP.NET C# Must declare the scalar variable

查看:359
本文介绍了ASP.NET C#必须声明标量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来填充使用一种叫做PopulateGrid(法)的GridView(下同),但继续得到同样的服务器错误必须声明标量变量@QUALID。

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

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();
    }

GridView控件在声明像这样..

The GridView is being declared like so..

<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.

服务器错误/应用程序。

必须声明标量变量@QUALID。

Must declare the scalar variable "@QUALID".

说明:在执行过程中发生未处理的异常
  当前Web请求。有关详情,请堆栈跟踪
  有关错误的信息以及它起源于code。

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.

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

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

源错误:

282线:DS的DataSet =新的DataSet();

Line 282: DataSet ds = new DataSet();

283线:da.Fill(DS,Qual_Levels);

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

如果任何人都可以对局势有何启示,我将非常感激!

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

推荐答案

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

应该是这样的:

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


对不起,打字太快,请尝试:


Sorry, typed too quick, try:

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


OK,你有你的code稍微更根本性的问题。您创建一个命令对象,但你将SQL字符串传递和你的DataAdapter的,在那里将不带任何参数上它是连接执行SQL字符串命令的连接。


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.

我没有用dataadapters太多,但我认为你需要设置你的适配器的选择命令的参数。

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

这篇关于ASP.NET C#必须声明标量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆