|
-
Jul 8th, 2008, 06:11 AM
#1
Thread Starter
Frenzied Member
[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!
-
Jul 8th, 2008, 06:39 AM
#2
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")
-
Jul 8th, 2008, 06:52 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Assign String to Integer Column
No because by using .ToString it prevents you from assign NULL values to the database it returns "".
-
Jul 8th, 2008, 07:13 AM
#4
Re: [2005] Assign String to Integer Column
oleDR.Item("Age") = val(txtage.text)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 8th, 2008, 07:53 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Assign String to Integer Column
Cheers for that Paul, it works fine now.
-
Jul 8th, 2008, 09:03 AM
#6
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.
-
Jul 8th, 2008, 09:22 AM
#7
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
-
Jul 8th, 2008, 09:40 AM
#8
Thread Starter
Frenzied Member
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!
-
Jul 8th, 2008, 10:38 AM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|