[Solved] Writing information from SQL Server to C#


Your query executes an INSERT (IE. Adds data to your table) It doesn’t retrive any record. The INSERT statement in T-SQL could use a SELECT subquery to feed the values that are requested by the columns listed after the INSERT.

So your query add a new record every time you run it, but there is no column returned from that query and using the reader indexer on a non existant column produces the mentioned error.

If you just want to read values then you should change your query to

try
{
    using(SqlConnection connect = new SqlConnection(....))
    using(SqlCommand command = new SqlCommand(
        @"SELECT FILE_DATE_PROCESSED, DATE_ENTERED FROM FILE_DATE_PROCESSED", connect))
    {
        connect.Open();
        using(SqlDataReader reader = command.ExecuteReader())
        {
           while (reader.Read())
           {
                 Console.WriteLine(reader["FILE_DATE_PROCESSED"].ToString());
                 Console.WriteLine(reader["DATE_ENTERED"].ToString());
           }
        }
   }
}
catch (Exception e)
{
   Console.WriteLine(e.ToString());
}

I suggest also to change the column name FILE_DATE_PROCESSED or the table name because having two objects with the same name could be an endless source of confusion and an excellent error maker

6

solved Writing information from SQL Server to C#