By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

binding a combo box to a dataset

P: n/a
Hello

I am working on a windows application with C#.net. What I need is to display a list of data in 2nd combo box based on the choice of the 1st combo box. I used SqlCommand object and SqlDataReader. It works fine. But when I use DataAdapter and DataSet objects, it doesn't work. Here is my code

************************************************** ********************************
In SQL db, there is a stored procedure

CREATE PROCEDURE [proTest]
@company as nvarchar(255)
AS
select distinct SubCompany from tblCompany where Company=@compan

In the applications

SqlConnection objConnection = new SqlConnection(connString)
SqlCommand cmdProduct=new SqlCommand(proTest, objConnection)
SqlDataAdapter daProduct=new SqlDataAdapter(cmdProduct)
daProduct.SelectCommand.Parameters.Add("@company", SqlDbType.NVarChar, 255, "Company")
daProduct.SelectCommand.Parameters["@company"].Value=1stComboBox.Text
DataSet dsProduct=new DataSet()
daProduct.Fill(dsProduct, "Product")
2ndComboBox.DataSource=dsProduct.DefaultViewManage r
2ndComboBox.DisplayMember="Product.SubCompany"

Does anyone has idea what is the problem? Thanks in advance

-

Jul 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Cor
Hi Jane,

I would just do this

2ndComboBox.DataSource=dsProduct.tables(0);

Cor
Jul 21 '05 #2

P: n/a
Thanks for your help.

Unfortunately, it doesn't work. Although I add one more line code,
daProduct.TableMappings.Add("Table","Product");

BTW, I couldn't use Tables(0) since Tables is only a property of DataAdapter.
----- Cor wrote: -----

Hi Jane,

I would just do this

2ndComboBox.DataSource=dsProduct.tables(0);

Cor

Jul 21 '05 #3

P: n/a
Cor
Did you try it?
Jul 21 '05 #4

P: n/a
yes, I did try it

Here is the code

daProduct.TableMappings.Add("Table","Product")
daProduct.Fill(dsProduct)
2ndComboBox.DataSource=dsProduct.Tables;
Jul 21 '05 #5

P: n/a
Cor
Hi Jane,

I wrote

daProduct.TableMappings.Add("Table","Product");
daProduct.Fill(dsProduct);
2ndComboBox.DataSource=dsProduct.Tables(0);


The first table, table 0

I hope this goes?

Cor
Jul 21 '05 #6

P: n/a
Cor
Sorry,

I did give you the vb.code mixed up with C#?

:-))
I can't complie the file by using the following code,
2ndComboBox.DataSource=dsProduct.Tables[0];

Since 'Tables' is only a property of DataAdapter object rather than a method, you can't have any parameters in it.

Jul 21 '05 #7

P: n/a
Thanks so much, Cor

I tried your code. Unfortunately, it doesn't work.
When I put each line of code into separate try/catch block, I did find that the problem is on 'daProduct.Fill(dsProduct)'. But I can't figure out what is wrong with this. Do you have any idea about it

Jane
Jul 21 '05 #8

P: n/a
Cor
Hi Jane,

The most simple method to test that is to make it first simple and than
remake it as you wish.
SqlConnection objConnection = new SqlConnection(connString);
SqlCommand cmdProduct=new SqlCommand(proTest, objConnection);
SqlDataAdapter daProduct=new SqlDataAdapter(cmdProduct);
daProduct.SelectCommand.Parameters.Add("@company", SqlDbType.NVarChar, 255,
"Company");
daProduct.SelectCommand.Parameters["@company"].Value=1stComboBox.Text;
--
by instance insert this tempory here with the good values for xxxx=xxxx
daProduct=new SqlDataAdapter("Select * from Product Where xxxx=xxxx",
objConnection);
----
DataSet dsProduct=new DataSet();
daProduct.Fill(dsProduct, "Product");
2ndComboBox.DataSource=dsProduct.DefaultViewManage r;
2ndComboBox.DisplayMember="Product.SubCompany";

I tried your code. Unfortunately, it doesn't work.
When I put each line of code into separate try/catch block, I did find that the problem is on 'daProduct.Fill(dsProduct)'. But I can't figure out
what is wrong with this. Do you have any idea about it?
Jane

Jul 21 '05 #9

P: n/a
Thanks so much for the help

I have figured out the problem, which is that I have to add parameter and set up its value in SqlCommand object rather than in DataAdapter object, and then create a DatatAdapter object using this SqlCommand object

Thanks again
Jan

Jul 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.