Solved

Convert string numbers into integer

Posted on 2011-02-11
9
615 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:mlg101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 3

Accepted Solution

by:
chrisrbloom earned 500 total points
ID: 34873871
You need to first convert the querystrings into integers, then add a variable of type Date to the command parameter (assuming your input parameter is of type date)

        Dim yr As Integer = CInt(Request.QueryString("Year"))
        Dim mo As Integer = CInt(Request.QueryString("Month"))
        Dim Da As Integer = CInt(Request.QueryString("Day"))
        Dim BirthDate As New Date(yr, mo, Da)

        Cmd.Parameters.Add(New SqlParameter("DOB", BirthDate))

Hope this helps.
0
 
LVL 1

Author Comment

by:mlg101
ID: 34874195
on debug, I get this error:
Year, Month, and Day parameters describe an un-representable DateTime.
0
 
LVL 3

Expert Comment

by:chrisrbloom
ID: 34874289
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?


0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 1

Author Comment

by:mlg101
ID: 34874499
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


0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34874706
Does this work?

Dim DOB AS Date = Date.ParseExact(mo & "/" & da & "/" & yr, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
0
 
LVL 1

Author Comment

by:mlg101
ID: 34874762
overload resolution tryparseexact does not accept this number of arguments.
0
 
LVL 1

Assisted Solution

by:mlg101
mlg101 earned 0 total points
ID: 34874917
I did this and it worked fine...not sure why.

                Dim yr As Integer = CInt(Request.QueryString("Year"))
                Dim mo As Integer = CInt(Request.QueryString("Month"))
                Dim Da As Integer = CInt(Request.QueryString("Day"))
                Dim BirthDate As String = (mo & "/" & Da & "/" & yr)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34879779
>I did this and it worked fine...not sure why.
Because BirthDate is of type String now.
0
 
LVL 1

Author Closing Comment

by:mlg101
ID: 34904859
USed my comment because it was worked out with the help of the expert.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question