views:

96

answers:

2

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

            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
+1  A: 

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)
Matthew Flynn
I get an SqlException was unhandled error at cmd.ExecuteNonQuery for the invalid object name DUAL. where in the original code would a sub query be put?
0bfus
@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.
Martin Smith
Using the code above i get an sql exception was unhandled error at the cmd.ExecuteNonQuery() saying incorrect syntax near ). i updated the code in my question to show the changes i have so far.
0bfus
You don't need the brackets here `SELECT * FROM([XXXXX])`
Martin Smith
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
0bfus
@0bfus - If you use @Martin in your comment I'll be aware that you replied! What is the PK of the table?
Martin Smith
@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?
0bfus
@Obfus - No just remove ` AND Date = @Date` from the WHERE clause. Matthew must have assumed that was part of the PK.
Martin Smith
@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?
0bfus
@Matthew Flynn thanks for the help!
0bfus
@Martin Smith - Thanks for working out the kinks!
Matthew Flynn
A: 

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
Martin Smith
That works! thanks a bunch!
0bfus
@Martin Smith One more quick thing, any reason this will not allow letters and only numbers to be entered?
0bfus
@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
@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
@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
@Martin Smith Not sure what i did but it works fine for some reason now. Thanks again.
0bfus
@Martin Smith Do you by any chance know how to make a result from a SELECT query display in a MsgBox?
0bfus