473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

data binding

hi
i have a combo box which i populate with all tables from northwind database
and when the user selects an item from the combo, i want to populate the
datagrid with data from that table.

private void FillComboBox()

{

DataSet allTablesDataSet = new DataSet("AllTables");

string sqlString;

this.tableComboBox.Items.Clear();

sqlString = "Select * from sysobjects where type ='u'";

this.sqlDataAdapter = new SqlDataAdapter();

this.sqlDataAdapter.SelectCommand = new SqlCommand(sqlString,
this.sqlConnection);

this.sqlDataAdapter.Fill(allTablesDataSet,"AllTabl es");

this.tableComboBox.DataSource = allTablesDataSet.Tables[0];

this.tableComboBox.DisplayMember = "name";

this.tableComboBox.ValueMember = "name";

}

private void OnTableComboBoxSelectedIndexChanged(object sender,
System.EventArgs e)

{

if(this.tableComboBox.SelectedIndex == -1)

return;

this.tableDataSet = new DataSet();

this.sqlDataAdapter.SelectCommand = new SqlCommand("Select * from [" +
this.tableComboBox.SelectedValue + "]" ,this.sqlConnection);

this.sqlDataAdapter.Fill(this.tableDataSet, "Table");

this.dataGrid1.DataSource = this.tableDataSet;

this.dataGrid1.DataMember = "Table";

}

however the first time when the program is run i get the foll error. how do
i resolve that?

thnx

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred
in system.data.dll

Additional information: System error.

on this line

this.sqlDataAdapter.Fill(this.tableDataSet, "Table");
Jul 21 '05 #1
5 2319
Hi Frazer,

The standard problem with the binded combobox is that when it is loading it
fires unpredictable the selected index change event.

Solutions for this are, set a switch in that event function or add the
handler on the moment that it is completly loaded (when you are reloading it
than you have to set the switch again or remove the handler at the right
moment).

(Why you do that clear of the combobox, if you are reseting it in your
program, than you should set in my opinion the datasource to null)

I hope this helps?

Cor
Jul 21 '05 #2
Hi Frazier:

Are you firing another query each time the index changes? you definitely
don't want to do this. You can just load the data into a datatable and bind
it to the combobox. Also, you're getting a SQLException which means
specifically it's a problem w/ the db.

Cor is exaclty right about the SelectedIndexChange and that's ostensibly
causing this even though it's a db error. Also, don't use Dynamic SQL, use
Parameters. Nonetheless, you'll want to grab two tables, one for the
parent, one for the child, and just set the bindings of the grid and the
CombBox. You'll need to use a DataRelation
http://www.knowdotnet.com/articles/datarelation.html to make this easy. If
you do this, you won't have to trap anything on SelectedIndexChanged to get
the child records to show up in a grid. This is pretty common and if hit a
search engine for Master Detail ADO.NET there will be tons of examples.

Let me know if you need any elaboration.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
"Frazer" <Ic***@hotmail.com> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
hi
i have a combo box which i populate with all tables from northwind database and when the user selects an item from the combo, i want to populate the
datagrid with data from that table.

private void FillComboBox()

{

DataSet allTablesDataSet = new DataSet("AllTables");

string sqlString;

this.tableComboBox.Items.Clear();

sqlString = "Select * from sysobjects where type ='u'";

this.sqlDataAdapter = new SqlDataAdapter();

this.sqlDataAdapter.SelectCommand = new SqlCommand(sqlString,
this.sqlConnection);

this.sqlDataAdapter.Fill(allTablesDataSet,"AllTabl es");

this.tableComboBox.DataSource = allTablesDataSet.Tables[0];

this.tableComboBox.DisplayMember = "name";

this.tableComboBox.ValueMember = "name";

}

private void OnTableComboBoxSelectedIndexChanged(object sender,
System.EventArgs e)

{

if(this.tableComboBox.SelectedIndex == -1)

return;

this.tableDataSet = new DataSet();

this.sqlDataAdapter.SelectCommand = new SqlCommand("Select * from [" +
this.tableComboBox.SelectedValue + "]" ,this.sqlConnection);

this.sqlDataAdapter.Fill(this.tableDataSet, "Table");

this.dataGrid1.DataSource = this.tableDataSet;

this.dataGrid1.DataMember = "Table";

}

however the first time when the program is run i get the foll error. how do i resolve that?

thnx

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.

on this line

this.sqlDataAdapter.Fill(this.tableDataSet, "Table");

Jul 21 '05 #3
What i am doing is populating the combobox with all tables from the
northwind database and
when the user selects a particular table i fill the datagrid with rows from
that table.

