Inserting a row - not working

  • Thread starter Thread starter JamesB
  • Start date Start date
J

JamesB

Not sure if you'll be able to help as I'm using an inherited database
connection that one of our devs wrote... but in any case...
I'm trying to add a new row to a table, using the following code (the below
is simplified)

DataTable tblImport = myConn.OpenTable("SELECT * FROM table);
DataRow NewRow = tblImport.NewRow();

NewRow["fieldA"] = "foo";
NewRow["fieldB"] = "bar";

NewRow.AcceptChanges();
tblImport.AcceptChanges();

After all this, my data never appears in the database. There are no errors
etc (i've removed the try-catch lines to see if it bombs out, but runs
through fine).
Is that methodology correct though? Or do I need to have a dataadapter in
there etc?
 
Hi James,

You have a couple of issues.
1. After you've populated new row, you have to add it to rows in table,
i.e.: tblImportn.Rows.Add(NewRow);
2. You shouldn't call AcceptChanges on any instance - if you do, you are
practially saying that there is no need to update anything in database
3. You have to persist changes in dataset (Datatable) to database by calling
adapter.Update on that table.
 
Miha Markic said:
Hi James,

You have a couple of issues.
1. After you've populated new row, you have to add it to rows in table,
i.e.: tblImportn.Rows.Add(NewRow);
2. You shouldn't call AcceptChanges on any instance - if you do, you are
practially saying that there is no need to update anything in database
3. You have to persist changes in dataset (Datatable) to database by
calling adapter.Update on that table.

Handy pointers- will have a play and see how I get on.
Thanks
 
Back
Top