Click here to Skip to main content
13,141,747 members (59,582 online)
Rate this:
 
Please Sign up or sign in to 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 24-Feb-13 23:23pm
jmpapa445
Updated 24-Feb-13 23:29pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

hi buddy

this will that you want....
just set properties as per requirement of yours..
 
 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
  Permalink  
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
jmpapa 25-Feb-13 7:34am
   
it's just the same with the code you posted above. The error is in this:

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

I've also tried to use "firstname" in exchange to lastname, but still got the same error.
Shubh Agrahari 25-Feb-13 8:09am
   
buddy one thing is that if you have 2 column one is firstname and 2nd is lastname then you concating it by this query "select CONCAT(1stname, ' (' ,lastname,')') AS mixname from userdbase " then now you have to write this
this.ListBox1.DataTextField = "userdbase.mixname ";
this.ListBox1.DataValueField = "userdbase.mixname";
mixname is a virtual name that making to column as one at runtime.....
Shubh Agrahari 25-Feb-13 8:11am
   
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", "userdbase");
da.Fill(ds, "userdbase");
this.ListBox1.DataSource = this.ds;
this.ListBox1.DataTextField = "userdbase.mixname ";
this.ListBox1.DataValueField = "userdbase.mixname";
sqlcon.Close();
da.Dispose();


here userdbase is your database name and also mixname is a virtual name that making to column as one at runtime... try this
jmpapa 25-Feb-13 9:26am
   
finally, I made it! I used this code:

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();
}

It's a combination of your given code and my previous code. Thank you very much for your help and patience.
Shubh Agrahari 25-Feb-13 9:32am
   
yap its ma pleasure buddy but also nice attempt by you also....its good quality to give peek of potential for trouble shooting....go ahead nice career waiting for us....
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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();
            }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try This->
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();
                }
         }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100