5 Replies - 25934 Views - Last Post: 26 December 2011 - 04:52 AM Rate Topic: -----

#1 omart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 09

fill data from database in the combobox

Posted 17 December 2009 - 06:15 AM

Hi all,
I want to fill a combobox from a column taken from my database, I worked on a code snippet that I found on the internet but it does not work:

void Fillcombo()
		{
			string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and	 Settings\\Administrateur\\Mes documents\\stockage.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
			cn.ConnectionString = connectionString;
			cn.Open();
			SqlDataAdapter d = new SqlDataAdapter("select nom_pdt from Produit", cn);
			DataTable dt = new DataTable();
			d.Fill(dt);
			comboBox1.DataSource = dt;
			//comboBox1.DataBindings.ToString(); 
		}




help me please
thank you

Is This A Good Question/Topic? 0
  • +

Replies To: fill data from database in the combobox

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: fill data from database in the combobox

Posted 17 December 2009 - 06:51 AM

Try this:

void Fillcombo()
{
	string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and	 Settings\\Administrateur\\Mes documents\\stockage.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
	cn.ConnectionString = connectionString;
	cn.Open();
	SqlDataAdapter d = new SqlDataAdapter("select nom_pdt from Produit", cn);
	DataTable dt = new DataTable();
	d.Fill(dt);
	comboBox1.DataSource = dt;
	comboBox1.DisplayMember = "nom_pdt";
}



Was This Post Helpful? 1
  • +
  • -

#3 karimi  Icon User is offline

  • Banned
  • member icon

Reputation: -9
  • View blog
  • Posts: 104
  • Joined: 29-May 09

Re: fill data from database in the combobox

Posted 19 December 2009 - 04:50 AM

The above code has some bugs try the bellow fixed one

void Fillcombo()
{
	string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and	 Settings\\Administrateur\\Mes documents\\stockage.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
	cn.ConnectionString = connectionString;
	cn.Open();
	SqlDataAdapter d = new SqlDataAdapter("select nom_pdt from Produit", cn);
	Dataset dt = new Dataset();
	d.Fill(dt);
	comboBox1.DataSource = dt.tables[0]; /// assing the first table of dataset
	comboBox1.DisplayMember = "nom_pdt";
}



This post has been edited by karimi: 19 December 2009 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: fill data from database in the combobox

Posted 19 December 2009 - 05:16 AM

karimi is right. I usually use "dt" for DataTable and "ds" for DataSet. And by the way: when you use DataAdapter to Fill your DataTable or DataSet, you don't need to explicitly open connection, because DataAdapter should do that automatically.
Was This Post Helpful? 0
  • +
  • -

#5 ddsreesiva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-December 11

Re: fill data from database in the combobox

Posted 26 December 2011 - 12:56 AM

View Postkarimi, on 19 December 2009 - 04:50 AM, said:

The above code has some bugs try the bellow fixed one

void Fillcombo()
{
	string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and	 Settings\\Administrateur\\Mes documents\\stockage.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
	cn.ConnectionString = connectionString;
	cn.Open();
	SqlDataAdapter d = new SqlDataAdapter("select nom_pdt from Produit", cn);
	Dataset dt = new Dataset();
	d.Fill(dt);
	comboBox1.DataSource = dt.tables[0]; /// assing the first table of dataset
	comboBox1.DisplayMember = "nom_pdt";
}




I used this code nd it was helpful to display data in combobox bt am not able to select the item from this combobox to database.
Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: fill data from database in the combobox

Posted 26 December 2011 - 04:52 AM

Please don't give the OP finished code... He needs to learn something for himself, and not be a copy/paste programmer :D

You should've said that all of the code is OK except the part when it actually displays the data in the comboBox.

And OP please define:

Quote

select the item from this combobox to database

How do you mean "select the item to a database"?

Maybe look at the events that the comboBox has and pass some of the event args as parameters
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1