Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me display data into my combo box when a search button is clicked

C#
private void lklSearch_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
   conn.Open();
   try
   {
      Personal person = new Personal();
      if (person.searchpersonDetails(txtClientid.Text, txtSurname.Text))
      {
         var
         _with3 = this;
         string mstatus = person.statusMarital.ToString();
         switch (mstatus)
         {
            case "Single":
               this.cboMaritalstatus.Text = "Single";
               break;
            case "Married":
               _with3.cboMaritalstatus.Text = "Married";
               break;
            case "Widow(er)":
               _with3.cboMaritalstatus.Text = "Widow(er)";
               break;
            case "Divorce":
               _with3.cboMaritalstatus.Text = "Divorce";
               break;
         }

and in the class i have
C#
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
   marital_status = dr["marital_status"].ToString();


the program run but do not display the data in the combo box
Posted
Updated 23-Jan-12 7:40am
v2
Comments
Amir Mahfoozi 27-Jan-12 23:51pm    
WPF ? Winform ? WebApplication ?

You need to use data bindings. Bind your CB DataSource to recordset and set the DisplayMember and ValueMember.

Read Data binding concepts in .NET windows forms[^] and Data binding in WPF[^]
 
Share this answer
 
Hi,

Add items to your combo box in the below way in your data read,

comboboxid.add.items(marital_status);



or get a dataset and bind it to your combo box.like,

C#
// Setup the combobox view and display-, value member
comboboxid.DataSource = dsView;
comboboxid.DisplayMember = "CompanyName";
comboboxid.ValueMember = "CustomerID";


Hope this helps.

Also, check this link for data binding examples.
http://www.akadia.com/services/dotnet_databinding.html[^]
 
Share this answer
 
 
Share this answer
 
thanks to you all for your help.
i just added a line of code and it is working perfectly

_with3.cboMaritalstatus.Text = person.statusMarital.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