Link to home
Start Free TrialLog in
Avatar of darrennelson
darrennelson

asked on

DB Query to populate dropdown C#

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

ASKER CERTIFIED SOLUTION
Avatar of tim_cs
tim_cs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might have to do it this way if storeID is an int

SELECT CAST(storeid AS varchar(10)) + ' ' + storename AS storeid
Avatar of darrennelson
darrennelson

ASKER

no need to cast...thanks