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; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)