Problem With DataRow Rowstate

M

Marco Gatto

Hello Everyone
I've got a problem when I set a value into a DataRow X after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..
isn't it?


I you see the following code (just "little boring"...)

Public Class testclass
Protected mDataTable As Data.DataTable
Protected mRow As Data.DataRow
Protected mDataAdapter As SqlClient.SqlDataAdapter
Protected mConn As SqlClient.SqlConnection
'----------------------------
' New
'----------------------------
Public Sub New()
mDataTable = New Data.DataTable()
mConn = New SqlClient.SqlConnection("Data
Source=test-srv;Integrated Security=SSPI;Initial Catalog=Test")
mDataAdapter = New SqlClient.SqlDataAdapter("SELECT ID,
Descrizione FROM Table", mConn)

mDataAdapter.FillSchema(mDataTable, SchemaType.Mapped)
mRow = mDataTable.NewRow()
End Sub


Public Sub Insert()
If mRow.RowState <> DataRowState.Detached Then Exit Sub
mRow(0) = "c51ac21a-05bc-475c-ad75-1affd8f980c3"
mRow(1) = "Hello!!"

'--------------------------------------------
' Delete evenually records from DataTable
'--------------------------------------------
mDataTable.Clear()
'-----------------------------------------
' AddRow
'-----------------------------------------
mDataTable.Rows.Add(mRow)
Debug.WriteLine(mRow.RowState)

'---------------------------
'Accept
'----------------------------
mDataTable.AcceptChanges()
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)
'----------------------------------------
' Try to change value BUT!!!!!
'----------------------------------------
mRow(1) = "Foo2"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

mRow(1) = "Foo3"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

End Sub

AND CLIENT USER...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim trow As New testclass()
trow.Insert()

End Sub

End Class


You can see that after AcceptChanges is all ok... (Rowstate=
Unchanged) but when I set:
mRow(1) = "Foo2"

mRow.rowstate remains Unchanged....

Please Help me!!

Marco Gatto
 
D

Derek LaZard

Hi Marco.

After changes are accepted the rowstate is now Unchanged. :)
[dataAdapter.Fill() and .Update() resets the rowstates to unchanged...

Derek LaZard
 
D

Derek LaZard

Marco, BTW, I've noticed in your code listing:

mDataTable.Clear()

IMO, apparently this statement, in this sequence, is breaking internal links that
mDataTable.NewRow() sets up-- .Clear() removes the row from the DataTable, and
at least removes some of the internal plumbing (events, etc.) that ..NewRow() makes for rows in a DataTable--.Clear() reverses .NewRow()...

Have you check this in the debugger? I have for the C# translation...

The code sequence should always be something like the following:

....
aDataRow = .NewRow()

//(optional processing)...edits, checks/validation...

..Add( aDataRow);
....

What do you think?

Derek LaZard

Marco Gatto said:
Sorry Derek but I didn't explain very well.... :)

Step1 Insert New Row X ----> Detached (OK)
Step2 Add X ----> Added (OK)
Step3 AcceptChanges ----> Unchanged(OK)
Step4 Set New Value (Foo2) ----> Unchanged(NOOO)
Step5 Set New Value (Foo3) ----> Modified (...)

Marco Gatto

Derek LaZard said:
Hi Marco.
after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..

After changes are accepted the rowstate is now Unchanged. :)
[dataAdapter.Fill() and .Update() resets the rowstates to unchanged...

Derek LaZard


Marco Gatto said:
Hello Everyone
I've got a problem when I set a value into a DataRow X after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..
isn't it?


I you see the following code (just "little boring"...)

Public Class testclass
Protected mDataTable As Data.DataTable
Protected mRow As Data.DataRow
Protected mDataAdapter As SqlClient.SqlDataAdapter
Protected mConn As SqlClient.SqlConnection
'----------------------------
' New
'----------------------------
Public Sub New()
mDataTable = New Data.DataTable()
mConn = New SqlClient.SqlConnection("Data
Source=test-srv;Integrated Security=SSPI;Initial Catalog=Test")
mDataAdapter = New SqlClient.SqlDataAdapter("SELECT ID,
Descrizione FROM Table", mConn)

mDataAdapter.FillSchema(mDataTable, SchemaType.Mapped)
mRow = mDataTable.NewRow()
End Sub


Public Sub Insert()
If mRow.RowState <> DataRowState.Detached Then Exit Sub
mRow(0) = "c51ac21a-05bc-475c-ad75-1affd8f980c3"
mRow(1) = "Hello!!"

'--------------------------------------------
' Delete evenually records from DataTable
'--------------------------------------------
mDataTable.Clear()
'-----------------------------------------
' AddRow
'-----------------------------------------
mDataTable.Rows.Add(mRow)
Debug.WriteLine(mRow.RowState)

'---------------------------
'Accept
'----------------------------
mDataTable.AcceptChanges()
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)
'----------------------------------------
' Try to change value BUT!!!!!
'----------------------------------------
mRow(1) = "Foo2"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

mRow(1) = "Foo3"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

End Sub

AND CLIENT USER...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim trow As New testclass()
trow.Insert()

End Sub

End Class


You can see that after AcceptChanges is all ok... (Rowstate=
Unchanged) but when I set:
mRow(1) = "Foo2"

mRow.rowstate remains Unchanged....

Please Help me!!

Marco Gatto
 
D

Derek LaZard

Marco, I'm getting a rowState = modified in C#, after the first edit update to the row in the DataTable...hmm...:

....
TableOfDeletes = dsAuthors.Tables["authors"].Clone();
ry = TableOfDeletes.NewRow();
TableOfDeletes.Rows.Add( ry);
ry[0] = "111-11-1111";
TableOfDeletes.AcceptChanges(); //ry is now unchanged
ry[0] = "333-33-3333"; //ry is now modified
....

I'll try it in VB...
BTW, Did you check this in the debugger?

Derek LaZard




Marco Gatto said:
Sorry Derek but I didn't explain very well.... :)

Step1 Insert New Row X ----> Detached (OK)
Step2 Add X ----> Added (OK)
Step3 AcceptChanges ----> Unchanged(OK)
Step4 Set New Value (Foo2) ----> Unchanged(NOOO)
Step5 Set New Value (Foo3) ----> Modified (...)

Marco Gatto

Derek LaZard said:
Hi Marco.
after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..

After changes are accepted the rowstate is now Unchanged. :)
[dataAdapter.Fill() and .Update() resets the rowstates to unchanged...

Derek LaZard


Marco Gatto said:
Hello Everyone
I've got a problem when I set a value into a DataRow X after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..
isn't it?


I you see the following code (just "little boring"...)

Public Class testclass
Protected mDataTable As Data.DataTable
Protected mRow As Data.DataRow
Protected mDataAdapter As SqlClient.SqlDataAdapter
Protected mConn As SqlClient.SqlConnection
'----------------------------
' New
'----------------------------
Public Sub New()
mDataTable = New Data.DataTable()
mConn = New SqlClient.SqlConnection("Data
Source=test-srv;Integrated Security=SSPI;Initial Catalog=Test")
mDataAdapter = New SqlClient.SqlDataAdapter("SELECT ID,
Descrizione FROM Table", mConn)

mDataAdapter.FillSchema(mDataTable, SchemaType.Mapped)
mRow = mDataTable.NewRow()
End Sub


Public Sub Insert()
If mRow.RowState <> DataRowState.Detached Then Exit Sub
mRow(0) = "c51ac21a-05bc-475c-ad75-1affd8f980c3"
mRow(1) = "Hello!!"

'--------------------------------------------
' Delete evenually records from DataTable
'--------------------------------------------
mDataTable.Clear()
'-----------------------------------------
' AddRow
'-----------------------------------------
mDataTable.Rows.Add(mRow)
Debug.WriteLine(mRow.RowState)

'---------------------------
'Accept
'----------------------------
mDataTable.AcceptChanges()
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)
'----------------------------------------
' Try to change value BUT!!!!!
'----------------------------------------
mRow(1) = "Foo2"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

mRow(1) = "Foo3"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

End Sub

AND CLIENT USER...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim trow As New testclass()
trow.Insert()

End Sub

End Class


You can see that after AcceptChanges is all ok... (Rowstate=
Unchanged) but when I set:
mRow(1) = "Foo2"

mRow.rowstate remains Unchanged....

Please Help me!!

Marco Gatto
 
M

Marco Gatto

Great Derek!!
I've tried without mDataTable.Clear() and now it works....
I think too that it removes links from datatable but it strange that
it losts a property of datarows!!!
I inserted mDataTable.Clear() because I wanted to insert the current
row only.

Ok now I know It...
Thanks


Derek LaZard said:
Marco, BTW, I've noticed in your code listing:

mDataTable.Clear()

IMO, apparently this statement, in this sequence, is breaking internal
links that
mDataTable.NewRow() sets up-- .Clear() removes the row from the
DataTable, and
at least removes some of the internal plumbing (events, etc.) that
.NewRow() makes for rows in a DataTable--.Clear() reverses .NewRow()...


Have you check this in the debugger? I have for the C# translation...

The code sequence should always be something like the following:

...
aDataRow = .NewRow()

//(optional processing)...edits, checks/validation...

.Add( aDataRow);
...

What do you think?

Derek LaZard

Marco Gatto said:
Sorry Derek but I didn't explain very well.... :)

Step1 Insert New Row X ----> Detached (OK)
Step2 Add X ----> Added (OK)
Step3 AcceptChanges ----> Unchanged(OK)
Step4 Set New Value (Foo2) ----> Unchanged(NOOO)
Step5 Set New Value (Foo3) ----> Modified (...)

Marco Gatto

"Derek LaZard" <[email protected]> wrote in message
Hi Marco.

after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..

After changes are accepted the rowstate is now Unchanged. :)
[dataAdapter.Fill() and .Update() resets the rowstates to unchanged...

Derek LaZard


Hello Everyone
I've got a problem when I set a value into a DataRow X after an
Acceptchanges() on DataTable that contains X. After this operation
DataRow.Rowstate is Unchanged... strange... it must be Modified ..
isn't it?


I you see the following code (just "little boring"...)

Public Class testclass
Protected mDataTable As Data.DataTable
Protected mRow As Data.DataRow
Protected mDataAdapter As SqlClient.SqlDataAdapter
Protected mConn As SqlClient.SqlConnection
'----------------------------
' New
'----------------------------
Public Sub New()
mDataTable = New Data.DataTable()
mConn = New SqlClient.SqlConnection("Data
Source=test-srv;Integrated Security=SSPI;Initial Catalog=Test")
mDataAdapter = New SqlClient.SqlDataAdapter("SELECT ID,
Descrizione FROM Table", mConn)

mDataAdapter.FillSchema(mDataTable, SchemaType.Mapped)
mRow = mDataTable.NewRow()
End Sub


Public Sub Insert()
If mRow.RowState <> DataRowState.Detached Then Exit Sub
mRow(0) = "c51ac21a-05bc-475c-ad75-1affd8f980c3"
mRow(1) = "Hello!!"

'--------------------------------------------
' Delete evenually records from DataTable
'--------------------------------------------
mDataTable.Clear()
'-----------------------------------------
' AddRow
'-----------------------------------------
mDataTable.Rows.Add(mRow)
Debug.WriteLine(mRow.RowState)

'---------------------------
'Accept
'----------------------------
mDataTable.AcceptChanges()
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)
'----------------------------------------
' Try to change value BUT!!!!!
'----------------------------------------
mRow(1) = "Foo2"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

mRow(1) = "Foo3"
Debug.WriteLine(mRow(1, DataRowVersion.Original) & " " &
mRow(1, DataRowVersion.Current) & " " & mRow.RowState)

End Sub

AND CLIENT USER...

Private Sub Button1 Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim trow As New testclass()
trow.Insert()

End Sub

End Class


You can see that after AcceptChanges is all ok... (Rowstate=
Unchanged) but when I set:
mRow(1) = "Foo2"

mRow.rowstate remains Unchanged....

Please Help me!!

Marco Gatto
--
 

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