简体   繁体   中英

combine 3 text from database to fill combobox

i have 3 columns fname = first name , mname = middle name and sname = surname so i have to combine them to fill a single combo box

var query = "SELECT CONCAT(fname, ' ', mname, '', sname ) AS FullName FROM database.table;";


   using(MySqlConnection myConn = new MySqlConnection(ConnectionClass.GetConnection()))
 using(MySqlCommand cmDB = new MySqlCommand(query, myConn))
{
    try
    {
        myConn.Open();
        using(MySqlDataReader myReader = cmDB.ExecuteReader())
        {
            while(myReader.Read())
            {
                string FName = myReader.GetString("FullName");
                cmbName.Items.Add(FName);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        myConn.Close();
    }
}

i tried calling FullName a several time but it kept saying it cannot find the field so how do i do this?

FullName is an alias rather than an existing filed name on your table, that's why sqldatareder cant recognize it. Try:

while(myReader.Read())
{
   string FName = myReader[0].ToString();
   cmbName.Items.Add(FName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM