Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Assign String to Integer Column

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Resolved [RESOLVED] [2005] Assign String to Integer Column

    Hi Peeps,

    I am sure this is a really easy question but I don't know the correct solution. Basically I have a form with several text boxes on and if the user leaves one or more blank and I come to do the assignment to the database columns I get a conversion error.

    oleDR.Item("Age") = txtage.text

    If txtage.txt is left blank the above gives an error. How can I make sure that when I do the assignment it handles blank strings. If its a string column I use .ToString like so and this works for strings:-

    oleDR.Item("Name") = txtName.text.ToString

    Any help please,

    Jiggy!

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Assign String to Integer Column

    This txtName.text.ToString seems redundant.

    Code:
            Dim intAsString = "1"
            Dim intAsInt As Integer
            If Integer.TryParse(intAsString, intAsInt) Then Debug.WriteLine(intAsInt) Else Debug.WriteLine("ERR")
            intAsString = "A"
            If Integer.TryParse(intAsString, intAsInt) Then Debug.WriteLine(intAsInt) Else Debug.WriteLine("ERR")
            intAsString = ""
            If Integer.TryParse(intAsString, intAsInt) Then Debug.WriteLine(intAsInt) Else Debug.WriteLine("ERR")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] Assign String to Integer Column

    No because by using .ToString it prevents you from assign NULL values to the database it returns "".

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: [2005] Assign String to Integer Column

    oleDR.Item("Age") = val(txtage.text)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] Assign String to Integer Column

    Cheers for that Paul, it works fine now.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Assign String to Integer Column

    personally i don't use val anymore for this reason

    Code:
            intAsString = "16 17 th street"
            If Integer.TryParse(intAsString, intAsInt) Then Debug.WriteLine(intAsInt) Else Debug.WriteLine("ERR")
            intAsInt = Convert.ToInt32(Val(intAsString))
            Debug.WriteLine(intAsInt)
    also, doesn't val return double?

    do you have
    option strict on
    option explicit on
    Last edited by dbasnett; Jul 8th, 2008 at 09:12 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Assign String to Integer Column

    Code:
            'this
            If String.IsNullOrEmpty(TextBox1.Text) Then oleDR.Item("Name") = String.Empty _
               Else oleDR.Item("Name") = txtName.text
            'instead of this
            'oleDR.Item("Name") = txtName.text.ToString
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [RESOLVED] [2005] Assign String to Integer Column

    Thanks dbasnett for your answers. You would have thought you could have simply done:-

    oleDR.Item("Age") = txtAge.Text.ToInt32

    But maybe I want it to easy! It seems a lot of code for one item when I have 20 to check for.

    Cheers again,

    Jiggy!

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Assign String to Integer Column

    you should turn these on if you don't have them on

    option strict on
    option explicit on

    it might save you headaches in the future.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width