Guys
I have a dropdownlist control with categories so when I select something from ddlCategory I wana display all the products fall under that category in a datagrid but I dont seem to get that right... here is the bit I have done
protected void Page_Load(object sender, EventArgs e)
{
//This code is filling in the Dropdownlist with data from Category table
string strCategory = "sdp_ViewCategory";
DataSet dsCategory = new DataSet();
SqlDataAdapter daCategory = new SqlDataAdapter (strCategory, conn);
daCategory.Fill(dsCategory, "Categories");
ddlItems.DataSource = dsCategory;
ddlItems.DataMember = "Categories";
ddlItems.DataTextField = "Description";
// ddlItems.DataSourceID = "CategoryID";
ddlItems.DataBind();
ddlItems.Items.Insert(0, "Please select a category");
}
protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
{
//Select a category and display datagrid
dgItems.Visible = true;
string strItems = "sdp_ViewCartAndProduct";
SqlDataAdapter daItems = new SqlDataAdapter(strItems, conn);
if (ddlItems.SelectedIndex > 0)
{
DataSet dsItems = new DataSet();
daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = "
+ ddlItems.SelectedItem.Value;
try
{
dsItems.Tables.Remove("Products");
}
catch (Exception de)
{
lblMsg.Text = de.Message;
}
daItems.Fill(dsItems, "Products");
dgItems.DataSource = dsItems;
dgItems.DataMember = "Products";
dgItems.DataKeyField = "CategoryID";
dgItems.DataBind();
dgItems.Visible = true;
}
else
dgItems.Visible = false;
}
Thanx in advance