We help IT Professionals succeed at work.

DB Query to populate dropdown C#

darrennelson
darrennelson asked
on
Medium Priority
303 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:
Change your select to this.  

SELECT storeid + ' ' + storename AS storeid

Commented:
You might have to do it this way if storeID is an int

SELECT CAST(storeid AS varchar(10)) + ' ' + storename AS storeid

Author

Commented:
no need to cast...thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.