Reputation: 105
I am trying to concat two columns but something is going wrong.... my output is not displayed .
String Query = " SELECT pa_forename , pa_surname FROM [ICPS].[dbo].[parking_attendants] order by pa_forename ";
SqlConnection conDataBase = new SqlConnection(conString);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
comboBox1.Items.Add(pa_forename);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Upvotes: 1
Views: 1564
Reputation: 12884
You are doing it wrong,
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
You cannot get two columns from DataReader
at the same time. Replace your code with something like this. The above code will try to look for column that doesn't exists.
string pa_forename = myReader["pa_forename"] +"," + myReader["pa_surname"];
Upvotes: 0
Reputation: 46005
replace
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
with
string pa_forename = myReader["pa_forename"] +"," + myReader["pa_surname"];
Upvotes: 7