我想使用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 != "";
}
}