0

I would like to add a way to see if an entry is already in a connected database in the following code. If it is then don't add the entry and pop up a dialog saying something to the effect of "already been scanned" and if it is not, proceed as usual.

Using connection As New SqlClient.SqlConnection("Data Source=XXXXX;Initial Catalog=XXXXXX;Integrated Security=True;Pooling=False;Encrypt=False"), _
    cmd As New SqlClient.SqlCommand("INSERT INTO [XXXXX] (TrackingNumber, Date) SELECT @TrackingNumber, @Date WHERE NOT EXISTS (SELECT * FROM([XXXXX])WHERE TrackingNumber = @TrackingNumber AND Date = @Date)", connection)
        cmd.Parameters.Add("@TrackingNumber", SqlDbType.VarChar, 50).Value = TrNum
        cmd.Parameters.Add("@Date", SqlDbType.DateTime, 8).Value = TrDate
        connection.Open()
        cmd.ExecuteNonQuery()
        connection.Close()
End Using

2 Answers 2

1

You should be able to put your inputs into a subquery that checks for redundancy:

INSERT INTO [XXXXXXX] (TrackingNumber, Date) 
    SELECT @TrackingNumber, @Date from DUAL 
    WHERE NOT EXISTS (
        SELECT * 
        FROM [XXXXXXX]
        WHERE TrackingNumber = @TrackingNumber AND Date = @Date)
Sign up to request clarification or add additional context in comments.

9 Comments

@0bfus - For SQL Server just delete the from DUAL you can get rows affected as the return value of cmd.ExecuteNonQuery. If zero nothing was changed.
You don't need the brackets here SELECT * FROM([XXXXX])
alright another error at the same point once the brackets are out and it says "Violation of PRIMARY KEY constraint 'PK_XXXXX'. Cannot insert duplicate key in object 'dbo.XXXXX'. The statement has been terminated." so it sees its a duplicate but now it needs to alert the user it is a duplicate and also not crash the app lol
@Martin Smith the trackingnumber is and also thanks, new here. should i make a new column in the table that auto increments and make that the primary?
@Martin Smith alright it no longer throws any errors but it also does not tell me it has already been enetered , is there a way?
|
0

My VB.NET might be somewhat off but hopefully this should give the general idea!

Dim rowsAffected AS Integer

    Using connection As New SqlClient.SqlConnection("Data Source=XXXXX;Initial Catalog=XXXXXX;Integrated Security=True;Pooling=False;Encrypt=False"), _
        cmd As New SqlClient.SqlCommand("INSERT INTO [XXXXX] (TrackingNumber, Date) SELECT @TrackingNumber, @Date WHERE NOT EXISTS (SELECT * FROM [XXXXX] WHERE TrackingNumber = @TrackingNumber)", connection)
        cmd.Parameters.Add("@TrackingNumber", SqlDbType.VarChar, 50).Value = TrNum
        cmd.Parameters.Add("@Date", SqlDbType.DateTime, 8).Value = TrDate
        connection.Open()
        rowsAffected = cmd.ExecuteNonQuery()
        connection.Close()
    End Using


If rowsAffected = 0 Then
MsgBox "Scanned Already"
Else
MsgBox "Inserted Succesfully"
End If

6 Comments

@Martin Smith One more quick thing, any reason this will not allow letters and only numbers to be entered?
@0bfus I can't see any reason why that would be the case. @TrackingNumber is varchar(50) so should allow letters. Does it give you an error?
@Martin Smith It allows one entry but it stores it in the database as a blank entry with a date and if you try any other entry it says it's inserted succesfully. very odd.
@0bfus - I'd try putting a break point on cmd.Parameters.Add("@TrackingNumber",... to check nothing else in your code is messing with TrNum and if that doesn't shed any light run SQL Profiler to see the statement actually being sent.
@Martin Smith Not sure what i did but it works fine for some reason now. Thanks again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.