Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how i can know if the phone number exist in ms database or not ?

I'm using this code but i have error , i think i need to get all phone number as array and search in array if the number exist or not .

Dim number As String
    con.Open()
    Dim sql As String = "select cphone from cust "
    Dim cmd As New OleDbCommand(sql, con)
    Dim dr As OleDbDataReader
    dr = cmd.ExecuteReader
    While dr.Read
        number = dr(5)
    End While
    con.Close()

    If TextBox5.Text = number Then
        MsgBox("exist")
    Else
        MsgBox("NOT-exist")
    End If
Posted
Updated 13-May-15 11:16am
v2
Comments
Rabeea Qabaha 13-May-15 17:43pm    
what ?

What is your error? I can tell you now that unless the phone number you are looking for is the last entry in the table, you will never find it. You should be doing something like this:

VB
Dim number As String
Dim found As Boolean
con.Open()
Dim sql As String = "select cphone from cust "
Dim cmd As New OleDbCommand(sql, con)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader

found = false

While dr.Read
    number = dr(5)
    
    If TextBox5.Text = number Then
        found = True
        Exit While
    End If
End While
con.Close()

If found Then
    MsgBox("exist")
Else
    MsgBox("NOT-exist")
End If
 
Share this answer
 
Let the database do the work for you:
VB.NET
Using con As New OleDbConnection("YOUR CONNECTION STRING HERE")
    Using cmd As New OleDbCommand("SELECT TOP 1 cphone FROM cust WHERE cphone = ?", con)
        
        ' OleDb doesn't use named parameters, so only the order matters here:
        cmd.Parameters.AddWithValue("p0", TextBox5.Text)
        
        con.Open()
        Dim result As Object = cmd.ExecuteScalar()

        If result IsNot Nothing Then
            MsgBox("exist")
        Else
            MsgBox("NOT-exist")
        End If
    End Using
End Using
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900