binding a combo box to a dataset

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.DefaultViewManager
2ndComboBox.DisplayMember="Product.SubCompany"

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

-
 
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
 
yes, I did try it

Here is the code

daProduct.TableMappings.Add("Table","Product")
daProduct.Fill(dsProduct)
2ndComboBox.DataSource=dsProduct.Tables;
 
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
 
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.
 
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
 
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.DefaultViewManager;
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?
 
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
 
Back
Top