2
Answers

Concatinating string into a listbox text

Ask a question
Larry Hojer

Larry Hojer

17y
1.7k
1
Hi,

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, "");

}

}


Answers (2)