|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Inserting a row - not workingconnection 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. -- Show quoteMiha Markic [MVP C#, INETA Country Leader for Slovenia] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "JamesB" <ja***@somewhere.com.net.com.net> wrote in message news:45d996c1$0$2455$db0fefd9@news.zen.co.uk... > 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? > "Miha Markic [MVP C#]" <miha at rthand com> wrote in message Handy pointers- will have a play and see how I get on.news:O%233iUZCVHHA.1552@TK2MSFTNGP05.phx.gbl... > 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. > Thanks |
|||||||||||||||||||||||