Solved

Convert string numbers into integer

Posted on 2011-02-11
9
602 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
  • 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
 
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

948 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now