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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    That is not how you pass values to Access; [use parameters](http://stackoverflow.com/questions/5893837/). – Dour High Arch Apr 16 '16 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. –  Apr 16 '16 at 18:43
  • it would be better for you to follow this instructions: http://stackoverflow.com/questions/1402173/sql-statement-with-datetimepicker – Ivan Yuriev Apr 16 '16 at 18:49

1 Answers1

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.

Telan Niranga
  • 437
  • 3
  • 10