?
Solved

Convert string numbers into integer

Posted on 2011-02-11
9
Medium Priority
?
623 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 2000 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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