Question conversion from string to integer is invalid

mkkb917

Member
Joined
Nov 24, 2012
Messages
18
Location
Pakistan
Programming Experience
Beginner
sir i am new in here i got an error when i try to search a phone no from the database the error is "conversion from string phoneno to type integer is invalid the phoneno with type nvarchar(11) and the code is Dim Con As SqlConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Vu Data\CS-619 Project\Miscall Log In\DBMSMissxcall.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True") Con.Close() ' '''' [ this code is generating error] Dim cmd As New SqlClient.SqlCommand Dim DaLogin As New SqlClient.SqlDataAdapter Dim dtable As New DataTable cmd.CommandText = "SELECT * from Users WHERE PhoneNo = @loginuser" Dim user As String = txtloginuser.Text cmd.Parameters.AddWithValue("@loginuser", user) Con.Open() cmd.Connection = Con DaLogin.SelectCommand = cmd DaLogin.Fill(dtable) Dim result As Integer result = dtable.Rows.Count If result > 0 Then MsgBox("User found now open the conenction", "Valid User", vbOKOnly + vbInformation) btnconnect.Enabled = True Else MsgBox("User not registered in database. try again or create a new account", "Invalid Phone No", vbOKOnly + vbCritical) txtloginuser.Text = Focused() End If plz tell me where i am doing mistake
 
Making this readable as its pretty shocking. Without even reading your code though, why would you make a phone number an integer? Some phone numbers look like this +44779123445, others look like this (01234) 555 454. These aren't integers.

I am new in here i got an error when i try to search a phone no from the database the error is

"conversion from string phoneno to type integer is invalid the phoneno with type nvarchar(11)

and the code

Dim Con As SqlConnection = New SqlClient.SqlConnection("Data  Source=.\SQLEXPRESS;AttachDbFilename=D:\Vu Data\CS-619   Project\Miscall  Log In\DBMSMissxcall.mdf;Integrated Security=True;Connect  Timeout=30;User Instance=True")
             
Con.Close()               '  '''' [ this code is generating error]
             
Dim cmd As New  SqlClient.SqlCommand
             
Dim DaLogin As New  SqlClient.SqlDataAdapter             
Dim dtable As New DataTable                          

cmd.CommandText = "SELECT * from Users WHERE PhoneNo =  @loginuser"             
Dim user As String = txtloginuser.Text

cmd.Parameters.AddWithValue("@loginuser", user)
Con.Open()
cmd.Connection = Con
DaLogin.SelectCommand = cmd

DaLogin.Fill(dtable)
Dim result As Integer

result = dtable.Rows.Count
if result > 0 Then

MsgBox("User found  now open the conenction", "Valid User", vbOKOnly + vbInformation)
btnconnect.Enabled = True
Else
MsgBox("User not registered in database. try again or create a new  account", "Invalid Phone No", vbOKOnly + vbCritical)
txtloginuser.Text = Focused()
End If


plz tell me where i am doing mistake
 
Last edited:
Back
Top