Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. perform subtration on a field obtained from DB but not getting correct value as result

perform subtration on a field obtained from DB but not getting correct value as result

Scheduled Pinned Locked Moved ASP.NET
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Meax
    wrote on last edited by
    #1

    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?

    T 1 Reply Last reply
    0
    • M Meax

      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?

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups