Last post Jun 07, 2016 08:42 AM by Fei Han - MSFT
Jun 06, 2016 01:55 PM|missy786|LINK
I am trying to run this method but i keep getting the following error on the <List> variable. is there a way to resolve this?
System.Data.SqlClient.SqlDataReader' does not contain a definition for 'ToCSV' and no extension method 'ToCSV' accepting a first argument of type 'System.Data.SqlClient.SqlDataReader' could be found (are you missing a using directive or an assembly
public static string userListCSV()
string name = "";
string surname = "";
string email = "";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["######"].ConnectionString);
string commandtext = "select uForenames, uSurname, uEmailAddress from mpl_UserDetails";
SqlCommand command = new SqlCommand(commandtext, con);
command.Parameters.Add(new SqlParameter("uForenames", name));
command.Parameters.Add(new SqlParameter("uSurname", surname));
command.Parameters.Add(new SqlParameter("uEmailAddress", email));
SqlDataReader reader = command.ExecuteReader();
List<string> rows = reader.ToCSV(false, ",");
var result = "";
foreach (string row in rows)
result += row + "\n";
error on line --> List<string> rows = reader.ToCSV(false, ",");
Jun 06, 2016 02:18 PM|Nataraj Gandhi Arunachalam|LINK
It seems you will have to implement the ToCSV extension method first. For implementing this extension method, please refer to the below listed examples,
Jun 06, 2016 02:28 PM|mithun.n33|LINK
To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object,
instead of directly using a constructor.
IsClosed and RecordsAffected are
the only properties that you can call after the SqlDataReader is closed.
Jun 07, 2016 08:42 AM|Fei Han - MSFT|LINK
System.Data.SqlClient.SqlDataReader' does not contain a definition for 'ToCSV' and no extension method 'ToCSV' accepting a first argument of type 'System.Data.SqlClient.SqlDataReader' could be found (are you missing a using directive or an assembly reference?)
Firstly, I’m glad to hear that you resolve the issue with the help of
. As for the issue, if you check “SqlDataReader Class”, you will find SqlDataReader object does not support ToCSV() method.
and we could loop through columns and rows of DataTable and dynamically export data from DataTable to CSV file.