Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Show data from ms access in a textBox
 

Show data from ms access in a textBox

Hi there,

I need your help with something you may find easy but it isn't for me, sadly. I need to show data from my MS Access database into several textBox-es just showing no modifing.

I have a sample from my program :

OleDbCommand nameCmnd = new OleDbCommand("SELECT name from Customer WHERE customerID = "+ enteredID, myConn);

OleDbDataAdapter nameAdapter = new OleDbDataAdapter(nameCmnd);

DataSet dsName = new DataSet();

nameAdapter.SelectCommmand = nameCmnd;

nameAdapter.Fill(dsName);

I try this: nameTextBox.Text = dsName.ToString(); but its wrong!

And I have to do the above for every customer details such as name, mobile number, home number ..etc

So you can see I need a solution plzz.

Candela  Tuesday, September 12, 2006 9:47 PM

add a databinding for each textbox:

this.theName.DataBindings.Add("Text", dsName.Tables[index], "name");

this.thePhoneNumber.DataBindings.Add("Text", dsName.Tables[index], "phoneNumber");

etc...

you will need to replace the textbox property names as well as the field names for the last parameter of the databindings.Add property

ahmedilyas  Tuesday, September 12, 2006 10:11 PM

add a databinding for each textbox:

this.theName.DataBindings.Add("Text", dsName.Tables[index], "name");

this.thePhoneNumber.DataBindings.Add("Text", dsName.Tables[index], "phoneNumber");

etc...

you will need to replace the textbox property names as well as the field names for the last parameter of the databindings.Add property

ahmedilyas  Tuesday, September 12, 2006 10:11 PM

Hi again

Could you please tell me how can I use index, I meanas long as its already assigned with the enteredID position?

Candela  Tuesday, September 12, 2006 11:33 PM

the index is just the table index, so if you filled 1 table, the index would be 0:

theDataSet.Tables[0]

ahmedilyas  Tuesday, September 12, 2006 11:34 PM

Hi again

I need you to clearify this to me because I didn't understand.

I use C# 2005 and this is my first time with it.. I did as you say ,wrote :

nameTextBox.DataBinding.Add("Text", dsName.Tables[0], "name");

mobileTextBox.DataBinding.Add("Text", dsMobile.Tables[0], "mobile");

...etc

where name, mobile used are the same variables I am using in my database.

Is that the right method? because I am not getting any data from the database.

I made sure the enteredID has a value and btw its identified as an integer.

And I have 3 databases but I am only dealing with one "Customer" concerning these textBoxes.

Please help me on this :(

 

Candela  Wednesday, September 13, 2006 10:03 PM

well is there any data in the dataset at all? (dsMobiles)

It could be simply because you have quite a few records also, and you need to get just 1 record in order to specify it to the textbox data bindings as if there are several then it may not show them as it needs to know which record. So if you select only 1 record (in the OleDbCommand query..."SELECT................ WHERE [ID] = IDHERE") - then try the above.

ahmedilyas  Wednesday, September 13, 2006 10:32 PM

Thanks 4 ur effort in helping me..

yes I will show several data in one record but I declared a different command to each such as

...("select name ... where ....=" + enteredID, myConn)

...("selectmobile ... where ....=" + enteredID, myConn)

...("selecthome ... where ....=" + enteredID, myConn)

I declared one connection to all but each has different dataSet, dataAdapter and Command.

I am still struggling in the same issue since 2 days and no result is shown ,,,sadly :(

I am confused

Candela  Wednesday, September 13, 2006 11:12 PM
post the entire code including retrieving data and storing it into a dataset and the code used to bind the data to the textbox
ahmedilyas  Wednesday, September 13, 2006 11:23 PM

oleDbConnection myConn = new oleDbConnection (myStr);

//////Name////////

oleDbCommand nameCmnd = new oleDbCommand( "select name from Customer where customerID = "+ enteredID, myConn);

oleDbDataAdapter nameAdapter = new oleDbDataAdapter(nameCmnd);

DataSet dsName = new DataSet();

nameAdapter.SelectCommand = nameCmnd;

nameAdapter.Fill(dsName);

nameTextBox.DataBinding.Add("Text", dsName,Tables[0], "name");

//////Mobile////////

oleDbCommand mobileCmnd = new oleDbCommand( "select mobile from Customer where customerID = "+ enteredID, myConn);

oleDbDataAdapter mobileAdapter = new oleDbDataAdapter(mobileCmnd);

DataSet dsMobile = new DataSet();

mobileAdapter.SelectCommand = mobileCmnd;

mobileAdapter.Fill(dsMobile);

mobileTextBox.DataBinding.Add("Text", dsMobile,Tables[0], "mobile");

//////Phone////////

oleDbCommand phoneCmnd = new oleDbCommand( "selectphone from Customer where customerID = "+ enteredID, myConn);

oleDbDataAdapter phoneAdapter = new oleDbDataAdapter(phoneCmnd);

DataSet dsPhone = new DataSet();

phoneAdapter.SelectCommand = phoneCmnd;

phoneAdapter.Fill(dsPhone);

phoneTextBox.DataBinding.Add("Text", dsPhone,Tables[0], "phone");

/////////////////////

Thats the code regarding the data binding and storing in the dataset.

For enteredID value its declared as a public int in this form but the value got from another form and I did check the value and its stored properly.

Candela  Wednesday, September 13, 2006 11:40 PM

seems ok to me, be sure data does exist. This code here works for me, with the addition of not having multiple queries being executed, this is ineffecient. Here is the correct way:

OleDbCommand theCommand = new OleDbCommand("SELECT * FROM someTable");

theCommand.Connection = new OleDbConnection(connectionString);

DataSet theDataSet = new DataSet();

OleDbDataAdapter theAdapter = new OleDbDataAdapter(theCommand);

theAdapter.Fill(theDataSet);

DataView theDataView = new DataView(theDataSet);

theDataView.RowFilter = "[ID] = 12";

this.txtName.DataBindings.Add("Text", theDataView, "Name");

this.txtPhoneNumber.DataBindings.Add("Text", theDataView, "PhoneNumber");

and so on....

does this not work for you?

ahmedilyas  Wednesday, September 13, 2006 11:53 PM

Hi ahmed

Thanks for your effort in helping me

Appearantly the problem was that the customerID is an autoNumber inthe Customertable and I formated it in access to start by 1000 so the first field will be 1001

But when I checked the table in C# 2005 the customerID had been set to increment in a 0 base as 1,2,3.. etc

If I used that format the data will be shown else it wont and I am now puzzled of how to show the data exactly as it is in my access table.

Candela  Friday, September 15, 2006 8:37 PM

You can use google to search for other answers

Custom Search

More Threads

• VS2005 CryptographicException when adding SqlDataAdapter
• Binding comboBoxes and TextBoxes on a relation
• Exception deleting a row from DataGrid bound to an ArrayList
• DataGridViewComboBoxColumn synchronization problem
• Binding custom objects to Data Grids
• SQL tableAdapter help...
• BindingSource AddNew with grandchild table
• ListBox
• Windows Forms Datagrid Red Cross Issue
• Adding to database