Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am creating a webpage to enter a date to sql server 2012.

When I run my application from visual studio, it works fine and saves the datetime.

But when I publish on the same machine the error shows up.

At the time of sql installtion, the machine time was set as english(US). which I have now changed to english(India) for date format: dd-MM-yyyy

Following is the parameter for sql:
daCreate.InsertCommand.Parameters.Add(New SqlParameter("@ROUTTD", DateTime.Parse(txtTargetDate.Text)))


Following is the function used for verifying the date format as I also allow manual date entry in text box. Also, I have used Jquery datetime picker.

VB
Public Function fnDateValidation(ByVal textfordate)
        fnDateValidation = 0

        Dim test As Date
        If Date.TryParseExact(textfordate, "dd-MM-yyyy", _
                              System.Globalization.CultureInfo.CurrentCulture, _
                              Globalization.DateTimeStyles.None, test) Then
            fnDateValidation = 1
            'TODO: ok
        Else
            fnDateValidation = 0
            'TODO: not ok
        End If
        Return fnDateValidation
    End Function


Pls suggest.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 11:21am    
First of all, don't use string types, like varchar, to store time. Use time data types, such as DATE...
—SA
Richard MacCutchan 17-Sep-14 11:23am    
You have used a pattern format for the call to TryParse but not for Parse; use the same format for both.

I may stand corrected, but i believe the Date format should actually be yyyy-mm-dd and the database structure set to ' date ' or ' verchar ' should also work. Otherwise you may have issues.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 11:23am    
You should not rely on any particular format. OP should 1) use time data types (DATE, for example), not varchar, 2) specify format in a query, if needed.
—SA
[no name] 17-Sep-14 11:32am    
It's been a while since I've done anything with dates in SQL, but upon looking this up, you would be correct Sergey.
atul sharma 5126 17-Sep-14 12:28pm    
I am using DATETIME format in sql.
I tried changing the format in function to yyyy-MM-dd but that did'nt help.
Should I change the type to DATE instead in sql?
Use your function to validate the date and change
daCreate.InsertCommand.Parameters.Add(New SqlParameter("@ROUTTD", DateTime.Parse(txtTargetDate.Text)))

To
daCreate.InsertCommand.Parameters.Add("@ROUTTD", SqlDbType.DateTime).Value = DateTime.Parse(txtTargetDate.Text)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900