Search code examples
c#sqloledboledbconnectionoledbcommand

No value given for one or more required parameters When all parameters are given


The Error i get is An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters.

but this is when all parameters a present code bellow:

private OleDbDataReader dbReader;// Data Reader object
    string sConnection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ICTSchool.accdb";
    string sql;
    OleDbConnection dbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ICTSchool.accdb");
    OleDbCommand dbCommand;

public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    } 

private void bAdd_Click(object sender, EventArgs e)
    {
        {
            dbConn = new OleDbConnection(sConnection);

            dbConn.ConnectionString = sConnection;


            dbConn.Open();
            string code = (cBQualification.SelectedItem as ComboboxItem).Value.ToString();
            string sqlinsert = "INSERT INTO Student VALUES (" + tBStudentNum.Text + "," + tBStudentName.Text+","+ tBCellNo.Text+","+ code + ")";
            Console.WriteLine("Test 'sqlinsert' "+ sqlinsert);

            dbCommand = new OleDbCommand(sqlinsert, dbConn);

            dbCommand.ExecuteNonQuery();
        }
    }

Solution

  • Here is part of the article about how to insert values in MS Access. To add one record to a table, you must use the field list to define which fields to put the data in, and then you must supply the data itself in a value list. To define the value list, use the VALUES clause.

    For example, the following statement will insert the values "1", "Kelly", and "Jill" into the CustomerID, Last Name, and First Name fields, respectively.

    INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name]) VALUES (1, 'Kelly', 'Jill')

    You can omit the field list, but only if you supply all the values that record can contain.

    INSERT INTO tblCustomers VALUES (1, Kelly, 'Jill', '555-1040', 'someone@microsoft.com')

    Source MSDN How to: Insert, Update, and Delete Records From a Table Using Access SQL