2012-03-06 115 views
5

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

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

     String sql = "SELECT QLEVELNAME FROM Qual_Levels WHERE [email protected]"; 
     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控件被宣布像这样..

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

尝试了无数的东西,通过我一直来面对同样的错误论坛拖网后。

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

如果任何人都可以摆脱这种情况,我会非常感激!

+0

感谢所有的答复,一般的共识似乎是我”在我的SQL参数中错过了@但尝试过了,并且抛出了相同的错误。此外,我拥有完全相同的语法,最初发布为我的下拉列表工作正常。 – PatrickJames 2012-03-06 14:48:14

回答

3

此:

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

应该是这样的:

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

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

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

OK,你的代码中有一个更基本的问题。你创建一个命令对象,然后你将SQL字符串和该命令的连接传递到你的dataadapter中,在那里它将执行你的sql字符串,而在它的连接上没有任何参数。

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

+0

感谢您的快速响应,但在更改上述内容后发生同样的错误。 – PatrickJames 2012-03-06 14:35:29

+0

再次出现同样的错误!我认为这些都是有效的选择,导致我认为它是在其他地方引起的。希望我知道在哪里! – PatrickJames 2012-03-06 14:59:58

1

尝试添加@到SQL PARAM像这样

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

你缺少了 “@”,您添加参数:

SqlParameter("@QUALID", val) 
1

变化

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

到要么

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

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

,你应该是好去。你的问题是,你缺少的放慢参数名称

1
String val = TextBox2.Text; 

String sql = "SELECT QLEVELNAME FROM Qual_Levels WHERE [email protected]"; 
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(); 

使用DIS一个将工作“@” ......(da.selectcommand = cmd;

+0

你可能想要添加为什么这个工作,而不是只粘贴一大块代码... – Sinkingpoint 2013-09-29 08:00:14

+0

这确实是一个可能的解决方案,我将necro这只是为了解释简而言之。 OP有一个SqlCommand对象,但从未使用它。更简单的解决方法是使用selectcommand对象而不是“sql”字符串来初始化适配器的“新SqlDataAdapter(cmd)”。 PS:使用三个字母的变量名称也不好,请尝试objCommand&strSql并加倍可读性(在我看来) – 2015-09-15 15:13:51