DBConcurrency Exception

G

Guest

I am populating an ASP.NET datagrid with a dataview object stored in session state
To update the database I am using a commandbuilder object to generate the relevant statements

I can update and delete rows without having to re-fill the dataset (dataview)
However, if I add a new row and then delete it after postback, using the datagrid, I get a DBConcurrency exception (there are no other users making changes to the database)

If I insert the new row and then re-fill the dataset (dataview), it works perfectly

Is this how I am supposed to insert rows? Can't I get around having to re-populate the whole dataset

Here's a snippet of my code that does an insert
Dim dv As DataView = CType(Session("DataView"), DataView

' insert new row into datavie
Dim dr As DataRow = dv.Table.NewRo
dr.Item("Artist") = txtArtist.Tex
dr.Item("Title") = txtTitle.Tex
dv.Table.Rows.Add(dr

' make changes to database (using command builder
UpdateData(dv

' re-fill the dataset (dataview
LoadData(

' rebind the datagri
FillGrid()
 
M

Miha Markic [MVP C#]

Hi James,

You are getting concurrency exception probably because of insertcommand.
Does your table have primary key?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

James said:
I am populating an ASP.NET datagrid with a dataview object stored in session state.
To update the database I am using a commandbuilder object to generate the relevant statements.

I can update and delete rows without having to re-fill the dataset (dataview).
However, if I add a new row and then delete it after postback, using the
datagrid, I get a DBConcurrency exception (there are no other users making
changes to the database).
 
M

Miha Markic [MVP C#]

Hi James,

I guess that adapter doesn't refresh the identity column of DataTable.
I strongly suggest you to create adapter either at design time or in code
rather the using commandbuilders.
 
G

Guest

I created my adaptor at design time, but use command builders at runtime

My insert works perfectly. My only problem is that I have to refill my dataset after I insert a row

Is this how it's supposed to be? I don't have to do this after an update or delete.
 
M

Miha Markic [MVP C#]

Hi James,

James said:
I created my adaptor at design time, but use command builders at runtime.

Well, you should avoid command builder - why don't you configure adapter at
design time?
My insert works perfectly. My only problem is that I have to refill my
dataset after I insert a row.

Yes, insert works. But refresh od identity field in memory doesn't work.
Is this how it's supposed to be?

No, insert command has to have a select command after insert, so it can
retrieve the newly created indentity value.

I don't have to do this after an update or delete.

Yup - no change of identity there.
 
G

Guest

Hey, I tried what you said - created the commands of the data adaptor at design time and it worked

Just one question: Why didn't the commandbuilder code work for an insert but it did for updates and deletes?
 
G

Guest

I see. Why doesn't the command builder place the select statement after the insert statement for you?
 
M

Miha Markic [MVP C#]

Hi James,

I don't know. CommandBuilder is a black box which I avoid at all costs :)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

James said:
Hey, I tried what you said - created the commands of the data adaptor at design time and it worked.

Just one question: Why didn't the commandbuilder code work for an insert
but it did for updates and deletes?
 

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