【问题标题】:ASP.NET C# error Must declare the scalar variableASP.NET C# 错误必须声明标量变量
【发布时间】:2017-08-06 07:49:38
【问题描述】:

我已经声明了,为什么会报错?

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

【问题讨论】:

  • 您的命令对象的名称是什么? cmdCountcmdSelect ?
  • 您应该查看Can we stop using AddWithValue() already? 并停止使用.AddWithValue() - 它可能会导致意想不到和令人惊讶的结果...
  • 没有用.AddWithValue()我们可以用什么方法解决?
  • 在那篇文章中清楚地解释了 - 请阅读!

标签: c# asp.net


【解决方案1】:

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

更改您添加参数的命令名称,它将起作用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2017-09-24
    相关资源
    最近更新 更多