Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Into - Date Error

Status
Not open for further replies.

tchaplin

Programmer
Jul 18, 2003
58
0
0
NZ
I’m trying to insert a date into a MSSQL database its fails with an error - converting datetime to character string


The code it s something wrong with strsql

int insertResult;
string ConnString;
string strsql;
SqlConnection cnSQL;
SqlCommand cmSQL;
SqlDataReader drSQL;

ConnString = "Server=localhost;" + "DataBase=BearScan;" + "Integrated Security=SSPI;Connect Timeout=5";
cnSQL = new SqlConnection(ConnString);
cnSQL = new SqlConnection(ConnString);
cnSQL.Open();

strsql = "INSERT INTO T_ENT_Entries (ENT_Date) VALUES (CONVERT(DATETIME, '" + DateTime.Today + "', 102))";

cmSQL = new SqlCommand(strsql, cnSQL);
drSQL = cmSQL.ExecuteReader();

There is nothing wrong with the connection or DB as this works

//strsql = "INSERT INTO T_ENT_Entries (ENT_Date, ENT_ScanCode) VALUES (CONVERT(DATETIME, '2006-12-12 00:00:00', 102), 'eew')";

The formatting DateTime.Today is “dd/mm/yyyy” although I don’t think that should matter, should it!

Thanks for any suggestions
 
What is the data type of the Ent_Date field in the database? SmallDateTime? Do you have a default value specified?

I ran your code and didn't have any problems.

Also, if you are executing an insert statement, you want to use the command.ExecuteNonQuery() method.
 
Maybe you should take a look at the CultureInfo of you application. Try to change the cultureInfo and see if it works.
Cheers

Country of eagles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top