Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 populate 2nd combobox based on the 1st combobox

Author  Topic 

yshie
Starting Member

28 Posts

Posted - 2007-04-02 : 11:14:44
i want to update my second combo box based on the choice on the first
combobox. what is the simplest way to do this? i had my first combobox works.

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-02 : 11:24:26
yshie --

I am also having a problem, maybe we can help each other. My textbox does not get updated from my checkbox. Can you help me?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-04-02 : 11:27:32
quote:
Originally posted by yshie

i want to update my second combo box based on the choice on the first
combobox. what is the simplest way to do this? i had my first combobox works.



Give us more info, what kind of system? What database type? Are you using stored procedures ? How are we supposed to help you with that information, think about that!

If you are using sprocs assume you are writing a car dealership system where you have 2 filters to find cars on. Make and model. The make is basically the parent, nothing depends on it. The model has a make attribute to it.

Consider the following makes:
GM
Ford
DCX

Consider the following models:
Mustang
PT Cruiser
Hummer

Consider the following make models:
GM -> Hummer
Ford -> Mustang
DCX -> PT Cruiser

Now your first drop down box is your make:

SELECT Make FROM MyMakes ORDER BY Make

Now your second drop down depends on the selection of your first drop down, in addition, whatever you select from your first drop down you pass into your stored procedure to pull data.

SELECT Model FROM MyModels WHERE Make=@YourPassedInValue



Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url]
Go to Top of Page

yshie
Starting Member

28 Posts

Posted - 2007-04-02 : 11:37:15
im sorry.. yes, that's what i mean! i thought of using stored procedure. but how about using dataadapter and dataset? do i need them? im sorry because im a newbie. tnx 4 trying 2 help
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-02 : 12:14:28
yshie -- the point I was trying to make is, it is impossible to help someone if you don't provide details.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

yshie
Starting Member

28 Posts

Posted - 2007-04-02 : 14:43:53
ok.. so far, this is my code for the 1st combobox
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.dbConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT [CategoryName] FROM Categories",conn);

DataSet ds = new DataSet();
daCat.Fill(ds,"Categories");
comboBox1.DataSource = ds.Tables["Categories"];
comboBox1.DisplayMember = "CategoryName";
}

i don't know how the code for second combobox will start. but the sql will say something like this..
select productname
from products
where categoryID=combobox1.text
Go to Top of Page

yshie
Starting Member

28 Posts

Posted - 2007-04-10 : 08:22:22
DataSet ds;
String strConn="Server=CHEETAH;Database=NorthWind;Trusted_Connection=True;";

private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(strConn ))
{
ds = new DataSet();
SqlDataAdapter daCat = new SqlDataAdapter("SELECT [CategoryID],[CategoryName] FROM Categories", conn );
daCat.Fill(ds, "Categories");
comboBox1.DisplayMember = "CategoryName";
comboBox1.ValueMember = "CategoryID";
comboBox1.DataSource = ds.Tables["Categories"];
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(strConn ))
{
SqlDataAdapter daProd = new SqlDataAdapter("SELECT [ProductID], [ProductName] FROM Products WHERE CategoryID = @CategoryID", conn);
daProd.SelectCommand.Parameters.AddWithValue("@CategoryID", comboBox1.SelectedValue);
daProd.Fill(ds, "Products");
comboBox2.DisplayMember = "ProductName";
comboBox2.ValueMember = "ProductID";
comboBox2.DataSource = ds.Tables["Products"];
}
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.dbConnectionString))
{
SqlDataAdapter daProdDesc = new SqlDataAdapter("SELECT [ProductID], [Description] FROM Products WHERE CategoryID=@CategoryID", conn);

daProdDesc.SelectCommand.Parameters.AddWithValue("@CategoryID", comboBox2.SelectedValue);
daProdDesc.Fill(ds, "Products");
listBox1.DataSource = ds.Tables["Products"];
listBox1.DisplayMember = "Description";
listBox1.ValueMember = "ProductID";
}
}

this is my code for this.. its working but not perfectly.
on the form load, category name displays as well as its corresponding products. but when i change to 2nd category for example, its corresponding products list is added to the previous list. meaning products for category 1&2 are displayed..

i have also added another code for displaying the product's description in a listbox. but the problem is that all the descriptions of all the products are being displayed. i just want to display description of the selected product.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-10 : 08:32:54
in the Form_Load event, you must check to see if it is a Postback, otherwise each time the form loads your dropdownlist gets refilled.


private void Form1_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

--- your code here ---
}
}


This is a key, fundamental part of ASP.NET to understand, so if you are not familiar with the postback model and how it works I STRONGLY suggest that you read up on it. it is crucial to understand when writing any ASP.NET code.

Anyway, if you surround your code with that if{ } , I think that should do the trick for you.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -