2017-08-06 90 views
0

我已经声明了,为什么我会得到一个错误?ASP.NET C#错误必须声明标量变量

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(); 
+2

什么是你的命令对象的名称? 'cmdCount'或'cmdSelect'? – SearchAndResQ

+0

你应该看看[我们可以停止使用AddWithValue()了吗?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)并停止使用'.AddWithValue()' - 它可能会导致意外和令人惊讶的结果... –

+0

没有用.AddWithValue()我们可以使用什么方法来解决? –

回答

1

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

改变命令名字您添加的参数,它会工作:

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