Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//I can't use the two attributes of the query as variables. Only one attribute is accommodated.


C#
 string myConn = "Server=localhost; user id = root; database = a_project";
//Eventhough my program runs with no errors when I use this string as query, but I can't use both attributes on the query (dBuildings, dID).
//string dQuery = "select building as dBuildings, id as dId from buildings order by building";
            string dQuery = "select building as dBuildings from buildings order by building";

            MySqlConnection dConnect = new MySqlConnection(myConn);
            MySqlCommand dCommand = new MySqlCommand(dQuery, dConnect);
            MySqlDataReader dReader;
            try
            {
                dConnect.Open();
                dReader = dCommand.ExecuteReader();
                while (dReader.Read())
                {
                    string dBuilding = dReader.GetString("dBuildings");
                    comboBox1.Items.Add(dBuilding);
                }
                dConnect.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
Posted
Updated 8-Feb-15 23:00pm
v3
Comments
Sinisa Hajnal 9-Feb-15 2:11am    
Why can't you use two fields? Do you get some error or something? Please provide more details, there is no reason you couldn't use any number of fields - there are plenty of examples on the net and in MSDN documentation. You're doing something wrong.
dReader.GetString("dBuildings");
dReader.GetString("dId"); should work

Also, please edit the question and leave only relevant code. Thank you.
Suvendu Shekhar Giri 9-Feb-15 2:44am    
Pasting the complete code here will not help. It's a community site, so come with the portion of code with the issue.

1 solution

In your query the fieldname is building, so GetString("building") if the fieldname to call from the while (dReader.Read()) part
 
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