2
votes
  string strConnect = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=record.accdb";


        using (OleDbConnection con = new OleDbConnection(strConnect))
        {
            con.Open();
            using (OleDbCommand cmd = new OleDbCommand("insert into Industry(cName,Amount,amt_type,cheqno,bankname,branchname,tDate) values(@cname,@amount,@amt_type,@chqnum,@bname,@bchname,@tdate)", con))
            {
                cmd.Parameters.AddWithValue("@cname", name_txt.Text);
                cmd.Parameters.AddWithValue("@amount", Convert.ToDouble(amount_txt.Text));
                cmd.Parameters.AddWithValue("@amt_type", amt);
                cmd.Parameters.AddWithValue("@chqnum", cheque_txt.Text);
                cmd.Parameters.AddWithValue("@bname", bank_txt.Text);
                cmd.Parameters.AddWithValue("@bchname", branch_txt.Text);
                cmd.Parameters.AddWithValue("@tdate",     dateTimePicker1.Value.ToShortDateString());

                cmd.ExecuteNonQuery();


            }
            con.Close();

I have a error saying An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Data type mismatch in criteria expression. What could be the possible reason for that and error is at line cmd.ExecuteNonQuery();.

There are two radio buttons which takes input text on changed event i noticed that on checking cheque and filling all the details of the form it works but when i check on cash field and leave some fields blank it throws an exception any help would be appreciated.

2
You need to make sure you bind the right type of value. I suspect that dateTimePicker1.Value.ToShortDateString() might be returning something that isn't compatible, try with just dateTimePicker1.Value (of course it could be one of the other values/fields). - user2864740

2 Answers

2
votes

Rather explicitly specify the parameters like below to eliminate that as an error:

SqlCommand command = new SqlCommand(commandText, connection); command.Parameters.Add("@ID", SqlDbType.Int); command.Parameters["@ID"].Value = customer

1
votes

you are using AddWithValue yet you want to convert the parameter to a double:

cmd.Parameters.AddWithValue("@amount", Convert.ToDouble(amount_txt.Text));

you should leave it up to ADO.NET to match the right datatype:

cmd.Parameters.AddWithValue("@amount", amount_txt.Text);