We help IT Professionals succeed at work.
Get Started

DB Query to populate dropdown C#

darrennelson
darrennelson asked
on
313 Views
Last Modified: 2012-05-12
I've got a function that grabs a list of storeID's to populate a dropdown.  I'd like to change the sql statement to grab both storeID and storeName so that the contents of the drop down show the storeName appended to the storeID.  If I change the sql statement to read:

select storeid,storename from storemaster where...

what do I need to do to put both values into "storeID"?

public static List<string> GetStoresCO()
        {
            List<string> oStores = new List<string>();
            string sql = "select storeid from storemaster where StoreType = 'CO' and StoreOpen = 1 order by storeid";
            string conString = ConnectStringDB.connectionString;
            System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(conString);
            System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(sql, myConnection);
            System.Data.SqlClient.SqlDataReader myReader;
            try
            {
                myConnection.Open();
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    string store = myReader["storeid"].ToString();
                    oStores.Add(store);

                }
                myConnection.Close();
            }
            catch (System.Exception ex)
            {
                string st = ex.Message;
            }
            return oStores;
        }

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE