Solved

Convert string numbers into integer

Posted on 2011-02-11
9
600 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expando 4 36
MSSQL 2008 with mySQL webservers 7 28
Hidden Field Value 10 37
Asp.net mvc 5 5 23
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…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

14 Experts available now in Live!

Get 1:1 Help Now