Search code examples
c#asp.netajaxcontroltoolkitbar-chart

Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlParameter'. An explicit conversion exists (are you missing a cast?)


I am trying to display bar chart on my web page using ajax control tool kit. But it shows one errors "Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlParameter'. An explicit conversion exists (are you missing a cast?)" Please help me. The code is given below:

code:

    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 != "";
    }
}

Solution

  • Change it to this.

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