无法隐式转换类型'对象'到' System.Data.SqlClient.SqlParameter'。存在显式转换(您是否错过了演员?)

时间:2014-09-29 04:54:22

标签: c# asp.net ajaxcontroltoolkit bar-chart

我正在尝试使用ajax控件工具包在我的网页上显示条形图。但是它显示了一个错误"无法隐式转换类型'对象'到' 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 Site_name=@site_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 != "";
    }
}

2 个答案:

答案 0 :(得分: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() };

答案 1 :(得分:1)

将其更改为此。

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