Click to See Complete Forum and Search --> : Insert statement


dr223
March 30th, 2009, 06:44 AM
Hallo,

I have the following insert into statement. What I want to do is insert the values to gprdsql.TblOracleNos if TxtOracleNo is not empty,therefore if a user logs on and fill the textbox the values are saved to the table. However, if the oracle number exists in gprdsql.TblOracleNos (i.e, the values ) it should not insert the values, presently it creates the same record again with same values (when the save button is clicked) and this creates duplication. Even if a user changes any other field on the form and clicks save button the record is saved again.

Any help is highly appreciated.. Thanks


If TxtOracleNo.Text <> "" Then

Try
query = "INSERT INTO gprdsql.TblOracleNos (Prac_no, Prac_eid, Pay_method, Oracle_no) VALUES ('" & _
TxtPracNo.Text & "', '" & _
TxtPracEid.Text & "', '" & _
CmbPayMethod.Text & "', '" & _
TxtOracleNo.Text & "')"
"Select Prac_no, Prac_eid, Pay_method, Oracle_no From gprdsql.TblOracleNos WHERE not exists Oracle_no"

cmd = New SqlCommand(query, conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "GCPM")
End Try
End If

HawkeyeD
March 30th, 2009, 09:16 AM
Hi dr223,

Not sure if I am understanding your post correctly. You want it to stop inserting if the txtOracleNo.text field is already filled in or if the entire row is already inserted?

Why not clear the form fields after the insert is complete?

DataMiser
March 30th, 2009, 11:12 AM
You could of course do a select first and check to see if you get any results and issue the insert only if the result of the select is empty.