0

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
asmgx
  • 7,328
  • 15
  • 82
  • 143
  • 1
    @OlivierRogier I added the schema in the question. – asmgx May 23 '21 at 21:35
  • I wonder why would someone rate this question negatively and ask to close it?!!! i am asking for help and have my code and I don't know what would that who asked to close the question want !! – asmgx May 23 '21 at 21:36
  • 1
    @OlivierRogier same error – asmgx May 23 '21 at 21:42
  • 1
    Does this answer your question? [Why does the returned DataTable has readonly columns in FileHelpers](https://stackoverflow.com/questions/9220185/why-does-the-returned-datatable-has-readonly-columns-in-filehelpers/9249814) –  May 23 '21 at 21:44
  • @OlivierRogier that worked, please put the answer so I mark it as an answer – asmgx May 23 '21 at 22:05

0 Answers0