简体   繁体   中英

C# - ASP.NET - Inserting Date from a calendar into a DataBase

I am trying to store the date from a calendar which the user has selected into a database. It seems to not being inserting it into the Access Database. The DB field data type is Date/Time and the variables are below. I know my Query works because the other information stores correctly.

DateTime dtUserDate;
dtUserDate = calenderUserDate.SelectedDate;

string myQuery = "INSERT INTO MMK( Name, Email, Address, Town, County, PostCode, Country, Telephone, Date Joined)   VALUES ( '" + strName + "' , '" + strEmail + "' , '" + strAddress + "' , '" + strTown + "' , '" + strCounty + "' , '" + strPostCode + "' , '" + strCountry + "' , '" + strTeleNumber + "' , '" + dtUserDate+ "')";

Thanks

try with

INSERT INTO MMK( Name, Email, Address, Town, County, PostCode, Country, Telephone, [Date Joined]) ....

Note that your table column Date Joined having space, use [Date Joined]

Use Parameters as below

string myQuery = "INSERT INTO MMK( Name, Email, Address, Town, County, PostCode, Country, Telephone, [Date Joined])   VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)";
using (var cmd = new OleDbCommand(myQuery, con))
{
    cmd.Parameters.AddWithValue("Name", strName);
    ....
    cmd.Parameters.AddWithValue("DateJoined", dtUserDate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM