Search code examples
vb.netsqliteoledbexecutescalar

Returning 0 instead of the actual value


I use the same SQL query within SQLiteStudio 3.1.0 and it works with no issues using the same parameter that I pass in. When I use the same SQL and use the code below in my VB project it returns 0 instead of right value.

Private Function GetData() As Object
     Dim sql As String = "Select Data from Table where Option = ?"
     Return FunctionObject(sql, New List(Of Object) From {"This is a Test"})
End Sub

Public Function FunctionObject(ByVal sql As String, ByVal parameters As List(Of Object)) As Object
    Using sqlconnection As New SQLiteConnection(ConnectionString)
          sqlconnection.Open()
          Using Cmd As New SQLiteCommand(sql, sqlconnection)
                Dim par As SQLiteParameter
                For Each parm As Object In parameters
                    par = Cmd.CreateParameter()
                    par.Value = parm
                   Cmd.Parameters.Add(par)
                Next
          FunctionObject = Cmd.ExecuteScalar()
          End Using
   End Using
End Function

I pass in list of objects into the method of the parameters. The connection string works because it is making other sql queries work. I have an identical set of code that uses OleDb that is built the exact same way as this and it works with this same SQL statement. So is there something I am missing that can make the behavior different from the SQLite and the OleDb connection and executeScalar since one returns 0 and the other returns the correct string?


Solution

  • It ended up being a Database field being wrong. It was suppose to be a VarChar(200) but instead it was a Numeric(200). So if you are getting issues with a wrong data being returned then you should look at the table itself. Something I learned from SQLite is it returns a vbCr so you should add .ToString().Replace(vbCr, String.Empty) to the ExecuteScalar().