|
-
Nov 17th, 2002, 08:18 PM
#1
Thread Starter
Addicted Member
How to test if add or update
Hi all,
In a datagrid, how do you test if the record is being added or updated.
What I have now is :
VB Code:
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
Dim rowindex As Integer = DataGrid2.CurrentRowIndex
Dim struserid As String = DataGrid2.Item(rowindex, 0)
Dim struserpass As String = DataGrid2.Item(rowindex, 1)
Dim sqlcmd As String
Dim ifexistcmd As String
ifexistcmd = "select count(*) from usermaster" & _
" where userid = " & obj.PrepareStr(struserid)
If obj.recordexist(ifexistcmd) Then
If MessageBox.Show(Me, "Confirm Update of User " & struserid, "Update Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign) = DialogResult.OK Then
Try
sqlcmd = "update usermaster set userpass=" & _
obj.PrepareStr(struserpass) & " where userid = " & _
obj.PrepareStr(struserid)
obj.UpdateRecord(sqlcmd)
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try
End If
Else 'add
If MessageBox.Show(Me, "Confirm Addition of User " & struserid, "Add Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign) = DialogResult.OK Then
Try
sqlcmd = "insert into usermaster (userid, userpass, usertype) values (" & obj.PrepareStr(struserid) & "," & obj.PrepareStr(struserpass) & "," & "'" & "M" & "'" & ")"
obj.AddRecord(sqlcmd)
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try
End If
End If
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|