Results 1 to 3 of 3

Thread: [RESOLVED] ExecuteScalar() returns weird result

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Resolved [RESOLVED] ExecuteScalar() returns weird result

    Hello!

    I'm trying to get an integer value from a table called Ventas_Table and then put this value in a Texbox, the code doesn't give me any erros, but the TextBox returns this text "System.Data.SQLite.SQLiteCommand" instead of the integer I want, and I'm pretty sure that my table has values in it.
    Name:  Captura de pantalla 2021-10-17 001536.png
Views: 145
Size:  22.4 KB

    I even set an inspection point on "CMD_Checar_Turno.ExecuteScalar()" and I get a double type value of 2.
    but the TextBox is not showing it.
    Name:  Captura de pantalla 2021-10-17 001426.jpg
Views: 145
Size:  14.3 KB


    this is my code:
    vb.net Code:
    1. Using Conexion_Checar_Turno As New SQLiteConnection("Data Source=C:\Koui\DB2\Base_de_Datos.db;Version=3;")
    2.             Conexion_Checar_Turno.Open()
    3.             Using CMD_Checar_Turno As New SQLiteCommand("SELECT MAX(Turno) FROM Ventas_Table WHERE Usuario = '" & My.Settings.Actual_Encargado.ToString & "'", Conexion_Checar_Turno)
    4.                 TextBox2.Text = ""
    5.                 CMD_Checar_Turno.ExecuteScalar()
    6.                 If CMD_Checar_Turno Is Nothing Then
    7.                     TextBox2.Text = "1"
    8.                 Else
    9.                     TextBox2.Text = Convert.ToString(CMD_Checar_Turno)
    10.                 End If
    11.             End Using
    12.             Conexion_Checar_Turno.Close()
    13.         End Using

    BTW, I have Option Strict set to ON.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,760

    Re: ExecuteScalar() returns weird result

    You're ignoring the basics. Firstly, look at this line:
    Code:
    TextBox2.Text = Convert.ToString(CMD_Checar_Turno)
    That is going to take the highlighted part, convert that to a String and display the result in the TextBox. What is the highlighted part? It's a SQLiteCommand object. Why would you expect converting that to a String would yield the result of a query? For the vast majority of types, converting them to a String simply returns the name of the type and that's exactly what you're seeing.

    If what you want is the result of the ExecuteScalar function then that's what you have to use look at this line:
    Code:
    CMD_Checar_Turno.ExecuteScalar()
    Why would you expect executing a function and just ignoring its return value to enable you to do anything useful with that return value? Like I said, this is purely fundamentals and nothing to do with ExecuteScalar specifically. If you want to display the result of the ExecuteScalar function in the TextBox then assign the result of that function to the Text property, not the result of some other function that gets the type name of an object as a String.

  3. #3

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: ExecuteScalar() returns weird result

    Hi jmc.
    I had never used this kind of syntax to get an scalar value before.
    I just learned something new today.

    vb.net Code:
    1. TextBox2.Text = CMD_Checar_Turno.ExecuteScalar().ToString

    Thank you.

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