Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 ASP.NET
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);
 
        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 16-Jan-13 4:00am
Edited 16-Jan-13 4:07am
5.9K
v2
Comments
maysam_p82 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid