TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Venkat S Iyer
NA
3
0
Concurrency Violation Exception
Dec 15 2009 11:02 PM
Hi i am writing displaying a table using
DataGridView
.
My Table is dynamic
in the sense it is created only
after the application is running
so there is no chance i can know about the no of columns available. However, i am having
useridauto
, which will always be the
primary key
and it is also
autoincremen
t. So, i need to write my own insertcommand and updatecommand. My code is as follows:
************************************************
'this is used to dynamically create a insert command according to the table
Dim ins As New OleDbCommand
'insert command
query = "INSERT INTO T_" & frmHome.strSelectedSet & "( "
j = Nothing
For Each j In gridviewAnswerData.Columns
'leave the first column which is useridauto
If Not j.Name = gridviewAnswerData.Columns(0).Name Then
query += j.Name & ","
End If
Next
query = query.Remove(query.Length - 1, 1)
query += ") VALUES( "
For Each j In gridviewAnswerData.Columns
'leave the first column which is useridauto
If Not j.Name = gridviewAnswerData.Columns(0).Name Then
query += "@" & j.Name & ","
End If
Next
query = query.Remove(query.Length - 1, 1)
query += ")"
MessageBox.Show(query)
'assigning the insert command
ins.CommandText = query
ins.Connection = New OleDbConnection(IshaData.My.Settings.ishaConnectionString)
daSet.InsertCommand = ins
For Each j In gridviewAnswerData.Columns
'leave the first column which is useridauto
If Not j.Name = gridviewAnswerData.Columns(0).Name Then
daSet.InsertCommand.Parameters.Add("@" + j.Name, OleDbType.VarChar).SourceColumn = j.Name
End If
Next
'Update command
query = "UPDATE T_" & frmHome.strSelectedSet & " SET "
j = Nothing
For Each j In gridviewAnswerData.Columns
'leave the first column which is useridauto
If Not j.Name = gridviewAnswerData.Columns(0).Name Then
query += j.Name & "=" & "@" & j.Name & ","
Else
MessageBox.Show(j.Name)
End If
Next
query = query.Remove(query.Length - 1, 1)
query += " WHERE Useridauto=@" + gridviewAnswerData.Columns(0).Name
MessageBox.Show(query)
MessageBox.Show(gridviewAnswerData.Columns(0).Name)
'assigning the insert command
ins.CommandText = query
ins.Connection = New OleDbConnection(IshaData.My.Settings.ishaConnectionString)
daSet.UpdateCommand = ins
For Each j In gridviewAnswerData.Columns
'leave the first column which is useridauto
If Not j.Name = gridviewAnswerData.Columns(0).Name Then
daSet.UpdateCommand.Parameters.Add("@" + j.Name, OleDbType.VarChar).SourceColumn = j.Name
End If
Next
daSet.UpdateCommand.Parameters.Add("@" + gridviewAnswerData.Columns(0).Name, OleDbType.Integer).SourceColumn = gridviewAnswerData.Columns(0).Name
daSet.UpdateCommand.Parameters("@Useridauto").SourceVersion = DataRowVersion.Original
************************************************
My Database is MS ACCESS. My
insert command is working fine, but update command is not working and it is showing concurrency violation when i try to update. Any other details needed ill be happy to give. Please help.
Reply
Answers (
2
)
Delete my email Message in Google using C#
difference between Machine.Config and Web.Config File