(OleDb) ExecuteNonQuery error - Type name is invalid

2.2k Views Asked by At

I pretty new in writing asp.net codes and I can't solve a problem. Every time I try to register I get the following error:

Type name is invalid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Type name is invalid.

Source Error:

Line 60:             }
Line 61: 
Line 62:             cmnd.ExecuteNonQuery();
Line 63:             this.cnct.Close();
Line 64:         }

The source code is:

    public class Connection
    {
        private OleDbConnection cnct;

        public Connection(string dbPath)
        {
            this.cnct = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + dbPath);
        }

        /// <param name="types">'c' - VarChar, 'C' - Char, 'n' - VarNumeric, 'b' - Boolean</param>
        public void Insert(string tableName, string[] inputs, char[] types)
        {
            string sql = "INSERT INTO " + tableName + " VALUES (";
            for (int i = 0; i < inputs.Length - 1; i++)
                sql += "@p" + i + ",";
            sql += "@p" + (inputs.Length - 1) + ")";

            this.cnct.Open();
            OleDbCommand cmnd = new OleDbCommand(sql, this.cnct);

            for (int i = 0; i < inputs.Length; i++)
            {
                if (types[i] == 'c')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.VarChar);
                    cmnd.Parameters["@p" + i].Value = inputs[i];
                }
                else if (types[i] == 'C')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.Char);
                    cmnd.Parameters["@p" + i].Value = inputs[i];
                }
                else if (types[i] == 'n')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.VarNumeric);
                    cmnd.Parameters["@p" + i].Value = double.Parse(inputs[i]);
                }
                else if (types[i] == 'b')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.Boolean);
                    cmnd.Parameters["@p" + i].Value = bool.Parse(inputs[i]);
                }
            }

            cmnd.ExecuteNonQuery();
            this.cnct.Close();
        }
    }

and the Insert method is called using

Connection cnct = new Connection(Server.MapPath("App_Data/WMUdb.mdb"));
cnct.Insert("members", values, new char[7] { 'c', 'c', 'c', 'c', 'C', 'n', 'b' });

(where

values = new string[7] {"userName","my name","pswrd123","[email protected]","2000-01-01"};

).

What should I do to solve it?

Thanks a lot.

2

There are 2 best solutions below

0
On

It looks like the OLEDB provider for the Jet engine doesn't support OleDbType.VarNumeric.

Try using OleDbType.Numeric instead.

0
On

Additionally, I'm smelling a date column... "2000-01-01"}; Ensure you table column data type is chara as you expect, or is it really a date/time column. If so, have a parameter type of DateTime and ensure proper setting the parameter value as a date/time value, even if it IS set to 12:00:00am of the 2000-01-01 date in question.