0
 public string bookno(string id, string un, DateTime date, string time, string reason)
    {
        using (OleDbConnection source = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
       Environment.CurrentDirectory + "\\IOOPAssignment.accdb"))
        {


                OleDbCommand insert = new OleDbCommand();
                insert.CommandText = "INSERT INTO [Pending_List] Values('" + id + "','" + un + "','#" + date + "#','" + time + "','" + reason + "','Nothing',0,'Pending')";
                source.Open();
                insert.Connection = source;
                insert.CommandType = System.Data.CommandType.Text;
                insert.ExecuteNonQuery();
                return "Success";

        }
    }

the error is:

System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.'

How can I solve this error?

the changed code:

 public string bookno(string id, string un, DateTime date, string time, string reason)
    {
        using (OleDbConnection source = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
       Environment.CurrentDirectory + "\\IOOPAssignment.accdb"))
        {

            source.Open();  

            using (OleDbCommand insert = source .CreateCommand())
            {
                // create command with placeholders
                insert.CommandText =
                   "INSERT INTO [Pending_List] " +
                   "([Class_ID], [User_ID],  [Date], [Time], [Reason],[Description],[Add_Req],[Room_Status]) " +
                   "VALUES(@id,@un,@date,@time,@reason,@describe,@add,@status)";

                // add named parameters
                insert.Parameters.AddRange(new OleDbParameter[]
                {
                     new OleDbParameter("@id", id),
                     new OleDbParameter("@un",un),
                     new OleDbParameter ("@date",date),
                      new OleDbParameter ("@time",time),
                      new OleDbParameter ("@reason",reason),
                     new OleDbParameter ("@describe", "Nothing"),
                      new OleDbParameter ("@add",0),
                      new OleDbParameter ("@status","Pending")
                });

                // execute
                insert.ExecuteNonQuery();
                return "Success";
            }

and the error is System.InvalidOperationException: 'Parameter[6]: the OleDbType property is uninitialized: OleDbType.Empty.'

braX
  • 11,506
  • 5
  • 20
  • 33
  • 2
    Ideally, you should not create the query in this way. You should use command parameter i.e. OleDbCommand. Refer https://stackoverflow.com/questions/5893837/using-parameters-inserting-data-into-access-database – Sumit Deshpande Jun 09 '18 at 09:43
  • You probably got the column names in the wrong order, or maybe skipped a column. If your SQL contained an explicit column list (and it really always ought to honestly) I'd be able to guess a little better. – John Wu Jun 09 '18 at 09:51
  • @SumitDeshpande after i tried you ways,i found another issue which is 'Parameter[6]: the OleDbType property is uninitialized: OleDbType.Empty.' – Jackery Pang Jia Shen Jun 09 '18 at 10:22
  • Which line of code gives you the error? – Chetan Jun 09 '18 at 10:57
  • remark out the // add named parameters block of code and try replacing it with `insert.Parameters.Add("@id", OldDbType.Integer).Value = id;` repeat this for each param with it's proper type explicitly defined and see if that gets you any further. – Charles May Jun 09 '18 at 12:45

0 Answers0