Results 1 to 7 of 7

Thread: [RESOLVED] How do you check if record is available in the database

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    46

    Resolved [RESOLVED] How do you check if record is available in the database

    I have this code that Deletes a record by its IP address inputted by the user. How do you check if that record is available? how do evaluate first that the IP address inputted is not in the database before deleting anything? in vb 6.0 I used to do this line:

    If rs.EOF And rs.BOF Then

    but it doesn't work anymore in vb 2008

    delete IP Code:
    1. If TextBox1.Text = "" Then
    2.                     MsgBox("Input IP Address", MsgBoxStyle.OkOnly, Title:="")
    3.                 Else
    4.                     Dim myConnection As OleDbConnection = New OleDbConnection
    5.                     Dim myCommand As OleDbCommand = New OleDbCommand
    6.                     myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Network_Info.mdb"
    7.  
    8.                     myConnection.Open()
    9.  
    10.                     myCommand.Connection = myConnection
    11.                     myCommand.CommandText = "DELETE * from Computer_Connection_Info WHERE IP_Address ='" & TextBox1.Text & "'"
    12.  
    13.                     myCommand.ExecuteNonQuery()
    14. End If

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: How do you check if record is available in the database

    Check for the existance of the IP Address and delete the entry

    Code:
    myCommand.CommandText = "If Exists(Select 1 From Computer_Connection_Info Where P_Address ='" & TextBox1.Text & "'"  )DELETE * from Computer_Connection_Info WHERE IP_Address ='" & TextBox1.Text & "'"
    I would suggest you to go for the OLEDB parameter rather than the String Concatenation.

    If any Record is Deleted the
    myCommand.ExecuteNonQuery()
    will return the number of rows affected
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    46

    Re: How do you check if record is available in the database

    How do you apply OLEDB parameters?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: How do you check if record is available in the database

    Quote Originally Posted by korae View Post
    How do you apply OLEDB parameters?
    The example below is simply a template which is doing a insert, adjust this to do a delete instead.

    AddWithValue
    Param 1 is the param name
    Param 2 is the value to place into it.

    Create a command object
    Code:
    Dim objCommand As OleDbCommand = New OleDbCommand()
    Fill in the command, the value section here will be populated next.
    Code:
          objCommand.CommandText = _
             "INSERT INTO Customers " & "(FirstName, LastName,Address) " & "VALUES(@firstname,@lastname,@address);"
    Do the values
    Code:
          objCommand.Parameters.AddWithValue("@firstname", p.FirstName)
          objCommand.Parameters.AddWithValue("@lastname", p.LastName)
          objCommand.Parameters.AddWithValue("@address", p.Address)

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: How do you check if record is available in the database

    Quote Originally Posted by korae View Post
    How do you apply OLEDB parameters?
    Also if using VS2008 or higher you can write the entire statement in an XElement and convert to a string as you are assigning the statement to the command commandtext as follows.

    Code:
            Dim myCommand As OleDbCommand = New OleDbCommand()
            myCommand.CommandText = _
            <SQL>
                DELETE 
                    FROM Computer_Connection_Info 
                WHERE IP_Address ='<%= TextBox1.Text %>'
            </SQL>.Value

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    46

    Re: How do you check if record is available in the database

    wait guys. My delete code above works fine. What I want to do is add some code after my else that will check first if the inputted IP address is available so that I can add a msgbox that that IP address does not exist if its not available. I'm really not familiar with vb so I'm having a hard time understanding about what you are referring to. Just a simple checking method will do.

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: How do you check if record is available in the database

    Quote Originally Posted by korae View Post
    wait guys. My delete code above works fine. What I want to do is add some code after my else that will check first if the inputted IP address is available so that I can add a msgbox that that IP address does not exist if its not available. I'm really not familiar with vb so I'm having a hard time understanding about what you are referring to. Just a simple checking method will do.
    Code:
    Dim myCommand As OleDbCommand = New OleDbCommand()
            myCommand.CommandText = _
            <SQL>
                SELECT COUNT(*)  
                    FROM Computer_Connection_Info 
                WHERE IP_Address ='<%= TextBox1.Text %>'
            </SQL>.Value
    
    Dim Count as Integer = myCommand.ExecuteScalar()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width