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

C# ASP.NET C错误必须声明标量变量,c#,asp.net,C#,Asp.net,我已经声明了,为什么会出现错误 conn.Open(); String strCount = "Select SUM(freight) AS TOTAL from Orders where EmployeeID = @employee AND Year(OrderDate)= @Oyear"; SqlCommand cmdCount = new SqlCommand(strCount, conn); cmdSelect.Parameters.AddWithValue("@employee",

我已经声明了,为什么会出现错误

conn.Open();
String strCount = "Select SUM(freight) AS TOTAL from Orders where EmployeeID = @employee AND Year(OrderDate)= @Oyear";
SqlCommand cmdCount = new SqlCommand(strCount, conn);

cmdSelect.Parameters.AddWithValue("@employee", DropDownList1.SelectedValue.ToString());
cmdSelect.Parameters.AddWithValue("@Oyear", RadioButtonList1.SelectedValue.ToString());

double intCount = (double)cmdCount.ExecuteScalar();
Label1.Text = intCount.ToString();
conn.Close();

您将参数添加到错误的命令中

更改将参数添加到的命令名,该命令将起作用:

cmdCount.Parameters.AddWithValue("@employee", DropDownList1.SelectedValue.ToString());
cmdCount.Parameters.AddWithValue("@Oyear", RadioButtonList1.SelectedValue.ToString());

您的命令对象的名称是什么?cmdCount或cmdSelect?您应该签出并停止使用.AddWithValue-它可能会导致意外和令人惊讶的结果…没有用。AddWithValue我们可以使用什么方法来解决?在那篇文章中有明确的解释-阅读它!可能重复的