Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey,im trying to populate my listview based on value selected in dropdownlist. actualy im not getting any errors, but listview is not displayed when user selects any dropdownlist value. DROPDOWNLIST1: i have a table say "cat" with columns-id,category and categoryid... i successfully populated its value from the database.
LISTVIEW1: based on category id i recieve from dropdownlist i need to update my listview
my code:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        DataTable table = new DataTable();
        //int id = Convert.ToInt32(DropDownList1.SelectedValue);
        // int id = int.Parse(DropDownList1.SelectedValue.ToString());
        string cat_id = DropDownList1.SelectedValue.ToString();
        int id;
        if (!(int.TryParse(cat_id.ToString(), out id)))
        using (SqlConnection con = new SqlConnection(constr))
            string sql = "select id,name,description,category,image,price,categoryid from products WHERE categoryid=@cid ORDER BY categoryid";
            using (SqlCommand cmd = new SqlCommand(sql, con))
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    cmd.Parameters.AddWithValue("@cid", cat_id);

        listview.DataSource = table;
Updated 26-Aug-14 23:41pm
Gihan Liyanage 27-Aug-14 5:17am    
Can you please debug and see where is the issue? Then you can post the code sample to get an answer. Or you can tell what you want clearly on here, then some times users will give you good links or advices. This type of large codes may not read by experts. This is only an advice for get quick answers for you . :)
jin19 27-Aug-14 5:45am    
see i dont have any error,but when any value in dropdownlist is not getting any listview
Gihan Liyanage 27-Aug-14 5:51am    
are you sure the Value of table Variable has filled out after select a specific dropdown list value ?

As I said earlier, if you get correct value to the datatable you have defined, then use this function to populate List view using the data table.

private void LoadList()
// Clear the ListView control
            int ColCount = dtable.Columns.Count;
            //Add columns
            for (int k = 0; k < ColCount; k++)
            // Display items in the ListView control
            for (int i = 0; i < dtable.Rows.Count; i++)
                DataRow drow = dtable.Rows[i];

                // Only row that have not been deleted
                if (drow.RowState != DataRowState.Deleted)
                    // Define the list items
                    ListViewItem lvi = new ListViewItem(drow[0].ToString());
                    for (int j = 1; j < ColCount; j++)
                    // Add the list items to the ListView
Share this answer
Gihan Liyanage 15-Sep-14 6:19am    
Did you got a solution from my support, I can see you have not accepted any answer.If you are ok with this answer plz accept it. Then any user having same problem can identified it has solved the problem..
jin19 19-Sep-14 4:00am    
sir,actualy i solved it using my code itself..i forgot to check Page.IsPostBack property thatz y i dint retrieve any value... thanks for your support :)

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