I'm populating a dropdown with this method:
private DataTable PopulateDropdown(string connectionString, DataTable datatable, string query, DropDownList myDropDownList, string DataTextField, string DataValueField)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(query, con);
adapter.Fill(dt);
myDropDownList.DataSource = dt;
myDropDownList.DataTextField = DataTextField;
myDropDownList.DataValueField = DataValueField;
myDropDownList.DataBind();
}
catch (Exception ex) { }
}
return dt;
}
This is great as it allows me to get any database field simply by changing the query. Example:
string query = "select firstName, id from table";
PopulateDropdown(constring, dt, query, aspdropdown, "FirstName", "id");
However I need to be able to get two fields from the database and join them together, before populating the dropdownlist. I tried using:
string query = "select firstName, lastName, id from table";
PopulateDropdown(constring, dt, query, aspdropdown, "FirstName", "id", JoinedString: "lastName");
string JoinedString
was added as another parameter and included in the method like this:
myDropDownList.DataTextField = DataTextField + JoinedString;
Unfortunately this just gave me an empty dropdown list. I don't really know how else to approach this. Can anyone show me how can I get it working?