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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert today's date into Access database

Status
Not open for further replies.

SkydivingSteve

Technical User
Feb 20, 2004
27
GB
Using C# / ASO.Net to create a record in Access. I'm having problems figuring out the syntax to add today's date into a date field in Access. The error message I'm getting is "Data type mismatch in criteria expression. " and the line flagged is "objCmd.ExecuteNonQuery();"

Any thoughts appreciated! Code follows;

[tt]
string strCmd;
string SQLtext;
SQLtext="INSERT INTO BlahTable (ordered_by_name, heading, entered_date) VALUES (@name, @heading, @entered_date)";
objCmd = new OleDbCommand(SQLtext, objConn);
objCmd.Parameters.Add("@name", Name.Text);
objCmd.Parameters.Add("@heading", Heading.Text);
objCmd.Parameters.Add("@entered_date", DateTime.Now);
objConn.Open();
objCmd.ExecuteNonQuery();
[/tt]
 
What is @entered_date defined as and where do you assign a value to it?
 
jbenson,

Same line

[tt]objCmd.Parameters.Add("@entered_date", DateTime.Now);[/tt]

DateTime.Now

from namespace System.Globalization . The property is working o.k. as I've tested it by casting to a string and response.writing it to the page.

Steve
 
Well somewhere there is a datatype mismatch. Check each column and parameter datatype and make sure they are correct.
 
Thanks jbenson. I'm sorry if I wasn't clear in my initial post.I know where the problem is; there's a datatype mismatch is in the line;

[tt]objCmd.Parameters.Add("@entered_date", DateTime.Now);[/tt]

I'm looking for the correct syntax to cast the .Net DateTime.Now datatype to an Access date datatype.

Any further help most welcome, this is driving me crazy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top