mlg101
asked on
Convert string numbers into integer
I need to convert string numbers into integer, so they can be inserted into date/time field of a sql database. I am getting the Year, Month, Day from querystring from previous page, like:
page.aspx?Year=1996&Month= 4&Day=23
Dim yr As Integer = Request.QueryString("Year" )
Dim mo As Integer = Request.QueryString("Month ")
Dim Da As Integer = Request.QueryString("Day")
Here is the insert parameter:
Cmd.Parameters.Add(New SqlParameter("DOB", mo & "/" & Da & "/" & yr))
I get an error on insert:
Error: Sys.WebForms.PageRequestMa nagerServe rErrorExce ption: Conversion failed when converting date and/or time from character string.
page.aspx?Year=1996&Month=
Dim yr As Integer = Request.QueryString("Year"
Dim mo As Integer = Request.QueryString("Month
Dim Da As Integer = Request.QueryString("Day")
Here is the insert parameter:
Cmd.Parameters.Add(New SqlParameter("DOB", mo & "/" & Da & "/" & yr))
I get an error on insert:
Error: Sys.WebForms.PageRequestMa
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you getting it from the database or from .net?
The debugger will throw an exception if the year is less than 1 or greater than 9999.-or- month is less than 1 or greater than 12.-or- day is less than 1 or greater than the number of days in month.
If you are not correctly doing the integer conversion from the querystring, it's likely that is what is throwing the exception.
can you insert a breakpoint and see what the actual values of yr,mo and da are?
The debugger will throw an exception if the year is less than 1 or greater than 9999.-or- month is less than 1 or greater than 12.-or- day is less than 1 or greater than the number of days in month.
If you are not correctly doing the integer conversion from the querystring, it's likely that is what is throwing the exception.
can you insert a breakpoint and see what the actual values of yr,mo and da are?
ASKER
I can't find the value using Visual Web developer. I'm not good enough to find it.
I am using sql server 2008, "Date" field, and not a "DateTime" field, if that makes a difference.
If I do this: Dim BirthDate As New Date(mo & "/" & Da & "/" & yr)
Then I get a different error saying "Conversion from string "12/29/1938" to type 'Long' is not valid."
So in that case, it gets the data of "12/29/1939", which is what I inputted into the birth day fields
I am using sql server 2008, "Date" field, and not a "DateTime" field, if that makes a difference.
If I do this: Dim BirthDate As New Date(mo & "/" & Da & "/" & yr)
Then I get a different error saying "Conversion from string "12/29/1938" to type 'Long' is not valid."
So in that case, it gets the data of "12/29/1939", which is what I inputted into the birth day fields
Does this work?
Dim DOB AS Date = Date.ParseExact(mo & "/" & da & "/" & yr, "MM/dd/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure)
Dim DOB AS Date = Date.ParseExact(mo & "/" & da & "/" & yr, "MM/dd/yyyy", System.Globalization.Cultu
ASKER
overload resolution tryparseexact does not accept this number of arguments.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>I did this and it worked fine...not sure why.
Because BirthDate is of type String now.
Because BirthDate is of type String now.
ASKER
USed my comment because it was worked out with the help of the expert.
ASKER
Year, Month, and Day parameters describe an un-representable DateTime.