UNABLE TO ADD NEW ENTRY IN DATATABLE C#

C G E Mannerheim

Total Post:29

Points:203
Posted by  C G E Mannerheim
 1423  View(s)
Ratings:
Rate this:
Hi Expert,

i'm trying to add a new row to my datatable.

I've no error while running this code but nothing happened in my table.

Here's my code :

        string table = "`DONNEE ENTRANT`";
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * From " + table, _conn);
        OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter);

        DataTable dataTable = new DataTable(table);
        adapter.Fill(dataTable);
        DataRow row = dataTable.NewRow();

        row["CODE LIAISON"] = "TEST";

        dataTable.Rows.Add(row);
        dataTable.AcceptChanges();

        adapter.Update(dataTable);

Any idea what i'm doing wrong (in this way, I know that we can run command with oleDb but really don't like this way ...).

Thanks
  1. Dev Patel

    Post:26

    Points:182
    Re: Unable to add new entry in datatable c#

    Hi Expert,


    So remove this line:

    dataTable.AcceptChanges();

    because that's changing the RowState to Unchanged and you need it left at Added. Further, you need to make sure that your adapter has an InsertStatement defined so it can use it.

    Thanks in advance. 

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!