Thank you John, Michael, and Kevin for your responses, I will attempt
to be more specific here...
I populate the dataset like so:
Private Function PopulateDataGrid() As Boolean
pstrSQL = "select * from " & pstrTable & " order by 1"
Dim strError As String
Dim da As New OracleDataAdapter(pstrSQL, gstrConnectionString)
Try
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(dsData, pstrTable)
tdbgData.DataSource = dsData.Tables(pstrTable)
Catch Ex As Exception
MsgBox(Ex.ToString)
Return False
Finally
da.Dispose()
da = Nothing
End Try
Return True
End Function
I update the database on a Save Click event. Due to the number of
changes, additions and deletions I must update each row individually
because, if I do not, then these changes may step on each other. I
tried it, it didn't work.
In any case I loop trough the primary dataset looking for the
appropriate RowState and then import that row into a new dataset and
attempt to update it. I catch the errors and then attempt to find
where the error occurred. This is where I would like to test the
GetColumnError so that I would know which cell in the datagrid to
place the focus on as that is where the error occurred. Please see
the 'ADDED (INSERTED) ROWS' Catch statement where I have a loop
looking for the column error.
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim strSQL As String
Dim intX As Integer
Dim strError As String, strErrorMessage As String
If dsData.HasChanges Then
Cursor.Current = Cursors.WaitCursor
strSQL = "select * from " & pstrTable
Dim da As New OracleDataAdapter(strSQL, gstrConnectionString)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim objCommandBuilder As New OracleCommandBuilder(da)
Dim ds As New DataSet
ds = dsData.Clone ' Create a new dataset we will update on.
ds.Tables(pstrTable).Clear() ' Clear all the data from this
new datasets datatable
Dim dr As DataRow
Try
'*/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=*
'*
'* UPDATE DATABASE IN ORDER, DELETED, MODIFIED, ADDED
(INSERT)
'*
'* Enclosing Try block will catch any exceptions that
occur outside of the loops try blocks.
'*
'#=========================================
'#
'# DELETED ROWS
'#
'# Only this loop has been commented as the action is the
same for all three loops
'#
For intX = 0 To dsData.Tables(pstrTable).Rows.Count - 1
If dsData.Tables(pstrTable).Rows.Count - 1 < intX Then
' Test for row deletion as removing a row
permanently from the dataset will drop the count lower
' causing an exception to occur.
Exit For
End If
If dsData.Tables(pstrTable).Rows(intX).RowState =
DataRowState.Deleted Then
dr = dsData.Tables(pstrTable).Rows(intX) ' Get the
row to update from our authoratative dataset.
ds.Tables(pstrTable).ImportRow(dr) ' Import that
row, update each row individually.
Try
If ds.Tables(pstrTable).Rows(0).HasErrors Then
MsgBox("Has Errors!")
End If
da.Update(ds, pstrTable) ' Try database update
strError = "" ' Clear string error as if we
arrive at this line then the update was successful.
Catch Ex As Exception
strError = Mid(Ex.Message, 1, 9) ' Retrieve
the ORA-##### error number to deterine the nature of the error.
Select Case strError
Case "ORA-02292" ' integrity constraint
violated
dsData.Tables(pstrTable).Rows(intX).RejectChanges()
strErrorMessage = strErrorMessage &
"Row #" & CType(intX + 1, String) & " You are not allowed to destroy
an " & _
"existing code that
CWT data currently references. This row will be restored." & vbCrLf
Case Else ' Currently unknown error
MsgBox("Row #" & CType(intX + 1,
String) & vbCrLf & Ex.ToString & vbCrLf)
End Select
End Try
If strError = "" Then
' Update successful, accept changes in
dataset to clear HasChanges property for this row
dsData.Tables(pstrTable).Rows(intX).AcceptChanges()
' Because AcceptChanges will renumber the
dataset rows set
' row number back one to catch any additional
deletes.
intX = intX - 1
End If
End If
ds.Tables(pstrTable).Clear()
pbSaving.Value = pbSaving.Value + 1
Next
'#=========================================
'#
'# MODIFIED ROWS
'#
For intX = 0 To dsData.Tables(pstrTable).Rows.Count - 1
If dsData.Tables(pstrTable).Rows(intX).RowState =
DataRowState.Modified Then
dr = dsData.Tables(pstrTable).Rows(intX)
ds.Tables(pstrTable).ImportRow(dr)
Try
da.Update(ds, pstrTable)
strError = ""
Catch Ex As Exception
strError = Mid(Ex.Message, 1, 9)
Select Case strError
Case "ORA-02291"
strErrorMessage = strErrorMessage &
"Row #" & CType(intX + 1, String) & " " & EX.Message
Case "ORA-02292"
dsData.Tables(pstrTable).Rows(intX).RejectChanges()
strErrorMessage = strErrorMessage &
"Row #" & CType(intX + 1, String) & " You are not allowed to destroy
an " & _
"existing code that
CWT data currently references. This row will be restored." & vbCrLf
Case Else
MsgBox("Row #" & CType(intX + 1,
String) & vbCrLf & Ex.ToString & vbCrLf)
End Select
End Try
If strError = "" Then
dsData.Tables(pstrTable).Rows(intX).AcceptChanges()
End If
End If
ds.Tables(pstrTable).Clear()
Next
'#=========================================
'#
'# ADDED (INSERTED) ROWS
'#
For intX = 0 To dsData.Tables(pstrTable).Rows.Count - 1
If dsData.Tables(pstrTable).Rows(intX).RowState =
DataRowState.Added Then
dr = dsData.Tables(pstrTable).Rows(intX)
ds.Tables(pstrTable).ImportRow(dr)
Try
da.Update(ds, pstrTable)
strError = ""
Catch Ex As Exception
strError = Mid(Ex.Message, 1, 9)
Select Case strError
Case "ORA-02291"
strErrorMessage = strErrorMessage &
"Row #" & CType(intX + 1, String) & " You are trying to enter a code "
& _
"that's not defined.
Use an already valid code, or add the new code to its lookup table."
Dim i As Integer
For i = 0 To
ds.Tables(0).Columns.Count - 1
If ds.Tables(0).Rows(0).HasErrors
Then
' Why does the GetColumnError
always return an empty string event though the Row HasErrors?
MsgBox(ds.Tables(0).Rows(0).GetColumnError(i))
End If
Next
Case "ORA-02292"
dsData.Tables(pstrTable).Rows(intX).RejectChanges()
strErrorMessage = strErrorMessage &
"Row #" & CType(intX + 1, String) & " You are not allowed to destroy
an " & _
"existing code that
CWT data currently references. This row will be restored." & vbCrLf
Case Else
MsgBox("Row #" & CType(intX + 1,
String) & vbCrLf & Ex.ToString & vbCrLf)
End Select
End Try
If strError = "" Then
dsData.Tables(pstrTable).Rows(intX).AcceptChanges()
End If
End If
ds.Tables(pstrTable).Clear()
Next
Catch ex As Exception
MsgBox(ex.ToString)
Finally
da.Dispose()
da = Nothing
ds.Dispose()
ds = Nothing
dr = Nothing
End Try
End If
Cursor.Current = Cursors.Arrow
End Sub