Is my approach too lengthy to achieve this?
Is master - detail approach the right one to use for this purpose.

Thanks for your valuable input
Jul 21 '05 #4
Hi Frazer,

I find your approach very nice. Exactly how it should be done in my opinion.

Cor
Jul 21 '05 #5
Hi,
Thanks for your reply,
However I didnt understand where this would fit in my approach.

" Nonetheless, you'll want to grab two tables, one for the
parent, one for the child, and just set the bindings of the grid and the
CombBox. You'll need to use a DataRelation
http://www.knowdotnet.com/articles/datarelation.html to make this easy. If you do this, you won't have to trap anything on SelectedIndexChanged to get the child records to show up in a grid. "
Can i bind a datagrid and a combobox ?, so that when i click on certain
items in the combo the data grid is reflected accordingly???

Thnx.
"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl... Hi Frazier:

Are you firing another query each time the index changes? you definitely
don't want to do this. You can just load the data into a datatable and bind it to the combobox. Also, you're getting a SQLException which means
specifically it's a problem w/ the db.

Cor is exaclty right about the SelectedIndexChange and that's ostensibly
causing this even though it's a db error. Also, don't use Dynamic SQL, use Parameters. Nonetheless, you'll want to grab two tables, one for the
parent, one for the child, and just set the bindings of the grid and the
CombBox. You'll need to use a DataRelation
http://www.knowdotnet.com/articles/datarelation.html to make this easy. If you do this, you won't have to trap anything on SelectedIndexChanged to get the child records to show up in a grid. This is pretty common and if hit a search engine for Master Detail ADO.NET there will be tons of examples.

Let me know if you need any elaboration.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
"Frazer" <Ic***@hotmail.com> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
hi
i have a combo box which i populate with all tables from northwind

database
and when the user selects an item from the combo, i want to populate the
datagrid with data from that table.

private void FillComboBox()

{

DataSet allTablesDataSet = new DataSet("AllTables");

string sqlString;

this.tableComboBox.Items.Clear();

sqlString = "Select * from sysobjects where type ='u'";

this.sqlDataAdapter = new SqlDataAdapter();

this.sqlDataAdapter.SelectCommand = new SqlCommand(sqlString,
this.sqlConnection);

this.sqlDataAdapter.Fill(allTablesDataSet,"AllTabl es");

this.tableComboBox.DataSource = allTablesDataSet.Tables[0];

this.tableComboBox.DisplayMember = "name";

this.tableComboBox.ValueMember = "name";

}

private void OnTableComboBoxSelectedIndexChanged(object sender,
System.EventArgs e)

{

if(this.tableComboBox.SelectedIndex == -1)

return;

this.tableDataSet = new DataSet();

this.sqlDataAdapter.SelectCommand = new SqlCommand("Select * from [" +
this.tableComboBox.SelectedValue + "]" ,this.sqlConnection);

this.sqlDataAdapter.Fill(this.tableDataSet, "Table");

this.dataGrid1.DataSource = this.tableDataSet;

this.dataGrid1.DataMember = "Table";

}

however the first time when the program is run i get the foll error. how

do
i resolve that?

thnx

An unhandled exception of type 'System.Data.SqlClient.SqlException'

occurred
in system.data.dll

Additional information: System error.

on this line

this.sqlDataAdapter.Fill(this.tableDataSet, "Table");


Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
1
by: matty.hall | last post by:
There's a lot of information out there about data-binding UI objects (i.e. derived from Control) to non-UI custom business objects. Is it possible to do the same without any UI being involved at...
0
by: popsovy | last post by:
Hi I have a question about whether Data Binding can facilitate the process of saving data in a web application I learned that you can data bind information from a number of different data...
10
by: Fares Eidi | last post by:
I am a VB6 intermediate programmer just starting out using VB.net and would like to ask what I'd be missing out on if I just use time saving techniques like data binding, data adapter wizards etc...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
0
by: Larry Serflaten | last post by:
I am not sure how many are aware of this sort of data binding, but as it is new to many (classic) VB developers I thought I would post this once just to let people know of its availablility. ...
1
by: Fred Flintstone | last post by:
I've created my own data classes to retrieve data from a SQL server. I find it far more flexible. But it leaves me with some other questions. I've compiled the data classes to self contained...
2
by: Matthias | last post by:
Hi Team this may be a newbie question. I have searched the discussions before posting: I'd like to re-use a form instance to edit record details. My data binding (a DataTable bound to...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
1
by: Monty M. | last post by:
Does anyone know how to perform two way data binding between a combo box and a listview. The listview is bound to a dataset table in code: Binding Bind = new Binding(); DataTable dt;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.