Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
populating the combox and displaying the value in the combobox
for eg. if I choose name in the combobox the corresponding ID should display in the text box . Do anybody know? I am learning c#. I tried it can anybody examine the following code.

After reading I tried to put it in a Binding list array and then I said
combobox1.datasorce = the new variable

Pl. examine the below code.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace DBExample
{
    public partial class Form1 : Form
    {
        private OleDbConnection dbConn; // Connectionn object
        private OleDbCommand dbCmd;     // Command object
        private OleDbDataReader dbReader;// Data Reader object
        private Member aMember;
        private string sConnection;
        // private TextBox tb1;
        // private TextBox tb2;
        private string sql;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // Construct an object of the OleDbConnection 
                // class to store the connection string 
                // representing the type of data provider 
                // (database) and the source (actual db)
                sConnection =
                    "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=c:member.mdb";
                dbConn = new OleDbConnection(sConnection);
                dbConn.Open();
                // Construct an object of the OleDbCommand 
                // class to hold the SQL query. Tie the  
                // OleDbCommand object to the OleDbConnection
                // object
                sql = "Select * From memberTable Order " +
                      "By LastName , FirstName ";
                dbCmd = new OleDbCommand();
                dbCmd.CommandText = sql;
                dbCmd.Connection = dbConn;
                // Create a dbReader object 
                dbReader = dbCmd.ExecuteReader();
                while (dbReader.Read())
                {
                    aMember = new Member
                            (dbReader["FirstName"].ToString(),
                             dbReader["LastName"].ToString(),
                             dbReader["StudentId"].ToString(),
                             dbReader["PhoneNumber"].ToString());
                    // tb1.Text = dbReader["FirstName"].ToString();
                    // tb2.Text = dbReader["LastName"].ToString();
                    // tb1.Text = aMember.X().ToString();

                    //tb2.Text = aMember.Y(aMember.ID).ToString();  
                    BindingList<Member> comboBoxList = new BindingList<Member>();
                    
                    comboBox1.DataSource = comboBoxList; 
                    comboBox1.DisplayMember = "FirstName"; 
                    comboBox1.ValueMember = "ID";
                   // this.comboBox1.Items.Add("aMember.FirstName.ToString()");
                    // this.listBox1.Items.Add(aMember.ToString());
                    // MessageBox.Show(aMember.ToString());
                    // Console.WriteLine(aMember.ToString());
                }
                dbReader.Close();
                dbConn.Close();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show("show" + exc);
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         // textBox2.DataBindings.Add("Text", aMember, "aMember.ID");
            textBox2.Text = comboBox1.SelectedValue.ToString();
            this.textBox1.Text = comboBox1.SelectedItem.ToString();
            //int[] item = (int[])comboBox1.SelectedItem;
            //textBox2.Text = string.Empty;
           // foreach (int i in item)
           //    textBox2.Text += item.ToString();
            
}
         
  
        private void DbGUI_Load(object sender, EventArgs e)
        {
        }

        

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
           
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
    }



}
Posted
Updated 13-Mar-11 18:32pm
v2
Comments
rajivpande86 13-Mar-11 21:17pm    
Can you post the specific error you're getting or the behavior your code shows..That may help to track it..
Mary Abraham 13-Mar-11 21:31pm    
Eliminated the error. It was out of scope error.

You should modify your code like this,
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         if (comboBox1.SelectedIndex > 0)
          {

         // textBox2.DataBindings.Add("Text", aMember,     "aMember.ID");
            textBox2.Text = comboBox1.SelectedValue.ToString();
            this.textBox1.Text = comboBox1.SelectedItem.ToString();
            //int[] item = (int[])comboBox1.SelectedItem;
            //textBox2.Text = string.Empty;
           // foreach (int i in item)
           //    textBox2.Text += item.ToString();
          }
}


Best Regards,
Theingi Win
 
Share this answer
 
Comments
Raj.rcr 14-Mar-11 1:39am    
Simple answer...my 5
Hi

Move this before the while loop

BindingList<member> comboBoxList = new BindingList<member>();</member></member>

Binding list is a signle instance, no need to instantiate again and again inside the loop

inside the while loop get this

C#
Member mem1 = new Member("Student A", "Student A", 1, "XXXXXXX");
Member mem2 = new Member("Student B", "Student B", 2, "XXXXXXX");

comboBoxList.Add(mem1);
comboBoxList.Add(mem2);


You have to populate the binding list with members

Then out side the while loop assign this to the combobox

C#
comboBox1.DataSource = comboBoxList;
comboBox1.DisplayMember = "FirstName";
comboBox1.ValueMember = "ID";


Then in the Selection Change Event

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Member mem=(Member)comboBox1.SelectedItem;
    textBox1.Text = mem.FirstName;
}




What you are doing is creating a binding list, but that binding list is empty.
 
Share this answer
 
Hi
Please try the below code block


C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         // textBox2.DataBindings.Add("Text", aMember, "aMember.ID");
            textBox2.Text = comboBox1.SelectedValue.ToString();
            this.textBox1.Text = comboBox1.Text;
            //int[] item = (int[])comboBox1.SelectedItem;
            //textBox2.Text = string.Empty;
           // foreach (int i in item)
           //    textBox2.Text += item.ToString();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900