problem with refreshing identity in RowUpdatedEventHandler

W

-=Wojtek=-

Hi!

I am trying to refresh identity column for datatable with computed(by
ado.net) column.
I have realized that I cant use batch query in dataadapter InsertCommand
because after DataAdapter.Update() Framework changes the "ReadOnly" property
of
computed column throwing an exception. That is why I wrote an EventHandler
for RowUpdated event of my DataAdapter. The problem is that another
exception was thrown
during modification of e.Row: " System.Data.VersionNotFound: There is no
Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."


Do you know what is wrong with my code?
Thanks for help!
Wojtek



private void daRataSkladkiRowUpdated(object sender, SqlRowUpdatedEventArgs
e)

{

if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))

{

e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar(); //returns
@@Identity

e.Row.AcceptChanges();

}


}
 
S

Sahil Malik [MVP]

Wojtek,

I hope you are using RowUpdated event to retreive the newly added value
because whatever database you are using does not support output parameters?
If you can leverage output parameters, you should try using those instead.

Secondly, if you must use RowUpdated event to retreive the newly generated
identity values - those are not in the dataset, you will need to execute
another query on the database to find out the last generated id (which is
why it is inefficient because it hits the network too many times).

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
W

-=Wojtek=-

Thanks Sahil for your relpy!
You are right that my implementation is not the fastest (normally I use
batch queries to refresh identity values but in this case it is not
possible - problem with expression based column) but is should work anyway.
Let me explain my code.

//daRataSkladki - this is DataAdapter
//odswiezRateSkladki - this is a command object which retrives
// identity value for inserted column
//ID_Raty_Skladki is a primary key(identity) of table being updated
daRataSkladki.RowUpdated+=new
SqlRowUpdatedEventHandler(daRataSkladkiRowUpdated);


private void daRataSkladkiRowUpdated(object sender,SqlRowUpdatedEventArgs e)
{
if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))
{ e.Row.BeginEdit();
e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//refresh "ID_Raty_Skladki" column with database generated identity
value
e.Row.AcceptChanges();
e.Row.EndEdit();
}
}

I debugged my code. SQL Command "odswiezRateSkladki" retrieves appropirate
identity values. The exception is thrown in e.Row.EndEdit(). It says:
" System.Data.VersionNotFound: There is no Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."

Please look again at my code.

Greetings!
Wojtek




U¿ytkownik "Sahil Malik said:
Wojtek,

I hope you are using RowUpdated event to retreive the newly added value
because whatever database you are using does not support output parameters?
If you can leverage output parameters, you should try using those instead.

Secondly, if you must use RowUpdated event to retreive the newly generated
identity values - those are not in the dataset, you will need to execute
another query on the database to find out the last generated id (which is
why it is inefficient because it hits the network too many times).

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------- --



-=Wojtek=- said:
Hi!

I am trying to refresh identity column for datatable with computed(by
ado.net) column.
I have realized that I cant use batch query in dataadapter InsertCommand
because after DataAdapter.Update() Framework changes the "ReadOnly"
property
of
computed column throwing an exception. That is why I wrote an EventHandler
for RowUpdated event of my DataAdapter. The problem is that another
exception was thrown
during modification of e.Row: " System.Data.VersionNotFound: There is no
Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."


Do you know what is wrong with my code?
Thanks for help!
Wojtek



private void daRataSkladkiRowUpdated(object sender, SqlRowUpdatedEventArgs
e)

{

if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))

{

e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//returns
@@Identity

e.Row.AcceptChanges();

}


}
 
S

Sahil Malik [MVP]

Wojtek,

Instead of BeginEdit and EndEdit, can you quickly try simply

"e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();" ?

Just curious if that gives you the same error.


--

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------------------------


-=Wojtek=- said:
Thanks Sahil for your relpy!
You are right that my implementation is not the fastest (normally I use
batch queries to refresh identity values but in this case it is not
possible - problem with expression based column) but is should work
anyway.
Let me explain my code.

