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#. What I need is to display a list of data in 2nd combo box based on the selection 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=@company


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";

When I put each line of code into separate try/catch block, I did find that the problem is on this line of code
'daProduct.Fill(dsProduct, "Prodct")'. But I can't figure out what is wrong with this. Does anyone have any idea about it?

Thanks in advance!
Jane
 
Jane,

What exactly is the problem you are getting? What is the exception
information?

Also, are you using relations between two tables to manage this? It is
very easy. If you set a relation between two data tables in a dataset, then
what you can do is bind the second combo box to the relation, and when an
item is selected in the first, only the related items in the second will
appear.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jane said:
Hello,

I am working on a windows application with C#. What I need is to display a
list of data in 2nd combo box based on the selection 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=@company


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";

When I put each line of code into separate try/catch block, I did find
that the problem is on this line of code
'daProduct.Fill(dsProduct, "Prodct")'. But I can't figure out what is
wrong with this. Does anyone have any idea about it?
 
hi jane!

try creating a data relation. use this created data relation as a data
source to your 2nd combo box.

HTH

-A


Jane said:
Hello,

I am working on a windows application with C#. What I need is to display a
list of data in 2nd combo box based on the selection 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=@company


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";

When I put each line of code into separate try/catch block, I did find
that the problem is on this line of code
'daProduct.Fill(dsProduct, "Prodct")'. But I can't figure out what is
wrong with this. Does anyone 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.
Jane
 
Back
Top