|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Detached datarow and datarow.tablestuff. This has been bugging me. Here's the basic question: What is going on behind the scenes when I create a detached datarow and then call datarow.table.add? What's being accessed there? It isn't attached to a table, so is .net creating a table on the fly, or does it automatically create a dataset and a datatable for it behind the scenes? Or is something else going on? Here's an example of something weird that I did that, oddly, works. 1) I create a detached datarow with datatable.NewRow ...I can call datarow.rowstate and see that it's detached. Also, the datatable falls out of scope because it's in a function in a data object. So that datatable is out of the picture. 2) I modify the datarow. Maybe I change a text field or something. 3) I want to save my changes, so I need to add it to a data table and use an adapter's update method. So, normally I'd instantiate a new datatable and do datatable.add, but here's the thing. I can actually call, /on the detached datarow/, the following lines of code: thisRow.Table.Rows.Add(thisRow) thisAdapter.update(thisRow) And it works! What is going on here? Did the table already exist because .net created a dataset and datatable behind the scenes, or did I just create a new table? Thanks in advance, Brian MacKay I believe it's because when you call NewRow there's still a reference to the
datatable. When you add the row to the table's rows collection, then the rowstate is no longer Detached and it can be passed into an Adapter for update. I believe you need to pass in an array of DataRows for that code to work but I'm not 100% sure on that. -- Show quoteW.G. Ryan, MVP www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com <bri***@inntec.com> wrote in message news:1102611758.666964.263000@f14g2000cwb.googlegroups.com... > Hello, I have a simple ADO.NET question for someone who knows their > stuff. This has been bugging me. > > Here's the basic question: What is going on behind the scenes when I > create a detached datarow and then call datarow.table.add? What's being > accessed there? > > It isn't attached to a table, so is .net creating a table on the fly, > or does it automatically create a dataset and a datatable for it behind > the scenes? Or is something else going on? > > Here's an example of something weird that I did that, oddly, works. > > 1) I create a detached datarow with datatable.NewRow ...I can call > datarow.rowstate and see that it's detached. > > Also, the datatable falls out of scope because it's in a function in a > data object. So that datatable is out of the picture. > > 2) I modify the datarow. Maybe I change a text field or something. > > 3) I want to save my changes, so I need to add it to a data table and > use an adapter's update method. So, normally I'd instantiate a new > datatable and do datatable.add, but here's the thing. I can actually > call, /on the detached datarow/, the following lines of code: > > thisRow.Table.Rows.Add(thisRow) > thisAdapter.update(thisRow) > > And it works! What is going on here? Did the table already exist > because .net created a dataset and datatable behind the scenes, or did > I just create a new table? > > Thanks in advance, > Brian MacKay > Brian,
When you call NewRow on a table - only the rowstate is detached. There is private variable inside of DataRow called as _Table, which is populated by the table instance that NewRow was called upon. How it got the instance was - datatable stores an instance of an internal class called DataRowBuilder, and DataRowBuilder.Table = this (the table itself). DataRowBuilder - is what is used to generate the new row for you. Soon as you did an Add to the rows collection, the rowstate was changed from Detached to <<non-detached>> something else, in which case the dataadapter will now pick it. Quite interesting huh? :-) - Sahil Malik http://dotnetjunkies.com/weblog/sahilmalik http://blogs.apress.com/authors.php?author=Sahil Malik <bri***@inntec.com> wrote in message Show quote news:1102611758.666964.263000@f14g2000cwb.googlegroups.com... > Hello, I have a simple ADO.NET question for someone who knows their > stuff. This has been bugging me. > > Here's the basic question: What is going on behind the scenes when I > create a detached datarow and then call datarow.table.add? What's being > accessed there? > > It isn't attached to a table, so is .net creating a table on the fly, > or does it automatically create a dataset and a datatable for it behind > the scenes? Or is something else going on? > > Here's an example of something weird that I did that, oddly, works. > > 1) I create a detached datarow with datatable.NewRow ...I can call > datarow.rowstate and see that it's detached. > > Also, the datatable falls out of scope because it's in a function in a > data object. So that datatable is out of the picture. > > 2) I modify the datarow. Maybe I change a text field or something. > > 3) I want to save my changes, so I need to add it to a data table and > use an adapter's update method. So, normally I'd instantiate a new > datatable and do datatable.add, but here's the thing. I can actually > call, /on the detached datarow/, the following lines of code: > > thisRow.Table.Rows.Add(thisRow) > thisAdapter.update(thisRow) > > And it works! What is going on here? Did the table already exist > because .net created a dataset and datatable behind the scenes, or did > I just create a new table? > > Thanks in advance, > Brian MacKay > |
|||||||||||||||||||||||