Workershere Workershere - 1 month ago 4
ASP.NET (C#) Question

Cannot convert char to string C#

List<string>[] list = new List<string>[4];//here i have showed usage of only 1
list[0] = new List<string>();
list[0].Add(dataReader["id"]+ "");


So the value in list[0] becomes (1,2,3,4) which is obtained from the database.
now i want to get value of 1 that is the 1st element in the generic list

I do this==

string combindedString = string.Join(",", list[0].ToArray());
Label3.Text = combindedString[0];//ptting the text on a label


//here i get an error that can't convert char to string how do i access 1st 2nd 3rd element of the list that is 1 , 2 , 3 ,4 ?

Answer

As per your code you try to add List at the 0th position of your first List.

List<string>[] list = new List<string>[4];//here i have showed usage of only 1
list[0] = new List<string>();
list[0].Add(dataReader["id"]+ "");

Label3 takes string input that is why Label3.Text(takes string input).

combindedString is a string, so combindedString[0] will be considered a char.

By assigning char to a string explicitly , it causes error .

Follow me now:

Label3.Text = combindedString[0].ToString();//Now a string 
Comments