Reputation: 15
I want to create an application which will create a .CSV
file with data from my database but I get this error :
System.IndexOutOfRangeException : 'CT_Num'
And when I remove :
command.CommandText = "SELECT [Actionnaire Pal] FROM f_comptet";
and
dataReader["[Actionnaire Pal]"].ToString()
I don't get any errors anymore.
class Program
{
static void Main(string[] args)
{
SqlConnection conn = DBUtils.GetDBConnection();
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT CT_Num, [Actionnaire Pal] FROM F_COMPTET WHERE CG_NumPrinc = '4110000'";
SqlDataReader dataReader = command.ExecuteReader();
StreamWriter sw = new StreamWriter("C:\\facture.csv");
while (dataReader.Read())
{
sw.WriteLine("FAAV" + ";" + "." + ";" + "02" + ";" + "JJ/MM/AAAA" + ";" + "A" + ";" + "11" + ";" + dataReader["CT_Num"].ToString() + dataReader["[Actionnaire Pal]"].ToString() + ";");
}
sw.Close();
}
}
Upvotes: 0
Views: 107
Reputation: 61784
Change
dataReader["[Actionnaire Pal]"]
to
dataReader["Actionnaire Pal"]
The [
and ]
just tell SQL to make a column name from everything within them, and disregard the spaces (which normally it would consider to denote the start of the next part of the statement). They don't actually form part of the final column name.
Upvotes: 1