VB Check if record exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raynita
    New Member
    • Jan 2015
    • 1

    VB Check if record exist

    Hi all,

    i have this problem;

    - i want to check if the serial number is exist inside the database, and if its exist, i want to check one of the column name called "FLAG" if its equal to 1 or 0. can someone suggest me on how can i do the code?

    FYI, my current code is ;

    Code:
    Private Sub SRN_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SRN.KeyUp
            If e.KeyCode = Keys.Enter Then
                Dim myconnect As New SqlClient.SqlConnection
                myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\User\My Documents\ETdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
                Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
                mycommand.Connection = myconnect
    
                mycommand.CommandText = "INSERT INTO T3 (SRN, SESSION, EVENT_NAME, SYSTIME, SYSDATE, FLAG) VALUES (' " & Replace(SRN.Text, vbNewLine, "") & "' ,'" & SESSION.Text & " ',' " & EVENT_NAME.Text & "','" & SYSTIME.Text & " ','" & SYSDATE.Text & "','" & "1" & "')"
    
                myconnect.Open()
                mycommand.ExecuteNonQuery()
                myconnect.Close()
                SRN.Text = ""
            End If
        End Sub
    ----

    There is nothing wrong with this code, i manage to add new record, but i need your help to do one more function which is, if the record is exist then i no need to add new record but only to change the value of the "FLAG" from 1 -> 0 or the other way around. i really need your help. thank you.
    Last edited by Rabbit; Jan 15 '15, 04:35 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    For a basic overview, please check out the How to Use a DataBase in Your Program article.

    _Frinny

    Comment

    Working...