database column to listbox

I want the entire contents of an Access column to appear in a listbox

Page 1 of 1

2 Replies - 993 Views - Last Post: 31 July 2009 - 02:51 PM Rate Topic: -----

#1 TheDuc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-July 09

database column to listbox

Posted 31 July 2009 - 02:26 PM

I am new to .Net and programming in general. I am trying to read an Access database, obtain all of the email addresses from the correct column, and present the results in a textbox with each email address separated by a semi-colon and a space (for easy copy/paste to Outlook).

I have played around with the code so much based on what looked similar on the web, but haven't got there. The best I have done is to get the final record in the database to appear. I have given up on the adding of the semi-colon + space part for now, as I am not even able to get the list to be pulled in. Here is the part that IS working for me so far:
private void button1_Click(object sender, EventArgs e)
		{
			string connPath = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=//path/to/folder/DCMClientContacts.accdb";
			string selectEmail = "SELECT * FROM Clients";

			contactList = new System.Data.OleDb.OleDbConnection(connPath);
			OleDbCommand myAccessCommand = new OleDbCommand(selectEmail, contactList);
			OleDbDataAdapter da = new OleDbDataAdapter(myAccessCommand);

			DataSet ds1 = new DataSet();
			da.Fill(ds1, "Clients");

			DataRowCollection dra = ds1.Tables["Clients"].Rows;
			ArrayList emailList = new ArrayList();

					textBox1.DataBindings.Add("text", ds1, "Clients.Email");
		  }



I have tried using a 'foreach' statement around the textbox line, but that throws an error (ArgumentException was unhandled: This causes two bindings in the collection to bind to the same property. Parameter name: binding) which I believe is due to the textbox having contents from the first pass still present on the second pass. I've also tried tinkering with IList and Arrays, but to no avail.

I had considered that I may be able to create additional variables and just move/manipulate the data on each pass to get my desired results, but that strikes me as being inefficient (and likely the way it was done 20 years ago).

There has got to be a simple way to get the entire contents of a database column into a single text field, but I cannot find it anywhere. Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: database column to listbox

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: database column to listbox

Posted 31 July 2009 - 02:36 PM

I haven't used DataAdapters much so I'll give you my advice using OleDbDataReader which I find very easy to use. Though datasets aren't hard either, it's just a question which you are more used to.

			contactList = new System.Data.OleDb.OleDbConnection(connPath);
			OleDbCommand myAccessCommand = new OleDbCommand(selectEmail, contactList);
			OleDbDataReader reader = myAccessCommand.ExecuteReader();
		   string str = "";
		   while (reader.Read()) {
			   str += reader["Email"].ToString() + ", ";
		   }
		   textBox1.Text = str;



This should work. Just make sure the column name is correct on reader["xxx"]. You can also use numeric index.
Was This Post Helpful? 1
  • +
  • -

#3 TheDuc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-July 09

Re: database column to listbox

Posted 31 July 2009 - 02:51 PM

Thanks...that appears to have done it (except I had to add a line to open the connection). Would you believe that I have been searching for an answer for 2 weeks on this?

Thank you so much for your help!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1