Solved

Convert string numbers into integer

Posted on 2011-02-11
9
605 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

16 Experts available now in Live!

Get 1:1 Help Now