Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
4.00/5 (1 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.
C#
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);

        try
        {
            con.Open();


            lstProduct.DataSource = com.ExecuteReader();
            lstProduct.DataTextField = "ProductName";
            lstProduct.DataValueField = "ID";
            this.DataBind();
        }
        catch (Exception Err)
        {
            lblMessage.Text = "Error connecting to database ";
            lblMessage.Text += Err.Message;
        }
        finally
        {
            con.Close();

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

        //lstCategory.Visible = true;
    }
Posted
Updated 16-Jan-13 4:07am
v2
Comments
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)



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