Search code examples
c#asp.netdatabasems-accesscalendar

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


Solution

  • 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);