I have a mysql database and I am trying to load a combo box.
This is what I have.
Private void Location_Load(object sender, EventArgs e)
{
{
string connectStr = "SERVER=localhost;" + "DATABASE=name;" + "UID=root;" + "Password=;";
system.data.idbconnection sqlconnect = new system.data.sqlclient.sqlconnection(connectStr);
string command = "Select [firstName] from [User]";
system.data.idbcommand co = new system.data.sqlclient.sqlcommand();
co.commandtext = command;
co.connection = sqlconnect;
sqlconnect.open();
co.executenonquery();
system.data.idbdataadapter ad = new system.data.sqlclient.sqldataadapter();
ad.selectcommand = co;
dataset ds = new dataset();
adfill(ds);
combobox3.datasource = ds.tables[0];
combobox3.datatextfield = ds.Tables[0].columns ["UserID"].columnName.tostring();
combobox3.datavaluefield = ds.tables[0].columns["FirstName"] .columnname.tostring();
combobox3.databind();
sqlconnect.close();
when i run this i get 3 errors combobox does not contain definition for datatextfield, datavaluefield, and databind. Any advice?
Newbie question
Started by cowdog88, Feb 07 2008 09:53 AM
5 replies to this topic
#1
Posted 07 February 2008 - 09:53 AM
|
|
|
#2
Posted 08 February 2008 - 12:50 AM
advice but not to your problem:
1. Instead of always using "system.data.idbconnection "
put it in the top/add it in the top. declare it like this "using System.Data;" so that when you declare a variable you will just simply decalre it as "SqlConnection varname = new sqlconnection();
1. Instead of always using "system.data.idbconnection "
put it in the top/add it in the top. declare it like this "using System.Data;" so that when you declare a variable you will just simply decalre it as "SqlConnection varname = new sqlconnection();
#3
Posted 08 February 2008 - 03:07 AM
as what i observed to your problem. you want to add a data in the combo box fo the database...so how about this one....
comboBox1.Items.Add(the index of the column in the current datarow);
comboBox1.Items.Add(the index of the column in the current datarow);
#4
Posted 09 February 2008 - 11:32 AM
I have actually done the above mentioned, but when I do this I lose the Primary key. When I go to pull from the combobox it only recognizes the name and not the primary key???
#5
Posted 10 February 2008 - 10:56 PM
please rephrase... I dont get what you mean...
did you still used this?
it's unnecessary...
did you still used this?
combobox3.datasource = ds.tables[0]; combobox3.datatextfield = ds.Tables[0].columns ["UserID"].columnName.tostring(); combobox3.datavaluefield = ds.tables[0].columns["FirstName"] .columnname.tostring(); combobox3.databind();
it's unnecessary...
#6
Posted 11 February 2008 - 03:55 PM
I did not try these two together. Ill have to play with it tonight and see if I can get it to work. Thanks for the help.


Sign In
Create Account

Back to top









