Jump to content

Newbie question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
cowdog88

cowdog88

    Newbie

  • Members
  • Pip
  • 4 posts
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?

#2
jireh

jireh

    Newbie

  • Members
  • PipPip
  • 18 posts
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();

#3
jireh

jireh

    Newbie

  • Members
  • PipPip
  • 18 posts
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);

#4
cowdog88

cowdog88

    Newbie

  • Members
  • Pip
  • 4 posts
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
jireh

jireh

    Newbie

  • Members
  • PipPip
  • 18 posts
please rephrase... I dont get what you mean...
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
cowdog88

cowdog88

    Newbie

  • Members
  • Pip
  • 4 posts
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.