Populating A Combobox

Well, what should be a simple task is turning out to be a nightmare.

I have a simple combobox on my form. I have a simple database with a table in it and some data.

All I want to do is populate this combobox, but nothing works.

I setup a dataset, the bindingsource, and tableadapter, in preview data comes back, but when the project is run the combo box is always empty.

I've tried a bunch of other stuff, but the results are the same.

I've searched and searched for a simple tutorial on this, but can't find anything specific to this problem.

I'm using Visual Studio 2005 Beta 2. Any help would be greatly appreciated.

[636 byte] By [brixel] at [2007-12-16]
# 1
Brixel,
Try doing it manual. I had the same problem in .NET 1.1 until I decided to manually bind the dropdownlist control. For some reason when I try to let Visual Studio 2003 to bind it, all it would show is a empty list.
Matt
lokiprime at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
You've probably missed a step or two. How about posting what you have so we can tell you what you're missing ...
BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3
Hi,
If all you want is to populate the combo. Then I suggest to do it manually, pereferred way is a datareader. creating dataAdapter objects and binding it to the control is quite a waste of memory... Better populate it via a reader and dispose the objects after it is populated...

cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4
Paul,

Sorry, this is just bad advice. None of that DataAccess stuff should be on a form, and if you use a DataReader, that's what you're doing. There's not much difference between that and using a DataAdapter to fill a DataSet anyway.

All access to the backend data should be done in a separate server-side DataAccess class and accessed through either Web Services (which is the way I recommend) or through Remoting.

The DataAccess class passes back a DataSet and the DataBinding is done to the tables in the DataSet.

BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5
Using VS 2003, using a IDataAdapter and a DataSet to bind to GUI components is powerful and easy. See below Method using Oracle ADO.NET objects...

private void Bind2DbCb()
{
DataSet ds=new DataSet();
OracleConnection ocx=new Oracle.DataAccess.Client.OracleConnection(@"Your own connection string here...");
OracleCommand ocmd=ocx.CreateCommand();
ocmd.CommandText=@"select projectname from table_values";
IDataAdapter oadapt=new Oracle.DataAccess.Client.OracleDataAdapter(ocmd);
oadapt.Fill(ds);
comboBox1.DataSource=ds.Tables[0];
comboBox1.ValueMember=ds.Tables[0].Columns[0].ToString();
}

The important thing to specify when you have a DataSource is the ValueMember.
Of course, you'd put all your DB stuff somewhere else and execute at start up or when you need them. HTH and what you were looking for.

valeneo at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 6
Hi BonnieB,
Thanks for enlightening me. I didn't knew that. Have you got links that discuss this kind of approach? Coz I wanna know the reason why this is needed even with a simple combobox population. I think there is a logical explanation for this, I am also creating DataLayers (Frameworks) for some of my large systems and im aware of its purpose and benefits. Any links that discusses this would be very much appreciated...

cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 7
Paul,

Any leading industry expert will tell you that the data access does not belong in the form. Sorry, I don't have any links bookmarked (you can probably Google just as easily as I can). What do you do if your database cannot be seen from the client-side? You need to be able to access it somehow! Web Services to the rescue!

As far as a simple ComboBox ... you gotta get the data from somewhere to begin with, that's true ... but you do NOT get it from the Form with a DataReader. Big no-no! Plus, using a DataTable for the DataSource of a ComboBox is a no-brainer.

What I do for code tables that are needed throughout the app for Combos is cache a Codes DataSet client-side (at app start-up, it's determined whether there are newer codes in the database and if so, after retrieving them, the DataSet is cached by writing it out as an XML file, to be used next time if there are no newer codes). It has static tables, so they can be used throughout the app quite easily.

Does this help clear things up at all?

BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 8
Hi,
Whew, almost got lost in there...
Got your point. Very clever.
Please don't get me wrong, I really wanna try on doing what you suggested. But i've got some inquiries before I do that.

Is webservices really that fast? Or would you rather recommend that I create a dll for my datalayer?

Im quite skeptical on webservices (I only use them when creating ASP.net apps, still haven't used it on a windows forms) in accordance to its performance. Any advice or tips would be very much appreciated...
cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 9
Paul,

There's nothing wrong with Web Service performance, IMHO. It works quite well. My Web Services are, for the most part, nothing more than a wrapper around my Biz classes (DLLs), which call my DataAccess classes (DLLs), so I'm really not sure what you mean when you said:

Is webservices really that fast? Or would you rather recommend that I create a dll for my datalayer?

Both the Biz layer and the DataAccess layer are server-side DLLs and everything passes around DataSets. I don't know what else to tell you ... you say that you've used Web Services before, so you know how to do it. Make a little test app and play with it. If you have questions, just ask.

BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 10
Hi,
Thanks for the advice, i'll do that...
What I meant about dll for my datalayer, is to create a dll with all my data classes, but its still located on the client (I guess i'll skip on that).
Thanks again...
cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified