Link to home
Start Free TrialLog in
Avatar of salmanfazal
salmanfazalFlag for United Arab Emirates

asked on

vb.net Date Problem

I have made an ASP application. I deployed it on the server. Application is working properly till the time i press a button which executes an INSERT SQL Statement. When i press this it gives the following message : "Sys.WebForms.PageRequestManagerServerErrorException:String was not recongnized as a valid Date Time."

While doing the insert i convert the text into data as follows:
Convert.ToDateTime(TextBox7.Text).ToString("MM/dd/yyyy")


This message dont appears on the pc where i developed this application. It appears only on the server.

regional setting for the machine where i developed this application and the server machine is "dd/MM/yyyy"

database is sql server 2005
What could be the reason behind this error?
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

when you are inserting or updating the database with the date then before sending it to the database convert the date format to yyyy-MM-dd HH:mm:ss
convert it like this Convert.ToDateTime(TextBox7.Text).ToString("yyyy/MM/dd")
try again
Avatar of salmanfazal

ASKER

Its still the same. Working on the development machine but not on the deployment machine.
Avatar of Wayne Taylor (webtubbs)
Instead of "converting" a String to a Date, parse it using ParseExact, which accepts an input format....

    Date.ParseExact(TextBox7.Text, "dd/MM/yyyy", Nothing)

Wayne
try this one
DateTime.Parse(TextBox7.Text, System.Globalization.CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat).ToStirng()
If apply the above syntax, it gives the following error message: "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."

I tried changing the format to MM/dd/yyyy, VB throws the following message:
"The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar."
what is the value in the text box which you are trying to convert
because the error says that its not between the range of values expected
i am not typing anything,i am picking up the values from the calendar. lets say "21/06/2009"
Another thing is why its happening only on the server machine .
i really dont know why its happening on the server but i am assuming that the textbox7.text is empty
the value is not getting set in the textbox or its not coming back on the server side
but its working fine on my machine and even on the server if i put the date with in 12 days then it will work. if i put after 12th date it will not work, so it shows that textbox is not empty
then its definitely the way your calendar control is returning the values
if your regional setting is MM-dd-yyyy and your calender is returning the date in dd-MM-yyyy then the dates greater than 12 will not work because 13 and above are not valid months
If you are using an ASP.Net Calendar control, why not simply use something like this?

    Calendar1.SelectedDate.ToString("MM/dd/yyyy")

BTW - to avoid any problems with date formats, I always use date format "dd-MMM-yy". Doing so avoids any confusion.

Wayne
Referring to webtubbs comments : You mean to say i need to put this format (dd-MMM-yy) in the text box or the regional settings.?
Neither. Use it in your SQL statement.
And where does the Textbox come into it? Are you sending the date to the Textbox from the Calendar?
ASKER CERTIFIED SOLUTION
Avatar of salmanfazal
salmanfazal
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial