UPDATE: I did a bit of a test to see if the Date of Birth was actually the problem. I changed everything into Text data type in my Access database and tried inserting the data both provided with Phoenyx's code and with the one I had when I had the question and still nothing. To my understanding, because the database has an autonumber ID as the first column, the query must be trying to add values starting at the ID. Any idea on how to dodge that and make it start from Name?
Hello, lately I have been working on a program and I am getting an error through the use of breakpoints saying "Data type mismatch in criteria exception". For my coding I use C# and Access, I have been trying to figure out the problem but to no luck. I will guide you through my code and show you where the issue comes up.
This is the part of the code that connects to the Access database to use the insert or read the data in the database
public Object ConnString
{
get { return Properties.Settings.Default.GoGreeceConnectionString; }
}
public OleDbConnection CreateConn()
{
return new OleDbConnection(this.ConnString.ToString());
}
This is the part of the code that adds the data. I point out with capitals and bold where the error is shown. Just so it is known, Name, Surname, Phone, Mobile and Email are set as Text in the Access Database, Date of Birth is set as a Date/Time.
public void SQLInsertRecord(string Name, string Surname, string DateOfBirth, string Phone, string Mobile, string Email)
{
string sqlStr = "INSERT INTO [Customers] ([Name], [Surname], [Date Of Birth], [Phone], [Mobile], [Email]) VALUES ('@Name','@Surname', '@Date Of Birth', '@Phone', '@Mobile', '@Email')";
OleDbCommand sqlCmd = null;
try
{
var nameParam = new OleDbParameter("@Name", Name);
var surnameParam = new OleDbParameter("@Surname", Surname);
var dobParam = new OleDbParameter("@Date Of Birth", DateOfBirth);
var phoneParam = new OleDbParameter("@Phone", Phone);
var mobileParam = new OleDbParameter("@Mobile", Mobile);
var emailParam = new OleDbParameter("@Email", Email);
sqlCmd = new OleDbCommand(sqlStr, ConnManager.Instance.CreateConn());
sqlCmd.Parameters.Add(nameParam);
sqlCmd.Parameters.Add(surnameParam);
sqlCmd.Parameters.Add(dobParam);
sqlCmd.Parameters.Add(phoneParam);
sqlCmd.Parameters.Add(mobileParam);
sqlCmd.Parameters.Add(emailParam);
sqlCmd.Connection.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("Error: Could not insert data", ex);
}
finally
{
if (((sqlCmd != null)) && ((sqlCmd.Connection != null)))
{
if ((!(sqlCmd.Connection.State == System.Data.ConnectionState.Closed)))
{
sqlCmd.Connection.Close();
}
sqlCmd.Connection.Dispose();
}
}
This is the line of code that executes the tests and which through I find the issue.
private void executeTestbutton_Click_1(object sender, EventArgs e)
{
executeTests();
}
private void executeTests()
{
CustomerTableSelect();
CustomerTableInsert();
}
private void CustomerTableInsert()
{
try
{
CustomerCRUDmanager.Instance.SQLInsertRecord("Test Name1", "Test Surname1", "03/11/1993", "2310", "234567", "test@test.gr");
this.testTextbox.AppendText("Successfully added into the database");
}
catch (Exception ex)
{
this.testTextbox.AppendText("Error: Could not add row in the database");
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Thank you for any help you provide :)