Reputation: 29
I am using a C# Winforms application. I want to concatenate three values from three different tables into a single label.
I have tried this line of code to get values from tables but it is not working.
this.label15.Text = reader["CourseName" + "TeacherName" +"RoomName"].ToString();
Kindly help me.
Upvotes: 0
Views: 69
Reputation: 1534
this.label15.Text = reader["CourseName" + "TeacherName" +"RoomName"].ToString();
- this is wron and weird because your result key in table is "CourseNameTeacherNameRoomName"
. Are you sure you have such a key in your tables somewhere?
To achive this just use one key name per access. Or, as sugessted by Mihail Stancescu in comments, you could use String.Concat
method
UPDATE
According to your question in comment, to show each of your values in different values you need String.Format
method:
String.Format("{0},\r\n{1},\r\n{2}", reader["CourseName"], reader["TeacherName"], reader["RoomName"])
- this is a case with return and new line carriages; or with Environment.NewLine
:
String.Format("{0},{3}{1},{3}{2}", reader["CourseName"], reader["TeacherName"], reader["RoomName"], Environment.NewLine)
Upvotes: 0
Reputation: 1104
You have to access your values individually from the reader
like so...
this.label15.Text = string.Format("{0}{3}{1}{3}{2}",
reader["CourseName"].ToString(),
reader["TeacherName"].ToString(),
reader["RoomName"].ToString(),
System.Environment.NewLine);
I'm also using string.Format()
to build the text you want and include the new lines which are represented by the {3} in the string and populated with System.Environment.NewLine
.
Upvotes: 0
Reputation: 94
The best solution for me (the most flexible) is to use string.Format(...) method.
Simply write:
this.label15.Text = string.Format("{0} {1} {2}", reader["CourseName"], reader["TeacherName"], reader["RoomName"]);
Upvotes: 0
Reputation: 3752
Your code will try to find a column CourseNameTeacherNameRoomName
. You need to get the 3 values separately, and then concatenate them.
this.label15.Text = reader["CourseName"].ToString() + reader["TeacherName"].ToString() + reader["RoomName"].ToString();
Upvotes: 1