//daRataSkladki - this is DataAdapter
//odswiezRateSkladki - this is a command object which retrives
// identity value for inserted column
//ID_Raty_Skladki is a primary key(identity) of table being updated
daRataSkladki.RowUpdated+=new
SqlRowUpdatedEventHandler(daRataSkladkiRowUpdated);


private void daRataSkladkiRowUpdated(object sender,SqlRowUpdatedEventArgs
e)
{
if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))
{ e.Row.BeginEdit();
e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//refresh "ID_Raty_Skladki" column with database generated identity
value
e.Row.AcceptChanges();
e.Row.EndEdit();
}
}

I debugged my code. SQL Command "odswiezRateSkladki" retrieves appropirate
identity values. The exception is thrown in e.Row.EndEdit(). It says:
" System.Data.VersionNotFound: There is no Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."

Please look again at my code.

Greetings!
Wojtek




U¿ytkownik "Sahil Malik said:
Wojtek,

I hope you are using RowUpdated event to retreive the newly added value
because whatever database you are using does not support output parameters?
If you can leverage output parameters, you should try using those
instead.

Secondly, if you must use RowUpdated event to retreive the newly
generated
identity values - those are not in the dataset, you will need to execute
another query on the database to find out the last generated id (which is
why it is inefficient because it hits the network too many times).

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------- --



-=Wojtek=- said:
Hi!

I am trying to refresh identity column for datatable with computed(by
ado.net) column.
I have realized that I cant use batch query in dataadapter
InsertCommand
because after DataAdapter.Update() Framework changes the "ReadOnly"
property
of
computed column throwing an exception. That is why I wrote an EventHandler
for RowUpdated event of my DataAdapter. The problem is that another
exception was thrown
during modification of e.Row: " System.Data.VersionNotFound: There is
no
Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32
proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."


Do you know what is wrong with my code?
Thanks for help!
Wojtek



private void daRataSkladkiRowUpdated(object sender, SqlRowUpdatedEventArgs
e)

{

if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))

{

e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//returns
@@Identity

e.Row.AcceptChanges();

}


}
 
S

Sahil Malik [MVP]

Also, I am quite stoked and curious about why can't you use output
parameters. :)


--

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------------------------



-=Wojtek=- said:
Thanks Sahil for your relpy!
You are right that my implementation is not the fastest (normally I use
batch queries to refresh identity values but in this case it is not
possible - problem with expression based column) but is should work
anyway.
Let me explain my code.

//daRataSkladki - this is DataAdapter
//odswiezRateSkladki - this is a command object which retrives
// identity value for inserted column
//ID_Raty_Skladki is a primary key(identity) of table being updated
daRataSkladki.RowUpdated+=new
SqlRowUpdatedEventHandler(daRataSkladkiRowUpdated);


private void daRataSkladkiRowUpdated(object sender,SqlRowUpdatedEventArgs
e)
{
if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))
{ e.Row.BeginEdit();
e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//refresh "ID_Raty_Skladki" column with database generated identity
value
e.Row.AcceptChanges();
e.Row.EndEdit();
}
}

I debugged my code. SQL Command "odswiezRateSkladki" retrieves appropirate
identity values. The exception is thrown in e.Row.EndEdit(). It says:
" System.Data.VersionNotFound: There is no Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."

Please look again at my code.

Greetings!
Wojtek




U¿ytkownik "Sahil Malik said:
Wojtek,

I hope you are using RowUpdated event to retreive the newly added value
because whatever database you are using does not support output parameters?
If you can leverage output parameters, you should try using those
instead.

Secondly, if you must use RowUpdated event to retreive the newly
generated
identity values - those are not in the dataset, you will need to execute
another query on the database to find out the last generated id (which is
why it is inefficient because it hits the network too many times).

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------- --



-=Wojtek=- said:
Hi!

I am trying to refresh identity column for datatable with computed(by
ado.net) column.
I have realized that I cant use batch query in dataadapter
InsertCommand
because after DataAdapter.Update() Framework changes the "ReadOnly"
property
of
computed column throwing an exception. That is why I wrote an EventHandler
for RowUpdated event of my DataAdapter. The problem is that another
exception was thrown
during modification of e.Row: " System.Data.VersionNotFound: There is
no
Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32
proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."


