Results 1 to 4 of 4

Thread: [RESOLVED] Get result of a query (single value) into a Visual Basic label

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    Resolved [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...

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width