• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 62
  • Last Modified:

C# combox box showing array instead of Data

Hi, I used following code to add data to Combo box, But combobox showing results
String[] Array
Where is wrong
public List<string[]> Displayadd()
        {
            List<string[]> lstpartners = new List<string[]>();
         
            string sqlPartners = "SELECT name ,address from test";

            string results = string.Empty;
            PartnerCon=dbConnect.Connect();
            PartnerCon.Open();
            SqlCommand cmdPartners = PartnerCon.CreateCommand();
            cmdPartners.CommandText = sqlPartners;

            using (SqlDataReader rdr = cmdPartners.ExecuteReader())
            {
                //List<string> mylist = new List<string>(new string[] 
                while (rdr.Read())
                {
                    lstpartners.Add(new string[] { rdr["address"].ToString(), rdr["name"].ToString() });


                }

            }


            PartnerCon.Close();



            return lstpartners;
        
        }

Open in new window


Calling Combobox

 cmbPartnerID.DataSource = null;
            Partner ptr = new Partner();
            cmbPartnerID.DataSource = ptr.DisplayAdd();
            cmbPartnerID.DisplayMember = "address";
0
ukerandi
Asked:
ukerandi
  • 3
1 Solution
 
Ryan ChongCommented:
first, you should call:

cmbPartnerID.DataSource = ptr.DisplayAdd();

Open in new window

but not:

cmbPartnerID.DataSource = ptr.Displayadd();

Open in new window


then make the changes below:

create a Partner Info class to handle the item

class PartnerInfo
    {
        public String Address { get; set; }
        public String Name { get; set; }

        public PartnerInfo(String Address, String Name)
        {
            this.Address = Address;
            this.Name = Name;
        }
    }

Open in new window


then:

public List<PartnerInfo> DisplayAdd()
        {
            List<PartnerInfo> lstpartners = new List<PartnerInfo>();

            string sqlPartners = "SELECT name ,address from test";

            string results = string.Empty;
            
            PartnerCon = dbConnect.Connect();
            PartnerCon.Open();
            SqlCommand cmdPartners = PartnerCon.CreateCommand();
            cmdPartners.CommandText = sqlPartners;

            using (SqlDataReader rdr = cmdPartners.ExecuteReader())
            {
                while (rdr.Read())
                {
                    lstpartners.Add(new PartnerInfo(rdr["address"].ToString(), rdr["name"].ToString()));
                }
            }

            PartnerCon.Close();
            return lstpartners;
        }

Open in new window


then in your binding:

cmbPartnerID.DataSource = null;
            Partner ptr = new Partner();
            cmbPartnerID.DataSource = ptr.DisplayAdd();
            cmbPartnerID.DisplayMember = "address";

Open in new window


to get your selected value:

PartnerInfo info = (PartnerInfo)cmbPartnerID.SelectedValue;
            MessageBox.Show(info.Address + ":" + info.Name);

Open in new window

0
 
Ryan ChongCommented:
do you need further assistance here?
0
 
Ryan ChongCommented:
as suggested solution meet the requirement
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now