Link to home
Start Free TrialLog in
Avatar of mlg101
mlg101Flag for United States of America

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.PageRequestManagerServerErrorException: Conversion failed when converting date and/or time from character string.
ASKER CERTIFIED SOLUTION
Avatar of Chris Bloom
Chris Bloom
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mlg101

ASKER

on debug, I get this error:
Year, Month, and Day parameters describe an un-representable DateTime.
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?


Avatar of mlg101

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


Avatar of Nasir Razzaq
Does this work?

Dim DOB AS Date = Date.ParseExact(mo & "/" & da & "/" & yr, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Avatar of mlg101

ASKER

overload resolution tryparseexact does not accept this number of arguments.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>I did this and it worked fine...not sure why.
Because BirthDate is of type String now.
Avatar of mlg101

ASKER

USed my comment because it was worked out with the help of the expert.