Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i am inserting date using vb in my Sqlserver database showing wrong date

using Sqlserver 2008

Help me out of this
Posted
Comments
/\jmot 26-Nov-14 7:16am    
what's the problem you faced??
/\jmot 26-Nov-14 7:17am    
be more specific about your problem.

Probably, you are committing the cardinal sin in SQL access, and concatenating striungs to form an SQL command:
VB
Dim cmdString As String = "INSERT INTO MyTable (dateColumnName) VALUES ('" & myDateTimeValue.ToString + "')"

Don't - that is always prone to errors, and if you are habitually using it, you leave your database wide open to an SQL injection attack which can easily damage or destroy your database.

Use a parameterised query instead, and the problem will disappear:
VB
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (dateColumnName) VALUES (@DT)", con)
        com.Parameters.AddWithValue("@DT", myDateTimeValue)
        com.ExecuteNonQuery()
    End Using
End Using
 
Share this answer
 
You don't provide a lot of info but the date is probably too small. If you don't set the date in vb.net it will be 01-01-0001 but that is not accepted by sql server. Try using the data type DATETIME2 instead of DATETIME in sql server.

Good luck!
 
Share this answer
 
try this i am using it and works fine..

declare format in sql query..


INSERT INTO myTable (dateColumnName) VALUES (date1.ToString("yyyy-MM-dd)")"
 
Share this answer
 
By default, the date format for SQL server is in U.S. date format MM/DD/YY, unless a localized version of SQL Server has been installed. So you have change default date format if you are not using default date format.

use the follwing command to change the default date format.


set dateformat dmy

INSERT INTO myTable (dateColumnName) VALUES (date1.ToString("dd-MM-yyyy)")"
 
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