2014-09-29 70 views
1

我想使用ajax控制工具包在我的网页上显示条形图。但它显示一个错误“不能隐式地将类型'对象'转换为'System.Data.SqlClient.SqlParameter'。存在明确的转换(你是否缺少一个转换?)”请帮助我。下面的代码给出:不能将类型'object'隐式转换为'System.Data.SqlClient.SqlParameter'。存在明确的转换(您是否缺少演员?)

代码:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      string query = "select Site_name from tbl_runtime_report"; 
      DataTable dt = GetData(query); 
      ddlCountries.DataSource = dt; 
      ddlCountries.DataTextField = "Site_name"; 
      ddlCountries.DataValueField = "Site_name"; 
      ddlCountries.DataBind(); 
      ddlCountries.Items.Insert(0, new ListItem("Select", "")); 
     } 

    } 
    private DataTable GetData(string query, SqlParameter[] prms = null) 
    { 
     DataTable dt = new DataTable(); 
     string constr = ConfigurationManager.ConnectionStrings["Gems1ConnectionString2"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(constr)) 
     { 
      using (SqlCommand cmd = new SqlCommand(query)) 
      { 
       if (prms != null) 
        cmd.Parameters.AddRange(prms); 
       using (SqlDataAdapter sda = new SqlDataAdapter()) 
       { 
        cmd.CommandType = CommandType.Text; 
        cmd.Connection = con; 
        sda.SelectCommand = cmd; 
        sda.Fill(dt); 
       } 
      } 
      return dt; 
     } 
    } 

    protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e) 
    { 
string query = "select Distinct Site_name, Battery_Run_Hrs From tbl_runtime_report where [email protected]_name"; 
     SqlParameter[] prms = new SqlParameter[1]; 
     prms[0] = new SqlParameter("@site_name", SqlDbType.NVarChar).Value = ddlCountries.SelectedItem.Value.ToString(); //Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlParameter'. An explicit conversion exists (are you missing a cast?) 
     DataTable dt=GetData(query,prms); 

     string[] x = new string[dt.Rows.Count]; 
     decimal[] y = new decimal[dt.Rows.Count]; 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      x[i] = dt.Rows[i][0].ToString(); 
      y[i] = Convert.ToInt32(dt.Rows[i][1]); 
     } 
     BarChart1.Series.Add(new AjaxControlToolkit.BarChartSeries { Data = y }); 
     BarChart1.CategoriesAxis = string.Join(",", x); 
     BarChart1.ChartTitle = string.Format("{0} Order Distribution", ddlCountries.SelectedItem.Value); 
     if (x.Length > 3) 
     { 
      BarChart1.ChartWidth = (x.Length * 100).ToString(); 
     } 
     BarChart1.Visible = ddlCountries.SelectedItem.Value != ""; 
    } 
} 

回答

1

它改变了这一点。

prms[0] = new SqlParameter("@site_name", SqlDbType.NVarChar); 
prms[0].Value = ddlCountries.SelectedItem.Value.ToString(); 
1
prms[0] = new SqlParameter(...).Value = x; 

在该声明中,new SqlParameter(...).Value = x;解析为x,而不是参数本身。你将不得不打破这一成两个单独的语句,如:

prms[0] = new SqlParameter("@site_name", SqlDbType.NVarChar); 
prms[0].Value = ddlCountries.SelectedItem.Value.ToString(); 

或者使用初始化语法:

prms[0] = new SqlParameter("@site_name", SqlDbType.NVarChar) { Value = ddlCountries.SelectedItem.Value.ToString() }; 
相关问题