Reputation: 1655
Not sure what I am doing wrong here. I selected date data from an sql server using the select statement below. I then wanted to add it to a list. I have checked the datatype using System.Type and it tells me the values I'm getting back are System.String. So I thought this should be able to add it to a list of type String but I keep getting the error:
Cannot implicitly convert type System.Collections.Generic.List<string>
to string
public String getListFromColumnAsDATE(String columnName, String database)
{
myCommand = new SqlCommand("SELECT [Date] = convert(char(10), getdate(), 103) FROM Weekly_Tannery_Data", myConnection);
List<String> graphList = new List<String>();
myConnection.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
graphList.Add(myReader.GetString(0));
}
myConnection.Close();
return graphList;
}
Upvotes: 1
Views: 6754
Reputation: 5945
The reason behind this error comes from the fact that you have a return value of type List<String>
when your method declaration says it should return a String
.
The compiler is informing you that it can't convert an object of type List
with the deferred type String
into a String
. This is normal because it has no way of doing so on its own. Depending on your context, you either need to switch the return value type in the declaration of the method as below or convert your list into a string like canon was suggesting.
public List<String> getListFromColumnAsDATE(String columnName, String database)
{
myCommand = new SqlCommand("SELECT [Date] = convert(char(10), getdate(), 103) FROM Weekly_Tannery_Data", myConnection);
List<String> graphList = new List<String>();
myConnection.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
graphList.Add(myReader.GetString(0));
}
myConnection.Close();
return graphList;
}
Little side note:
I would humbly suggest adding a using statement for your SqlCommand
and the reader (myReader = myCommand.ExecuteReader();
) since they implement IDisposable
.
Upvotes: 8