我試圖填充使用一種叫做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");
如果任何人都可以擺脫這種情況,我會非常感激!
感謝所有的答覆,一般的共識似乎是我」在我的SQL參數中錯過了@但嘗試過了,並且拋出了相同的錯誤。此外,我擁有完全相同的語法,最初發布爲我的下拉列表工作正常。 – PatrickJames 2012-03-06 14:48:14