WebMatrix Razor - 必须声明标量变量“@ 0”

时间:2014-03-11 02:04:30

标签: asp.net-mvc razor

我现在已经搜索了两天,无法想出这个。我是使用Razor的新手。无论我尝试什么,我都会收到以下错误。基本上我试图从数据库中提取数据并渲染图表(工作)。然后我还想要一个下拉菜单,以便用户可以选择要查看的特定点并让它呈现新图表 这是最重要的代码:

    @{  var db = Database.Open("telecommConnectionString"); 
     var data = db.Query("select distinct employees.cost_center, costcenters.costcenterdesc from    dbo.invoices, dbo.employees, dbo.costcenters where invoices.emp_id = employees.emp_id and employees.supervisor = '11111111' and employees.cost_center = costcenters.costcenterid group by costcenterdesc,cost_center, bill_date");

var categories = data.Select(categorieslist =>new SelectListItem {
     Value = categorieslist.cost_center.ToString(),
     Text = categorieslist.costcenterdesc
 });
var filename="~MCCE2.jpg";
var cat = "";   
  if(!IsPost){

var dbdata = db.Query("select a.bill_date, a.costcenterdesc, a.total from (select invoices.bill_date, sum(invoices.total_current) as total, costcenters.costcenterdesc from dbo.invoices, dbo.employees, dbo.costcenters where invoices.emp_id = employees.emp_id and employees.supervisor = '11111111' and employees.cost_center = costcenters.costcenterid group by costcenterdesc, bill_date)a"); 

 var myChart = new Chart(width: 800, height: 500, theme: ChartTheme.Blue) 
.AddTitle("Count of Employees by Carrier") 
.AddLegend()
.DataBindCrossTable(dbdata, groupByField:"costcenterdesc", xField:"bill_date", yFields:"total")
.Save(filename);

 }

 if(IsPost){

     cat=Request.Form["costcenter"];
 var dbdata =  db.Query("select invoices.bill_date, sum(invoices.total_current) as total, costcenters.costcenterdesc from dbo.invoices, dbo.employees, dbo.costcenters where invoices.emp_id = employees.emp_id and employees.supervisor = '11111111' and employees.cost_center = costcenters.costcenterid and costcenters.costcenterid = @0 group by costcenterdesc, bill_date"); 

 var myChart = new Chart(width: 800, height: 500, theme: ChartTheme.Yellow) 
.AddTitle("Count of Employees by Carrier") 
.AddLegend()
.DataBindCrossTable(dbdata, groupByField:"costcenterdesc", xField:"bill_date", yFields:"total")
.Save(filename);
 }
 }

这是我的HTML来调用下拉列表和图表:

 <form method="post" action="MCCE2">
        Select Cost Center:
         @Html.DropDownList("costcenter", "--Choose Your Cost Center--", categories )
        <input type="submit" value="Submit">
    </form>

      <p>     
    </br>
    <img src="@Href(filename)" alt="@Href(filename)"/>

无论我尝试什么,这都是我得到的错误:

异常详细信息:

System.Data.SqlClient.SqlException: Must declare the scalar variable "@0".

Line 32:  var dbdata =  db.Query("select invoices.bill_date, sum(invoices.total_current) as total, costcenters.costcenterdesc from dbo.invoices, dbo.employees, dbo.costcenters where invoices.emp_id = employees.emp_id and employees.supervisor = '11111111' and employees.cost_center = costcenters.costcenterid and costcenters.costcenterid = @0 group by costcenterdesc, bill_date"); 

任何帮助都会很棒。感谢

1 个答案:

答案 0 :(得分:1)

这是因为你有SQL的WHERE子句:

costcenters.costcenterid = @0

你的意思是,或者你的意思是传入零,或者实际上你是说这是一个你设置的参数(但可能已经忘记了)?

我认为您想要传递所选类别:

cat=Request.Form["costcenter"];

你还没有做过。代码需要更改为:

var dbdata =  db.Query("select invoices.bill_date, sum(invoices.total_current) as total, costcenters.costcenterdesc from dbo.invoices, dbo.employees, dbo.costcenters where invoices.emp_id = employees.emp_id and employees.supervisor = '11111111' and employees.cost_center = costcenters.costcenterid and costcenters.costcenterid = @0 group by costcenterdesc, bill_date", cat);