0

I would really appreciate if there is any reference to show in how i could transfer value from database into textbox. This my code for combobox, but instead of combobox i would like in textbox

        OleDbDataAdapter oda = new OleDbDataAdapter("select subject_name from  subjecti where subject_name = '" + comboBoxSubjectName.Text + "'", con);
        DataTable dt = new DataTable();
        oda.Fill(dt);
        comboBoxSubjectCodeUpdate.DataSource = dt;
        comboBoxSubjectCodeUpdate.DisplayMember = "subject_name";
Software Engineer
  • 431
  • 1
  • 4
  • 18

1 Answers1

1

You can try something like this

string myquery = "SELECT MyColumn FROM MyTable";

using (var command = new OleDbCommand(myquery, connection))
{
  MyTextBox.Text = command.ExecuteScalar().ToString();
}

Please note if you will be returning multiple values then I suggest you use ExecuteReader() as ExecuteScalar() only returns a single value.

Izzy
  • 6,163
  • 5
  • 32
  • 73
  • thanks i try and see. Mope, i will only return a single value – Software Engineer Feb 24 '16 at 15:37
  • If it's single value then you can stick with `ExecuteScalar()` – Izzy Feb 24 '16 at 15:37
  • o..k.. thanks... i testing it now.. and reading on ExecuteScalar and ExecuteReader – Software Engineer Feb 24 '16 at 15:39
  • sir i having problem with this statement ommand.ExecuteScalar() which stating that cannot implictly convert a type object to string – Software Engineer Feb 24 '16 at 15:47
  • I've updated it, You need to use `.ToString()` that will return the string – Izzy Feb 24 '16 at 15:50
  • thanks sir it is working only i must fix my error and find reaosons when i put this comboBoxSubjectCodeRegister.SelectedIndex = -1; in combobox the output is an error which is "Object reference not set to an instance of an object." but if i remove the statement it display based on what specifies in the combox. So i will try find how can i put comboBoxSubjectCodeRegister.SelectedIndex = -1; without getting an error – Software Engineer Feb 24 '16 at 15:58
  • Read this [question](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) it will help you with the error – Izzy Feb 24 '16 at 16:00
  • You're welcome! Glad I could help. Good luck with your project – Izzy Feb 24 '16 at 16:01