deezee deezee - 1 month ago 5
MySQL Question

error: Unable to cast object of type 'System.Int64' to type 'System.Data.Odbc.OdbcDataReader'

I'm trying to count records on my saved datas.

Here is my code:

Dim pl_qry As String
Dim pl_cmd As New Odbc.OdbcCommand
Dim pl_dr As Odbc.OdbcDataReader

If txtPurpose.Text = "PERSONAL LOAN" Then
pl_qry = "select count(purpose) from details where borrower_id = '" & bor_id & "' and purpose = '" & "PERSONAL LOAN" & "'"
pl_cmd.CommandText = pl_qry
pl_cmd.Connection = con
pl_dr = pl_cmd.ExecuteScalar

pl_dr.Read()
If Not pl_dr.IsDBNull(0) Then
MsgBox(pl_dr.GetString(0))
End If
End If


I got my error in this line

pl_dr = pl_cmd.ExecuteScalar

Answer

If you have check the ExecuteScalar method, you will notice it will return the first column of the first row of your SQL.

Problem is you have assigned the result to 'OdbcDataReader`

Dim pl_dr As Odbc.OdbcDataReader

but maybe you can just get the result directly:

Int64 result = Convert.ToInt64(cmd.ExecuteScalar())
Comments