Hi all,

In a datagrid, how do you test if the record is being added or updated.

What I have now is :

VB Code:
  1. Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
  2.         Dim rowindex As Integer = DataGrid2.CurrentRowIndex
  3.         Dim struserid As String = DataGrid2.Item(rowindex, 0)
  4.         Dim struserpass As String = DataGrid2.Item(rowindex, 1)
  5.         Dim sqlcmd As String
  6.         Dim ifexistcmd As String
  7.         ifexistcmd = "select count(*) from usermaster" & _
  8.                     " where userid = " & obj.PrepareStr(struserid)
  9.         If obj.recordexist(ifexistcmd) Then
  10.             If MessageBox.Show(Me, "Confirm Update of User " & struserid, "Update Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign) = DialogResult.OK Then
  11.                 Try
  12.                     sqlcmd = "update usermaster set userpass=" & _
  13.                             obj.PrepareStr(struserpass) & " where userid = " & _
  14.                             obj.PrepareStr(struserid)
  15.  
  16.                     obj.UpdateRecord(sqlcmd)
  17.  
  18.                 Catch Exp As Exception
  19.                     MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
  20.                 End Try
  21.             End If
  22.         Else 'add
  23.             If MessageBox.Show(Me, "Confirm Addition of User " & struserid, "Add Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign) = DialogResult.OK Then
  24.                 Try
  25.                     sqlcmd = "insert into usermaster (userid, userpass, usertype) values (" & obj.PrepareStr(struserid) & "," & obj.PrepareStr(struserpass) & "," & "'" & "M" & "'" & ")"
  26.                     obj.AddRecord(sqlcmd)
  27.  
  28.                 Catch Exp As Exception
  29.                     MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
  30.                 End Try
  31.             End If
  32.         End If
  33.     End Sub

The problem here is whenever I scroll down to the last row and enter a userid that is already existing, it runs the update block since the userid is already existing, leaving me in the grid with 2 entry of the same userid. And upon refreshing the grid, will display the updated userid.

How do I accomplish the same task that if the user inputs a duplicate userid, it will display an error "Duplicate userid" without running the update block.

Any ideas???

Thanks in advance,
Marivic