-
Oct 17th, 2021, 12:22 AM
#1
Thread Starter
Hyperactive Member
[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.
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.
this is my code:
vb.net Code:
Using Conexion_Checar_Turno As New SQLiteConnection("Data Source=C:\Koui\DB2\Base_de_Datos.db;Version=3;")
Conexion_Checar_Turno.Open()
Using CMD_Checar_Turno As New SQLiteCommand("SELECT MAX(Turno) FROM Ventas_Table WHERE Usuario = '" & My.Settings.Actual_Encargado.ToString & "'", Conexion_Checar_Turno)
TextBox2.Text = ""
CMD_Checar_Turno.ExecuteScalar()
If CMD_Checar_Turno Is Nothing Then
TextBox2.Text = "1"
Else
TextBox2.Text = Convert.ToString(CMD_Checar_Turno)
End If
End Using
Conexion_Checar_Turno.Close()
End Using
BTW, I have Option Strict set to ON.
-
Oct 17th, 2021, 12:33 AM
#2
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.
-
Oct 17th, 2021, 01:20 AM
#3
Thread Starter
Hyperactive Member
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|