asmgx
asmgx

Reputation: 8014

Why DataTable columns are read-only when loaded from CSV using FileHelpers?

Scenario 1 :

Loading datatable from SQL Server database is working fine.

I also can update data with no issues in that datatable:

SqlConnection conn = new SqlConnection(gDBConn);
SqlCommand cmd = new SqlCommand("ListData", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Parm1", "*"));

conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
cmd.Dispose();

dt.Select()[0]["Col1"]=9; //<--------- Works Fine

Scenario 2 :

Loading datatable from CSV file is problamatic.

I cannot update data in that datatable.

I get this error:

using (var reader = new StreamReader(path))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    using (var dr = new CsvDataReader(csv))
    {
        var dt = new DataTable();
        dt.Load(dr);
    }
}

dt.Select()[0]["Col1"]=9; //<--------- Cause error

threw an exception of type 'System.Data.ReadOnlyException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146232025
    HelpLink: null
    InnerException: null
    Message: "Column 'IncExcSug' is read only."
    Source: "System.Data"
    StackTrace: "   at System.Data.DataRow.set_Item(DataColumn column, Object value)\r\n
                    at System.Data.DataRow.set_Item(String columnName, Object value)"
    TargetSite: {Void set_Item(System.Data.DataColumn, System.Object)}

What is wrong with the loading from CSV and how can I make it updatable??

Data in the CSV and from DB look like this:

Col1       Col2        Col3           Col4
-------------------------------------------
1           A            2             M
1           H            6             R
1           K            3             K
3           A            6             D
5           K            3             R
5           E            1             D
8           D            0             I

Upvotes: 0

Views: 42

Answers (0)

Related Questions