ddlcategory.SelectedIndex.ToString()
give you the selected item index like if you select 2nd item then your
SelectedIndex
is
1
. is that you expected as
Category
? if not you need to change it to
ddlcategory.SelectedValue
or
ddlcategory.SelectedItem.Text
to fire the index changed event you need to have both AutoPostBack property and
OnSelectedIndexChanged
event set correctly as below
<asp:DropDownList id="ddlcategory"
AutoPostBack="True"
OnSelectedIndexChanged="ddlcategory_SelectedIndexChanged"
runat="server"/>
there are few areas you can improve your code/application. If all other data in database you better move your categories to your database and bind the dropdownlist using that table. for example. create table called Category and you can have id and Name field then.
using(SqlConnection con = new SqlConnectio(conString))
using(SqlCommand cmd = new SqlCommand("Select Id, Name from Category", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
ddlcategory.DataSource = dt;
ddlcategory.DataTextField = "Id";
ddlcategory.DataValueField = "Name";
ddlcategory.DataBind();
}
And your selected index changed event can be change as below with more secure parameterized SQL
protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
{
string cmdr1 = "select arts.artsId,artdetails.mainImage from artdetails INNER JOIN arts ON artdetails.artsId=arts.artsId and arts.Category=@Category";
using (SqlConnection con = new SqlConnection("connectionString"))
using (SqlCommand cmd = new SqlCommand(cmdr1, con))
{
cmd.Parameters.AddWithValue("@Category", category);
con.Open();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
}