Please help me to Checking for Duplicate Record of SQL Database before Inserting & Updating
this my code please may anyone check my code
If class1.sqlcon.state = connectionstate.open then
class1.sqlcon.close()
End If
class1.sqlcon.open()
dim cmd as new sqlcommand
dim Qresault as integer
cmd = new sqlcommand (" SELECT COUNT (*) FROM suppcust WHERE ID='" & TxtID.Text & "' , class1.sqlcon)
Qresault = cmd.executescalar
If Qresault = 0 then
Dim SaveOk As Boolean = winclass.SQLExicute (" Insert Into suppcust (supcust_Code, supcust_Name) VALUES ('" & Txtsupcust_code.text & "' , '" & Txtsupcust_Name.text & "')")
If SaveOk = True Then
Msgbox ("Save OK")
TxtID.Text = ""
Txtsupcust_code.Text = ""
Txtsupcust_Name.Text = ""
FillDataGrid()
End If
MsgBox("Record already exists")
Else
If IsUpdate = True Then
Dim cmd1 As New sqlcommand (" SELECT supcust_Code FROM suppcust WHERE 1 < (SELECT COUNT(*) FROM suppcust WHERE supcust_Code = '" & Txtsupcust_code.text & "' " , class1.sqlcon)
Dim dr As SqlDataReader = cmd1.ExcuteReader()
If dr.HasRows Then
MsgBox ("The Code already exists")
End If
Txtsupcust_code.Focus()
Dim cmd2 As New sqlcommand (" SELECT supcust_Name FROM suppcust WHERE 1 < (SELECT COUNT(*) FROM suppcust WHERE supcust_Name = '" & Txtsupcust_Name.text & "' " , class1.sqlcon)
Dim dr1 As SqlDataReader = cmd2.ExcuteReader()
If dr1.HasRows Then
MsgBox ("The Name already exists")
End If
Txtsupcust_Name.Focus()
Dim UpdateOk As Boolean = winclass.SQLExicute (" UPDATE suppcust SET supcust_Code = '" & Txtsupcust_code.text & "' , supcust_Name = '" & Txtsupcust_Name.text & "' WHERE ID=" & TxtID.Text & "")
If UpdateOk = True Then
MsgBox ("Updated OK")
TxtID.Text = ""
Txtsupcust_code.Text = ""
Txtsupcust_Name.Text = ""
FillDataGrid()
End If
End If
Please i need help with my code if something wrong in code