必须声明标量变量n#39; @ site_name'?

时间:2014-10-01 09:59:55

标签: c# sql

我必须在我的网页上绑定并显示asp.net图表。运行此程序时出错。请帮帮我。代码如下:

public partial class chartDummy : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"connectionString");
        cmd = new SqlCommand("select top (1)  Dg_energy_daily, Load_energy_daily, mains_energy_daily,solar_energy_daily  From tbl_energy_report inner join tbl_site_details ON tbl_energy_report.Site_ID=tbl_site_details.site_id where tbl_site_details.site_name=@site_name order by tbl_energy_report.sl_no desc", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        DataView source = new DataView(ds.Tables[0]);
        Chart1.DataSource = ds;
        Chart1.DataBind();

源:

<asp:Chart ID="Chart1" runat="server" CssClass="auto-style1" Width="987px" Height="276px">
<Series>
<asp:Series Name="Dg_energy_daily" XValueMember="site_name" YValueMembers="Dg_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
<asp:Series Name="Load_energy_daily" XValueMember="site_name" YValueMembers="Load_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
<asp:Series Name="mains_energy_daily" XValueMember="site_name" YValueMembers="mains_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
<asp:Series Name="solar_energy_daily" XValueMember="site_name" YValueMembers="solar_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Title="RunTime Report" Name="Legend1"></asp:Legend>
</Legends> 
</asp:Chart>

1 个答案:

答案 0 :(得分:3)

基本上,您必须将参数及其相应的值添加到查询中:

string value=///set the value you want
...
 cmd = new SqlCommand("select top (1)  Dg_energy_daily, Load_energy_daily, mains_energy_daily,solar_energy_daily  From tbl_energy_report inner join tbl_site_details ON tbl_energy_report.Site_ID=tbl_site_details.site_id where tbl_site_details.site_name=@site_name order by tbl_energy_report.sl_no desc", con);         
 cmd.Paramaters.AddWithValue("@site_name ", value);