[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!
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")
Re: [2005] Assign String to Integer Column
No because by using .ToString it prevents you from assign NULL values to the database it returns "".
Re: [2005] Assign String to Integer Column
oleDR.Item("Age") = val(txtage.text)
Re: [2005] Assign String to Integer Column
Cheers for that Paul, it works fine now.
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
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
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!
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.