0

I have tried using a DateTimePicker with format dd/MM/yyyy to insert a date into a Date/Time column in my MS Access database using the insert SQL statement.

Here is what I have done:

connection.Open();

OleDbCommand command = new OleDbCommand();
command.Connection = connection; // Connecting the command to the connection.
command.CommandText = "insert into [TestEntry] (TestTableID, StudentID, DateOfTest, [DeadLine]) values(" + int.Parse(txtTestName.Text) + ", " + int.Parse(studentID) + ", #" + this.dateTimeStartD.Text + "# , #" + this.dateTimeEndD.Text + "#)";

command.ExecuteNonQuery();
connection.Close();

I keep getting the error

Input string was not in correct format

The Date/Time columns in my Access table are formatted as 'Short date' eg. 12/11/2015.

Any help will be appreciated.

3
  • 2
    That is not how you pass values to Access; use parameters. Commented Apr 16, 2016 at 18:37
  • I have been using this way to insert values into my Access tables through out my program and have had no issues. I think I am getting the error because I am not passing the date data from the dateTimePicker correctly. Commented Apr 16, 2016 at 18:43
  • it would be better for you to follow this instructions: stackoverflow.com/questions/1402173/… Commented Apr 16, 2016 at 18:49

1 Answer 1

1

I do not think its coming from database level. This exception is coming when you try to case value which in different format. Please check you first argument for the insert statement.

int.Parse(txtTestName.Text)

You are trying to insert integer value to "TestTableID" field by converting text box value to integer. Please check your text box input is number or characters. It should be only numbers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.