|
-
May 3rd, 2013, 02:28 PM
#1
Thread Starter
Junior Member
[RESOLVED] Get result of a query (single value) into a Visual Basic label
I'm performing a SELECT query to get the student ID. I'm getting the following error:
Conversion from String "SELECT est_ID FROM ESTUDIANTE WH" tp type 'Long' is not valid
Here is the code:
Try
Using Con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & globalvariable.dbpath
& ";Integrated Security=True;User Instance=True")
Con.Open()
Using com As New SqlCommand("SELECT est_id FROM ESTUDIANTE WHERE est_nombre ="
& Val(txtNombre.Text) And "est_apellido =" & Val(txtApellido.Text), Con)
Dim textObj = com.ExecuteScalar
If Not textObj Is Nothing AndAlso Not textObj Is DBNull.Value Then
txtID.Text = CInt(textObj)
End If
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
THX...
-
May 3rd, 2013, 05:33 PM
#2
Re: Get result of a query (single value) into a Visual Basic label
Welcome to VBForums 
Read that line a bit more carefully... here is the SQL statement part with colouring to make it a bit clearer what is a string and what isn't:
Code:
"SELECT est_id FROM ESTUDIANTE WHERE est_nombre =" &
Val(txtNombre.Text) And "est_apellido =" & Val(txtApellido.Text)
The word And does not belong where it is (it should be inside the string, and that string should be appended to the rest as you do with the other parts).
-
May 5th, 2013, 10:19 PM
#3
Thread Starter
Junior Member
Re: Get result of a query (single value) into a Visual Basic label
Thanks for the tip. It worked.
Here is the code corrected:
Try
Using Con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" &
globalvariable.dbpath & ";Integrated Security=True;User Instance=True")
Con.Open()
Using com As New SqlCommand("SELECT est_id FROM ESTUDIANTE WHERE est_nombre LIKE '" &
txtNombre.Text & "%'AND est_apellido LIKE '" & txtApellido.Text & "%'", Con)
Dim textObj = com.ExecuteScalar
If Not textObj Is Nothing AndAlso Not textObj Is DBNull.Value Then
txtID.Text = CInt(textObj)
End If
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
-
May 6th, 2013, 04:49 AM
#4
Re: Get result of a query (single value) into a Visual Basic label
No problem 
As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
(this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)
You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|