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

hii
Add the 3rd line code while binding DDlist.

ddlItems.DataSource = dsCategory;        
ddlItems.DataMember = "Categories";        
ddlItems.DataValueField= "CategoryID"
ddlItems.DataTextField = "Description";       
ddlItems.DataS= "CategoryID";

Mark as sovled if it helps you!!!

I have all my categories loaded on my Dropdownlist but the problem is when I select a category the details of that category do not show on datagrid and I think the problem is on selectedIndexChanged event of the dropdownlist coz I am suppose to be selecting a category then the details appear on the datagrid but thats not working

As i understood from you code in selectindexchanged event:

daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = "                + ddlItems.SelectedItem.Value;

You are passing param "categoryId", therefore u need to bind
dddl1.DataValueField = "categoryid"

Pls. let me know your exact concern

Yes I added that on my code but still it doesnt work or show the details that falls under that category in the datagrid

Try doing this.. it should work

Replace this code :

ddlItems.DataSource = dsCategory;
ddlItems.DataMember = "Categories"; 
ddlItems.DataTextField = "Description";

With

ddlItems.DataSource = dsCategory;
ddlItems.DataValuefield = "CategoryId"; 
ddlItems.DataTextField = "CategoryName";

and also puut all your code under Page_Load like this

protected void Page_Load(object sender, EventArgs e)    
{        

         if(!IsPostback)
          {
//This code is filling in the Dropdownlist with data from Category table
               // your complete Dropdown code here
        
       }
}

and in your SelectedIndexChanged function
change this

DataSet dsItems = new DataSet();            daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = "                + [B]ddlItems.SelectedValue[/B];

Thanx a lot man.... ur a star

hi above....
can u post ur entired code after making the necessry corrections pls....i need it urgently

My code is partial working cozI still have a problem coz when I select add to cart on my 1st datagrid it overwrite the existing record instead of adding to it but everything else is working

But then again u need to understand how the code work hey if u dont let me know then I will explain

/*Filling up the dropdownlist with each category. I used a procedure which select everything from Category which only have CategoryID and categoryDescription*/
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string strCategory = "sdp_ViewCategory";
            DataSet dsCategory = new DataSet();
            SqlDataAdapter daCategory = new SqlDataAdapter(strCategory, conn);
            daCategory.Fill(dsCategory, "Categories");
            ddlItems.DataSource = dsCategory;
            ddlItems.DataValueField = "CategoryID";
            ddlItems.DataTextField = "Description";
            ddlItems.DataBind();
            ddlItems.Items.Insert(0, "Please select a category");
            
        }
    }

/*Selecting a category from dropdownlist(DDL) and desplay the details of all the available product on that category depending on what I have selected on the DDL*/
protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
    {

        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 ProductID, ProductName, PictureName, UnitsInStock from dbo.Products where CategoryID = " + 
                ddlItems.SelectedValue; 


            try
            {
                dsItems.Tables.Remove("Products");
            }
            catch (Exception de)
            {
                lblMsg.Text = de.Message;
            }
            daItems.Fill(dsItems, "Products");
            dgItems.DataSource = dsItems;
            dgItems.DataMember = "Products";
            dgItems.DataBind();
            dgItems.Visible = true;
            //dgItems.SelectedItem.Cells[4].Visible = false;
           foreach(DataGridColumn col in dgItems.Columns)
           {
               if (col.HeaderText == "PictureName")
                   col.Visible = false;

           }
        }
        else
            dgItems.Visible = false;
    }

