red_ghost
Member
Okay, I have a numeric feild in SQL CE and I read it into vb.net and try putting it into a decimal field. I end up getting invalid cast exception. I am 100% sure the field is not DBNULL or nothing. I tried that already. I cannot debug this application because it is on our server at work. Here is the code:
Dim oqty As Decimal = 0
strsql = "SELECT ORD_QTY FROM ORDERS WHERE ORD_ORDERNO='" & txtOrder.Text & "' AND ORD_ITM='" & item & "'"
If SqlCeGetValue(glb_connstring, strsql, oqty, strerr) = False Then
MessageBox.Show("Error obtaining qty: " & strerr)
txtbar.Text = ""
txtbar.Focus()
Exit Sub
End If
Here is the code for the function:
Public Function SqlCeGetValue(ByVal cnnString As String, _
ByVal selectTxt As String, _
ByRef retObject As Object, _
ByRef errString As String) As Boolean
Dim ret_status As Boolean = True
Dim connection As New SqlCeConnection(cnnString)
Dim command As New SqlCeCommand(selectTxt, connection)
Try
connection.Open()
retObject = command.ExecuteScalar()
Catch exSQL As SqlCeException
errString = exSQL.Message
ret_status = False
Catch ex As Exception
errString = ex.Message
ret_status = False
Finally
If Not connection Is Nothing Then
connection.Close()
End If
End Try
Return (ret_status)
End Function
Dim oqty As Decimal = 0
strsql = "SELECT ORD_QTY FROM ORDERS WHERE ORD_ORDERNO='" & txtOrder.Text & "' AND ORD_ITM='" & item & "'"
If SqlCeGetValue(glb_connstring, strsql, oqty, strerr) = False Then
MessageBox.Show("Error obtaining qty: " & strerr)
txtbar.Text = ""
txtbar.Focus()
Exit Sub
End If
Here is the code for the function:
Public Function SqlCeGetValue(ByVal cnnString As String, _
ByVal selectTxt As String, _
ByRef retObject As Object, _
ByRef errString As String) As Boolean
Dim ret_status As Boolean = True
Dim connection As New SqlCeConnection(cnnString)
Dim command As New SqlCeCommand(selectTxt, connection)
Try
connection.Open()
retObject = command.ExecuteScalar()
Catch exSQL As SqlCeException
errString = exSQL.Message
ret_status = False
Catch ex As Exception
errString = ex.Message
ret_status = False
Finally
If Not connection Is Nothing Then
connection.Close()
End If
End Try
Return (ret_status)
End Function