Click here to Skip to main content
15,885,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating gallery and I am using drop down list on that page. I want to display data based on drop down list item selection from sql server database if user selects item from drop down list.I have written following code but it is not working.Please help.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
          
           bind1();
       }

   }


public void bind1()
   {
       ListItem item1 = new ListItem("Choose category", "1");
       ListItem item2 = new ListItem("Landscapes", "3");
       ListItem item3 = new ListItem("Nature", "4");
       ListItem item4 = new ListItem("People", "6");
       ListItem item5 = new ListItem("Seascapes", "8");
       ListItem item6 = new ListItem("Unique", "9");
       ListItem item7 = new ListItem("Popular", "7");
       ListItem item8 = new ListItem("Patterns", "5");
       ListItem item9 = new ListItem("Abstract", "2");
       ListItem item10 = new ListItem("Other", "10");
       ddlcategory.Items.Add(item1);
       ddlcategory.Items.Add(item9);
       ddlcategory.Items.Add(item2);
       ddlcategory.Items.Add(item3);
       ddlcategory.Items.Add(item8);
       ddlcategory.Items.Add(item4);
       ddlcategory.Items.Add(item7);
       ddlcategory.Items.Add(item5);
       ddlcategory.Items.Add(item6);
       ddlcategory.Items.Add(item10);
   }

protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
   {
       DataSet ds = new DataSet();
       DataTable dt = new DataTable();
       con.Open();

       string cmdr1 = "select arts.artsId,artdetails.mainImage from artdetails INNER JOIN arts ON artdetails.artsId=arts.artsId and Category='" + ddlcategory.SelectedIndex.ToString() + "' ";

       SqlCommand cmd = new SqlCommand(cmdr1, con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(dt);
       DataList1.DataSource = dt;
       DataList1.DataBind();
     }
Posted
Comments
Kornfeld Eliyahu Peter 18-May-14 5:44am    
You have to be more specific with that 'not working'! But event now I can tell you two thing.
1. Read here to learn how bind DD to list of values in a simpler way - http://www.codeproject.com/Tips/303564/Binding-DropDownList-Using-List-Collection-Enum-an
2. NEVER use string concatenation to create SQL query, but paramterized query
Member 10724668 18-May-14 6:16am    
okay. Thanx for the link. and not working means nothing is changed after selecting item from drop down list.My page remains as it is.No error and No exception
Kornfeld Eliyahu Peter 18-May-14 6:19am    
Do you have a postback at all?
Put debuggers inside the DropDownList SelectedIndexChanged Event and see if it is hitting or not while you select any option.

Please implement the following things to make your code working..

1) Please ensure that you have set the AutoPostBack property of dropdownlist to true..

2) In your query, you have written code to compare category with the selectedIndex of dropdownlist which is not right.. If you are going to compare the category with it's name then please write ddlcategory.SelectedItem.Text in place of ddlcategory.SelectedIndex.ToString(). If you are going to compare category with category ID then please write ddlcategory.SelectedItem.Value)in place of ddlcategory.SelectedIndex.ToString()

Happy Coding :) :)
 
Share this answer
 
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.NET
<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.
C#
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

C#
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); //set category value using ddlcategory.SelectedValue or ddlcategory.SelectedItem.Text depending on your data in Category column
        con.Open();
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }

    }
}
 
Share this answer
 
v6
Comments
Member 10724668 18-May-14 7:20am    
thanx. i haven't set AutoPostBack property. It worked now
DamithSL 18-May-14 7:22am    
You are welcome!
I have added few more improvements for your coding, hope that helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900