Inserting a row - not working

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?
 
M

Miha Markic [MVP C#]

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.
 
J

JamesB

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top