I have basically a combobox and and a text box. What should i do is the combobox consist of subject code whereas the textbox consist of subject name which is stored in database. Can i know is there any ways that when an item from combobox is chosen, it is automatically display subject name based on subject code chosen which will be from the database. belwo is my code but not working

private void FormAttendanceFaculty_Load(object sender, EventArgs e)

            con.Open();
            OleDbDataAdapter oda1 = new OleDbDataAdapter("select code from subject where code like '%'", con);
            DataTable dt1 = new DataTable();
            oda1.Fill(dt1);
            comboBoxCode.DataSource = dt1;
            comboBoxCode.DisplayMember = "code";
            comboBoxCode.SelectedIndex = -1;

string myquery = "SELECT name FROM subject where name = '"  +comboBoxName.Text + "'";

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

}

Recommended Answers

All 2 Replies

I don't know what ExecuteScalar() is, but should you not have a reader for a db query?

When the query returns a single value then you can use ExecuteScalar and do not require a reader.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.