0

using query i have called two columns value from database into one column. the point is now i want to select a value form combobox and put one column value into textbox.

e.g

two column values from database into combobox below

10001 haider <------ when i select this index i want only haider to be viewed into the textbox

10002 fahad

10003 aitazaz

the snippet which i have used for calling the two colums value from database is:

public void account()
        {
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT acc_no, acc_name FROM accounts_record";
            MySqlDataAdapter adpt = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adpt.Fill(ds);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                cbacc.Items.Add(ds.Tables[0].Rows[i][0] + "   " + ds.Tables[0].Rows[i][1]);
            }

            con.Close();
        }
Haider Khattak
  • 567
  • 3
  • 8
  • 32
  • 1
    To avoid keeping the connection open I recommend moving the for loop after the connection is closed – TGH Feb 14 '14 at 05:11

4 Answers4

1

You should be adding values and text to the combobox separately. Here's an example ComboBox: Adding Text and Value to an Item (no Binding Source).

If you have to display the id in the text you have to do some parsing before putting the selected text into the textbox.

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
  • The "10001" given in the example is probably not an id. If you check the select statement, the column name is "acc_no" which probably means account number, hence a property of a business entity – samar Feb 14 '14 at 05:29
0

If you are able to get the 2 value text of the combo box on selection change then you can split it with space (" ") character and take the 2nd string and put it in textbox.

For example

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] splitedStr = comboBox1.SelectedText.Split(' ');
        textBox1.Text = splitedStr[1];
    }
samar
  • 5,021
  • 9
  • 47
  • 71
0

Use ComboBox.SelectedIndexChanged event of the combobox to populate the textbox accordingly. use http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx

turbo88
  • 423
  • 2
  • 8
  • 23
0

You should use the code as below

   private void Form1_Load(object sender, EventArgs e)
    {
            string conString = "Data Source=\\SQLEXPRESS;Initial Catalog=Test;Integrated                 Security=True";
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT acc_no +'-' + acc_name as AccNoWithName , acc_no as ActNo FROM accounts_record";
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataSet dsn = new DataSet();
            adpt.Fill(dsn);
            con.Close();

            comboBox1.DisplayMember = "AccNoWithName";
            comboBox1.ValueMember = "ActNo";
            comboBox1.DataSource = dsn.Tables[0];
   }


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
            textBox1.Text = comboBox1.SelectedValue.ToString();
 }
Keshavdas M
  • 674
  • 2
  • 7
  • 25
  • I am happy to help you but can you please let me know what you doing or want to do in between the code that I have provide you above. – Keshavdas M Feb 14 '14 at 09:01
  • i get this data in the textbox and the combobox "System.Data.DataViewManagerListItemTypeDescriptor" – Haider Khattak Feb 14 '14 at 09:04
  • the main purpose of this is that i want to get the data from two fields in the combobox and when i click the item from combobox i want to get only the acc_name against the acc_num @keshavdas – Haider Khattak Feb 14 '14 at 09:08
  • See above the entire code block that I have tried and working fine. Just use and update appropriate object at your end and all should work well. – Keshavdas M Feb 14 '14 at 09:13