Sometimes the following code generates this exception - "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sql As String = "SELECT * FROM VsrData WHERE RaceDate=@RaceDate and Venue=@Venue and Time=@Time"
RaceDateString = RaceDate.Text.Substring(6, 1).ToString + RaceDate.Text.Substring(7, 1).ToString() + RaceDate.Text.Substring(8, 1).ToString + RaceDate.Text.Substring(9, 1).ToString + RaceDate.Text.Substring(5, 1).ToString + RaceDate.Text.Substring(3, 1).ToString + RaceDate.Text.Substring(4, 1).ToString + RaceDate.Text.Substring(2, 1).ToString + RaceDate.Text.Substring(0, 1).ToString + RaceDate.Text.Substring(1, 1).ToString
DupAlertCount = 0
Me.VsrDataTableAdapter.Update(Me.EquinexMasterSqlDataSet.VsrData)
FlatStr = ""
HurdleStr = ""
ChaseStr = ""
HcapStr = ""
conn.Open()
VsrDataRow = Me.EquinexMasterSqlDataSet.VsrData.NewRow()
VsrDataRow.CustomRecordId = ""
If (RaceMarker = "Y") Then
DupAlertCount = 0
RaceMarker = ""
RaceTime.Text = ""
RaceVenue.Text = ""
RaceDistance.Text = ""
RaceRunners.Text = ""
RowCount = 1
ScCount = 1
VsrDataRow.Time = Me.RaceTime.Text
VsrDataRow.RaceDate = Me.RaceDate.Text
VsrDataRow.Venue = Me.RaceVenue.Text
If (RowCount < 10) Then
VsrDataRow.CustomRecordId = Me.RaceVenue.Text + Me.RaceDate.Text.ToCharArray(0, 10) + "-" + Me.RaceTime.Text.ToCharArray(0, Me.RaceTime.TextLength) + "-0" + RowCount.ToString()
Else
VsrDataRow.CustomRecordId = Me.RaceVenue.Text + Me.RaceDate.Text.ToCharArray(0, 10) + "-" + Me.RaceTime.Text.ToCharArray(0, Me.RaceTime.TextLength) + "-" + RowCount.ToString()
End If
VsrDataRow.CHF = Me.RaceChf.Text
VsrDataRow.Hcap = Me.RaceHcap.Text
VsrDataRow.RaceDist = Me.RaceDistance.Text
If (RaceRunners.Text = "") Then
MsgBox("Please enter number of runners")
Me.VsrDataTableAdapter.Update(Me.EquinexMasterSqlDataSet.VsrData)
Else
VsrDataRow.RaceRunners = Me.RaceRunners.Text
VsrDataRow.RaceClass = Me.RaceClass.Text
VsrDataRow.Going = Me.RaceGoing.Text
If (Me.RaceChf.Text = "F") Then
FlatStr = "Flat"
ElseIf (Me.RaceChf.Text = "H") Then
HurdleStr = "Hurdle"
ElseIf (Me.RaceChf.Text = "C") Then
ChaseStr = "Chase"
Else
' Do Nothing
End If
If (Me.RaceHcap.Text = "Y") Then
HcapStr = "Handicap"
Else
' Do Nothing
End If
VsrDataRow.RaceTitle = String.Format("{0}{1}{2} {3} {4}", FlatStr, HurdleStr, ChaseStr, HcapStr, TitlesDebug.Text)
VsrDataRow.SaddleCloth = ScCount
VsrDataRow.HorseName = "Horses"
VsrDataRow.Lf3 = "1"
VsrDataRow.Lf2 = "1"
VsrDataRow.Lf1 = "1"
VsrDataRow.LastRan = "2"
VsrDataRow.BettingPos = "1"
VsrDataRow.FcPrice = "9/2"
VsrDataRow.TrainerForm = "1"
VsrDataRow.GoingForm = "1"
VsrDataRow.DistForm = "1"
VsrDataRow.CourseForm = "1"
VsrDataRow.DrawForm = "-"
VsrDataRow.AbilityForm = "1"
VsrDataRow.FormForm = "1"
VsrDataRow.PS = "1"
VsrDataRow.Rating = "1"
VsrDataRow.FDB = "1"
VsrDataRow.B = "1"
VsrDataRow.Jockey = "1"
VsrDataRow.Trainer = "1"
VsrDataRow.TravCheck = "1"
VsrDataRow.Bolds = "1"
VsrDataRow.Arrows = "1"
VsrDataRow.EndOfRace = "Y"
If VsrDataRow.SaddleCloth = CInt(RaceRunners.Text) Then
VsrDataRow.EndOfRace = "Y"
RaceMarker = "Y"
Else
' Do Nothing
End If
'Me.EquinexMasterSqlDataSet.VsrData.Rows.Add(VsrDataRow)
RaceRows1 = EquinexMasterSqlDataSet.VsrData.Rows.Count - 1
If (showndata = 1 And datedata = 0 And racedata = 0) Then
For DupRaceCount1 = 0 To EquinexMasterSqlDataSet.VsrData.Rows.Count - 1 Step 1
If (VsrDataRow.CustomRecordId.ToString.Trim = EquinexMasterSqlDataSet.VsrData.Rows(DupRaceCount1).Item(EquinexMasterSqlDataSet.VsrData.CustomRecordIdColumn).ToString.Trim) Then
MsgBox("This Race already exists")
conn.Close()
Exit Sub
Else
' Do Nothing
End If
Next
shownda.Fill(shownds, "VsrData")
VsrDataDataGridView.DataSource = shownds.Tables("VsrData")
builder.GetUpdateCommand()
shownda.Update(shownds, "VsrData")
shownda = New SqlDataAdapter(sql, conn)
shownda.SelectCommand = New SqlCommand(sql, conn)
shownda.SelectCommand.Parameters.AddWithValue("@RaceDate", RaceDateString)
shownda.SelectCommand.Parameters.AddWithValue("@Time", RaceTime.Text.ToString)
shownda.SelectCommand.Parameters.AddWithValue("@Venue", RaceVenue.Text.ToString)
builder = New SqlCommandBuilder(shownda)
Me.EquinexMasterSqlDataSet.VsrData.Rows.Add(VsrDataRow)
VsrDataDataGridView.DataSource = shownds.Tables("VsrData")
Me.VsrDataTableAdapter.Update(Me.EquinexMasterSqlDataSet.VsrData)
shownds.Clear()
shownda.Fill(shownds, "VsrData")
else
' Do Nothing
end if
end if
conn.Close()
End Sub