Do you know what is wrong with my code?
Thanks for help!
Wojtek



private void daRataSkladkiRowUpdated(object sender, SqlRowUpdatedEventArgs
e)

{

if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))

{

e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//returns
@@Identity

e.Row.AcceptChanges();

}


}
 
W

-=Wojtek=-

Hi Sahil!

"e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();"
gives the same error. I am not using stored procedures because it is faster
for me to create my application with SQL queries built-in the C# code.
Anyway I suppose that output parameters wouldnt solve this problem.
In my opinion bad implementation of dataset expression based column causes
this problem. I resigned from this ado.net functionality and replaced it
with my custom code. Now everything works fine with batch queries
refreshing the dataset.

Greetings!
Wojtek

U¿ytkownik "Sahil Malik said:
Wojtek,

Instead of BeginEdit and EndEdit, can you quickly try simply

"e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();" ?

Just curious if that gives you the same error.


--

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------- -----------------


-=Wojtek=- said:
Thanks Sahil for your relpy!
You are right that my implementation is not the fastest (normally I use
batch queries to refresh identity values but in this case it is not
possible - problem with expression based column) but is should work
anyway.
Let me explain my code.

//daRataSkladki - this is DataAdapter
//odswiezRateSkladki - this is a command object which retrives
// identity value for inserted column
//ID_Raty_Skladki is a primary key(identity) of table being updated
daRataSkladki.RowUpdated+=new
SqlRowUpdatedEventHandler(daRataSkladkiRowUpdated);


private void daRataSkladkiRowUpdated(object sender,SqlRowUpdatedEventArgs
e)
{
if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))
{ e.Row.BeginEdit();
e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//refresh "ID_Raty_Skladki" column with database generated identity
value
e.Row.AcceptChanges();
e.Row.EndEdit();
}
}

I debugged my code. SQL Command "odswiezRateSkladki" retrieves appropirate
identity values. The exception is thrown in e.Row.EndEdit(). It says:
" System.Data.VersionNotFound: There is no Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."

Please look again at my code.

Greetings!
Wojtek




U¿ytkownik "Sahil Malik [MVP]" <[email protected]> napisa³ w
wiadomooci news:#[email protected]...
Wojtek,

I hope you are using RowUpdated event to retreive the newly added value
because whatever database you are using does not support output parameters?
If you can leverage output parameters, you should try using those
instead.

Secondly, if you must use RowUpdated event to retreive the newly
generated
identity values - those are not in the dataset, you will need to execute
another query on the database to find out the last generated id (which is
why it is inefficient because it hits the network too many times).

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
------------------------------------------------------------------------- -
--
Hi!

I am trying to refresh identity column for datatable with computed(by
ado.net) column.
I have realized that I cant use batch query in dataadapter
InsertCommand
because after DataAdapter.Update() Framework changes the "ReadOnly"
property
of
computed column throwing an exception. That is why I wrote an EventHandler
for RowUpdated event of my DataAdapter. The problem is that another
exception was thrown
during modification of e.Row: " System.Data.VersionNotFound: There is
no
Original data to access.
at System.Data.DataTable.SetNewRecord(DataRow row, Int32
proposedRecord,
DataRowAction action, Boolean isInMErge)
at System.Data.DataRow.EndEdit() .."


Do you know what is wrong with my code?
Thanks for help!
Wojtek



private void daRataSkladkiRowUpdated(object sender, SqlRowUpdatedEventArgs
e)

{

if ((e.StatementType == StatementType.Insert) & (e.Status ==
UpdateStatus.Continue))

{

e.Row["ID_Raty_Skladki"]=(int)odswiezRateSkladki.ExecuteScalar();
//returns
@@Identity

e.Row.AcceptChanges();

}


}
 
C

Cor Ligthert [MVP]

Wojtek,

Are you doing this with every datarow change in your datatable.

That would mean in my opinion that you try to set the identies in your
datatable all to the same key. I can me not imaging that this will not go
wrong?

(While it is probably even recursive and therefore endless calling this
routine so you would not know on what error it will stick).

I hope this helps,

Cor
 

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