0

Im using Visual Studio C# and using Access as my database. I have two columns, ItemCode and ProductName. I want to auto input of the productname on its textbox whenever its itemcode was selected. how can I do this?

some codes:

 try
 {
 con.Open();
 OleDbCommand command = new OleDbCommand(@"Select * from TblInventory where ItemCode='" + txtItem.Text + "'");
 command.Connection = con;
 command.Parameters.AddWithValue("itemcode", txtItem.Text);

 OleDbDataReader reader = command.ExecuteReader();
 if (reader.Read())//Update Item Code is already exist
    {
     .........

Feel free to edit my question and please be kind. thank you guys

Jonald Samilo
  • 297
  • 1
  • 2
  • 11
  • What you mean by *auto input of the productname whenever its itemcode was selected*, I hope that you want to update the product_name corresponds to the id in the `where` clause, isn't it? – sujith karivelil Aug 11 '16 at 02:39
  • yes i want to get the value of the product name and call it on my textbox which is corresponds to same row of that itemcode – Jonald Samilo Aug 11 '16 at 02:53
  • I would load at least the ItemCode and ProductName pair into a [DataTable](http://stackoverflow.com/questions/2374221/loading-access-db-table-to-datatable), then [bind](http://stackoverflow.com/a/5660011/529282) the DataTable to whatever controls you use for displaying & selecting itemcode & productname. Remember you can bind [one data source to multiple controls](http://stackoverflow.com/questions/8713218/one-datasource-for-multiple-controls). Adjust the DisplayMember as needed for each controls. – Martheen Aug 11 '16 at 02:56

2 Answers2

0

Try this .

text_box.Text=reader["ProductName"].ToString();

You are filtering the rows by specifying the ItemCode in the Where clause, So The reader will contains the corresponding row/s that matches the specified code. What you need to do is Access the required column value by specifying the name as like the above snippet.

lakshmi prakasan
  • 330
  • 4
  • 12
0

For extracting data from database, I prefer that use OleDbDataAdapter. You can simply use:

string command = @"Select * from TblInventory where ItemCode='" + txtItem.Text + "'";
OleDbDataAdapter da = new OleDbDataAdapter(command, con);
DataTable dt = new DataTable();
da.Fill(dt);

Now, use dt :

if (dt.Rows.Count == 0)
    //Error Message
else
    cmbTreatyTitle.Text = dt.Rows[0]["ProductName"].ToString();

I hope this is helpful.

Morteza.M
  • 177
  • 13