Click here to Skip to main content
13,357,610 members (35,739 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,I have this code-behind . I have two lists(a drop-down list that have some fields retrieved from database. And a list view). I want when I select an item in the drop-down list a category of that field be highlighted at the another list. To now, the code was executed successfully.The problem is where I want to select an item from product list(drop-down list). But when I selected every item in the first list, only the first item in the second list is highlighted! I dont know, the problem was from my database query or was from my code,I think when I choose every item in the first list the item value nothing increased! please help me thank you.
protected void Page_Load(object sender, EventArgs e)
        string sqlSelect="Select ID,ProductName from Product";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand com = new SqlCommand(sqlSelect, con);


            lstProduct.DataSource = com.ExecuteReader();
            lstProduct.DataTextField = "ProductName";
            lstProduct.DataValueField = "ID";
        catch (Exception Err)
            lblMessage.Text = "Error connecting to database ";
            lblMessage.Text += Err.Message;

        lstProduct.SelectedIndex = -1;   
    protected void lstProduct_SelectedIndexChanged(object sender, EventArgs e)
        String sqlSelect1 = "Select ProductName,QuantityPerUnit,CategoryName " +
            "from Product Inner Join Category on " +
            "Category.CategoryID=Product.CategoryID " +
            "where ID=@ID";

        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(sqlSelect1, con);
        using (con)
            cmd.Parameters.AddWithValue("@ID", lstProduct.SelectedItem.Value);
            SqlDataReader reader = cmd.ExecuteReader();
            lblRecordInfo.Text = "Product: " +
                reader["ProductName"] + "<br />";
            lblRecordInfo.Text += "Quantity: " +
                reader["QuantityPerUnit"] + "<br />";
            lblRecordInfo.Text += "Category: " +
            string matchCategory = reader["CategoryName"].ToString();
            string selectCategory = "Select CategoryName, " +
                "CategoryID from Category";
            SqlCommand cmdCategories = new SqlCommand(selectCategory, con);
            lstCategory.DataSource = cmdCategories.ExecuteReader();
            lstCategory.DataTextField = "CategoryName";
            lstCategory.DataValueField = "CategoryID";
            //Highlight the matching category in the list
            //قسمت پیدا شده مورد نظر را در لیست مشخص می کند
            lstCategory.Items.FindByText(matchCategory).Selected = true;

        //lstCategory.Visible = true;
Posted 16-Jan-13 5:00am
Updated 16-Jan-13 5:07am
maysam_p82 16-Jan-13 11:00am
Is anyone knows its solution?:(

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180111.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100