I have two fields (FirstName and LastName) in a table which I want to present in a listbox. I just seem to be able to put either FirstName or LastName in the listbox.
I'am using a sqlDataReader object till fill the listbox.
I tried: ddlEmployee.DataTextField = "FirstName" + "LastName";
I'am getting a complie error when I do that.
Thanks in advance,
Here's my code:
private void LoadDDLUSer()
{
string sql = @"SELECT fkCompany, FirstName, LastName FROM [Users] WHERE fkCompany = " + "'" + ddlCompany.Text + "'" + " ORDER BY FirstName"; SqlConnection con = new SqlConnection("ConnectionString") SqlCommand cmd = new SqlCommand(sql, con);con.Open();
try{
SqlDataReader reader = cmd.ExecuteReader();ddlEmployee.DataSource = reader;
ddlEmployee.DataTextField =
"FirstName";ddlEmployee.DataValueField =
"fkCompany";ddlEmployee.DataBind();
}
finally{
con.Close();
ddlEmployee.Items.Insert(0,
"");}
}
Larry HojerPosted Jun 18, 2007, 3:54 PM
Thanks again,
AlanPosted Jun 14, 2007, 6:01 AM
You'd need to replace:
FirstName, Lastname
in your query with:
FirstName + ' ' + LastName As 'FullName'
and then replace:
ddlEmployee.DataTextField = "FirstName";
with:ddlEmployee.DataTextField = "FullName";