/*Im clicking button select which I added on my 1st datagrid and named it to add to cart then I add what the user select on the 1st datagrid using the select button*/
protected void dgItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        imgItems.Visible = true;
        imgItems.ImageUrl = "Pictures/" + dgItems.SelectedItem.Cells[4].Text;
        string strDetails = "sdp_ViewProducts";
        DataSet dsDetails = new DataSet();
        SqlDataAdapter daDetails = new SqlDataAdapter(strDetails, conn);
        daDetails.SelectCommand.CommandText = "Select ProductID, ProductName, UnitPrice from dbo.Products where ProductID = " +
              dgItems.SelectedItem.Cells[2].Text;

        daDetails.Fill(dsDetails, "Products");
        dgDetails.DataSource = dsDetails;
        dgDetails.DataMember = "Products";
        dgDetails.DataKeyField = "ProductID";
        dgDetails.DataBind();
        dgDetails.Visible = true;
    }

pls give me the complete code for it ..i tryed but not getting the output..pls help..iam pasting the code here but no output

protected void Page_Load(object sender, EventArgs e)
    {
       
        
        Response.Write("sql is connected");
        SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn);
        DataSet ds = new DataSet();
        da.Fill(ds,"countryname");
        ddlcountryname.DataSource = ds.Tables["countryname"];
        ddlcountryname.DataTextField = "countryname";
        ddlcountryname.DataValueField = "countryid";
        ddlcountryname.DataBind();
        

    }
    protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e)
    {
        string country_id;
        country_id = ddlcountryname.SelectedValue.ToString();
        SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
        DataSet ds1 = new DataSet();
        da1.Fill(ds1, "statename");
        ddlstatename.DataSource = ds1.Tables["statename"];
        ddlstatename.DataTextField = "statename";
        ddlstatename.DataValueField = "stateid";
        ddlstatename.DataBind();
    }

my database is something like this...i have two tables countryname(countryid identity(1,1),countryname) and statename(stateid identity(100,1),statename,countryid)

so i have inserted 78 countries in country table in tat 73rd is india...
n i have inserted states of india in statename table(28)..how to get india's states wen india in selected

i have two drop downlist wen i select india the partcular states shld come....but my selected index function is different from urs....is it correct

Is your DDL filled with each country?

hi myrequirement is i have two dropdownlist one is country n other is statename.....wen i selelc india corresponding states shls come...but i see my selected index function is different from urs...pls explain tat...n is my code right..iam not getting the output....

one more thing how did u create the table for tat category..like i inserted 79 countries.....with id...now in state table i need to insert 28 states for india..
stateid statename countryid.....or i need to have stateid statename1........statename28 countryid

I had the same problem my DDL doesnt talk to me back if I just select and do nothing, I couldnt figure out what was the problem because I had selectedChanged event but then until I had a button which I named to go and I didnt code anything on the button I just drag and dropped it so I select something from the DDL and click in the button and it worked just try that to see if its working then u will fix that problem later

ya i got all the countries wic i inserted into ddllcountryname

its registration form ..i cant keep button na...u find this in many site select country n corresponidng states will come in another dropdownlist...but the code iam not finding

U jst need to make sure that the countryID on the state table is corresponding to the one on the country name, then you can have a select stateme like this on ur dataadpater

Select * from State
where CountryID = ddlCountries.selectedValue;

Im sory I cant read your code can u edit it and make it C# instead of just text, its killing me

[ Code=C# ]
Paste code here dont leave
[ /Code ]
there souldnt be any space in between ur square brackets

I think at the moment u can use that to debug and see whats working and what's not working then after that u can fix ur selectedindexchanged event on DDL

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("sql is connected");
SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn);
DataSet ds = new DataSet();
da.Fill(ds,"countryname");
ddlcountryname.DataSource = ds.Tables["countryname"];
ddlcountryname.DataTextField = "countryname";
ddlcountryname.DataValueField = "countryid";
ddlcountryname.DataBind();


}
protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e)
{
string country_id;
country_id = ddlcountryname.SelectedValue.ToString();
SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "statename");
ddlstatename.DataSource = ds1.Tables["statename"];
ddlstatename.DataTextField = "statename";
ddlstatename.DataValueField = "stateid";
ddlstatename.DataBind();
}

SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn); Why your country_ID which you declared as a string is within single and double qoutation marks

Try this and see if it helps SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid = " + country_id ,cn);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.