Click here to Skip to main content
13,289,626 members (53,512 online)
Rate this:
 
Please Sign up or sign in to vote.
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, con))
{
  MyTextBox.Text = command.ExecuteScalar().ToString();
}
}


What I have tried:

I tried based on the code whic ilearn via online and msdn written. But still not working
Posted 24-Feb-16 7:38am
Updated 25-Feb-16 3:27am
v3
Comments
CHill60 24-Feb-16 12:57pm
   
Well one reason it might not work is that you are looking up the subject name "where name = " instead of "where code = ".
You should never use string concatenation to create your query - use Parameterised queries instead - see Query Parameterization Cheat Sheet - OWASP[^]
Nareen Nair 24-Feb-16 22:26pm
   
owh.. got it.. i try this and see what happening
Nareen Nair 24-Feb-16 22:27pm
   
yup this test code.. later once i complte then i will review to avoid sql injection
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Probably the simplest way is to utilise both the DisplayMember and the ValueMember properties of the ComboBox E.g.
private void FormAttendanceFaculty_Load(object sender, EventArgs e)
{
    con.Open();
    OleDbDataAdapter oda1 = new OleDbDataAdapter("select code, name from subject where code is not null", con);
    DataTable dt1 = new DataTable();
    oda1.Fill(dt1);
    comboBoxCode.DisplayMember = "code";
    comboBoxCode.ValueMember = "name";
    comboBoxCode.DataSource = dt1;
    comboBoxCode.SelectedIndex = -1;
}

Note that I'm retrieving both code AND name from the subject table and I've changed your
code like '%'
to check for not null. Also I'm setting the DataSource after I've set up the Display and Value members.

To change the contents of the TextBox when the ComboBox changes you could use the SelectedIndexChanged event but note that it is going to be called when you are loading up the ComboBox with data, so you need to handle that e.g.
private void comboBoxCode_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBoxCode.SelectedValue != null)
        MyTextBox.Text = comboBoxCode.SelectedValue.ToString();
    else
        MyTextBox.Text = "";
}

A better option is to use the SelectionChangeCommitted event (reference[^]) as that is only raised when the User selects the value in the ComboBox e.g.
private void comboBoxCode_SelectionChangeCommitted(object sender, EventArgs e)
{
    MyTextBox.Text = comboBoxCode.SelectedValue.ToString();
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

in this event
comboBoxName_SelectedIndexChangedlike this 
<pre lang="C#">private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e)
       {
           string myquery = &quot;SELECT name FROM subject where name = &#39;&quot; + comboBoxName.Text + &quot;&#39;&quot;;
 
       }</pre>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 25 Feb 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100