perform subtration on a field obtained from DB but not getting correct value as result
-
i have a filed saved in sql sever db and its data type is int. i fetch that field from db using this code and assign it to a variable called 'ValueFromDB'
Private Sub GetMyIntDataTypeField() Dim Get_ MyIntDataTypeField_StrSQL As String Dim Get_ MyIntDataTypeField_ObjCmd As SqlCommand Dim strConn As String = ConfigurationManager.ConnectionStrings("MyConnString").ToString() Dim MyCon As SqlConnection MyCon = New SqlConnection(strConn) Get_ MyIntDataTypeField_StrSQL = "Select MyIntField from MyTable" MyCon.Open() Get_ MyIntDataTypeField_ObjCmd = New SqlCommand(Get_ MyIntDataTypeField_StrSQL, MyCon) ValueFromDB = Get_ MyIntDataTypeField_StrSQL_ObjCmd.ExecuteScalar MyCon.Close() End Sub
after obtaining this value i need to perform a simple subtraction, like a = ValueFromDB - 17. after subtraction i am getting, a = -17. is this because "ValueFromDB" from DB in string format? -
i have a filed saved in sql sever db and its data type is int. i fetch that field from db using this code and assign it to a variable called 'ValueFromDB'
Private Sub GetMyIntDataTypeField() Dim Get_ MyIntDataTypeField_StrSQL As String Dim Get_ MyIntDataTypeField_ObjCmd As SqlCommand Dim strConn As String = ConfigurationManager.ConnectionStrings("MyConnString").ToString() Dim MyCon As SqlConnection MyCon = New SqlConnection(strConn) Get_ MyIntDataTypeField_StrSQL = "Select MyIntField from MyTable" MyCon.Open() Get_ MyIntDataTypeField_ObjCmd = New SqlCommand(Get_ MyIntDataTypeField_StrSQL, MyCon) ValueFromDB = Get_ MyIntDataTypeField_StrSQL_ObjCmd.ExecuteScalar MyCon.Close() End Sub
after obtaining this value i need to perform a simple subtraction, like a = ValueFromDB - 17. after subtraction i am getting, a = -17. is this because "ValueFromDB" from DB in string format?Meax wrote:
after obtaining this value i need to perform a simple subtraction, like a = ValueFromDB - 17. after subtraction i am getting, a = -17. is this because "ValueFromDB" from DB in string format?
I think execute scalar returns the type of object. Set a break point on the line of code to check. If you must use VB.Net I suggest setting option strict and option explicit to true. Just cast the result as an integer.
I didn't get any requirements for the signature