Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello! I have a database that has a firstname and lastname field. I want to retrieve the two fields and bind it into a listbox.

I've manage to bind the datas in a listbox with a single field only. With code like this:
void LoadGrid()
        {
            MySqlConnection sqlcon = new MySqlConnection(strConnString);
            sqlcon.Open();
            sqlcmd = new MySqlCommand("SELECT lastname from userdbase", sqlcon);
            da = new MySqlDataAdapter(sqlcmd);
            dt.Clear();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {

                ListBox1.DataSource = dt;
                ListBox1.DataTextField = "lastname";
                ListBox1.DataBind();
            }
        } 


But I don't know how to insert another field and concatenate it with this first one. Any help? Thanks.

Example:
firstname || lastname
----------------------
Kristine || Ramos
Mary || Mariano

Output in listbox will be:
Kristine Ramos
Mary Mariano
Posted
Updated 24-Feb-13 23:29pm
v2

hi buddy

this will that you want....
just set properties as per requirement of yours..
C#
try
               {

                   con.Open();
                   string str = "select CONCAT(1stname, ' (' ,lastname,')') AS mixname from userdbase where pfno='" + sd.epfno + "'";
                    da = new MySqlDataAdapter(str, con);
                   ds = new DataSet();
                   da.TableMappings.Add("table", "userdbase");
                   da.Fill(ds, "userdbase");
                   this.listBox1.DataSource = this.ds;
                   this.listBox1.DisplayMember = "userdbase.mixname ";
                   this.listBox1.ValueMember = "userdbase.mixname";
                   //listBox1.MultiColumn = true;
                  con.Close();
                  da.Dispose();

               }
               catch (Exception ex)
               {
                   MessageBox.Show("Error" + ex.Message);
               }


happy to help
 
Share this answer
 
v2
Comments
jmpapa 25-Feb-13 6:01am    
Thank you Shubh! I applied your code in mine. And I came up with this:

sqlcon.Open();
string str = "select CONCAT(firstname, ' (' ,lastname,')') AS mixname from userdbase";
da = new MySqlDataAdapter(str, sqlcon);
DataSet ds = new DataSet();
da.TableMappings.Add("table", "empdependentd");
da.Fill(ds, "empdependents");
this.ListBox1.DataSource = this.ds;
this.ListBox1.DataTextField = "empdependents.mixname ";
this.ListBox1.DataValueField = "empdependents.mixname";
sqlcon.Close();
da.Dispose();

However, there's an error with this line:
this.ListBox1.DataSource = this.ds;
in the ds to be specific. What may caused this?
Shubh Agrahari 25-Feb-13 6:09am    
what error showing??
Shubh Agrahari 25-Feb-13 6:12am    
tried with this...??
void LoadGrid()
{
MySqlConnection sqlcon = new MySqlConnection(strConnString);
sqlcon.Open();
sqlcmd = new MySqlCommand("select CONCAT(1stname, ' (' ,lastname,')') AS mixname from userdbase ", sqlcon);
da = new MySqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{

ListBox1.DataSource = dt;
ListBox1.DataTextField = "lastname";
ListBox1.DataBind();
}
}
jmpapa 25-Feb-13 7:03am    
Got this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'lastname'.
Shubh Agrahari 25-Feb-13 7:05am    
put your last one code here.......that showing error
I came up with this code. And it works fine.

MySqlConnection sqlcon = new MySqlConnection(strConnString);
            sqlcon.Open();
            string sqlcmd = "select CONCAT(firstname, ' (' ,lastname,')') AS mixname from userdbase"; 
            da = new MySqlDataAdapter(sqlcmd,sqlcon);
            dt.Clear();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {

                ListBox1.DataSource = dt;
                ListBox1.DataTextField = "mixname";
                ListBox1.DataBind();
            }
 
Share this answer
 
Try This->
C#
void LoadGrid()
        {
                MySqlConnection sqlcon = new MySqlConnection(strConnString);
                sqlcon.Open();
                sqlcmd = new MySqlCommand("SELECT (ISNULL(firstname, '') + '' + ISNULL(lastname,         '')) FROM userdbase", sqlcon);
                da = new MySqlDataAdapter(sqlcmd);
                dt.Clear();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
 
                  ListBox1.DataSource = dt;
                  ListBox1.DataTextField = "lastname";
                  ListBox1.DataBind();
                }
         }
 
Share this answer
 
v3
Comments
jmpapa 25-Feb-13 7:02am    
got this error:
Incorrect parameter count in the call to native function 'ISNULL'

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