Is it possible to pass datatable object to another control object as parameter as following :
protected void DropDownlist2(string model)
{
queryString = "Select modelcar from useritem where typecar=@model";
SqlCommand cmd = new SqlCommand(queryString, useritemConnectionString1);
cmd.Parameters.AddWithValue("@model", model);
useritemConnectionString1.Open();
DataTable dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList2.DataSource = dt;
DropDownList2.DataBind();
}
useritemConnectionString1.Close();
Button1(dt);
}
private void Button1_Click(DataTable dt)
{
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
while (dr.Read())
{
System.Web.UI.WebControls.Image imgControl = new System.Web.UI.WebControls.Image();
HtmlGenericControl paraContainer = new HtmlGenericControl("p");
paraContainer.Controls.Add(imgControl);
PlaceHolder1.Controls.Add(paraContainer);
//Get Image Information
imgControl.ImageUrl = dr["Image"].ToString();
}
}
else
dr.Close();
}
For some reason I get this error
button1_Click(System.data.datatable)is inacccessible due to its protection level
Make your
Button1_Clickmethodprotected.Check, is
Button1method called fromDropDownlist2the same asButton1_Click?Update
In your dropdownlist selection event handler you should store your
DataTableobject in some field or property of your form. In event handler which corresponds to the your button click you should operate with this field.