0
votes

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();
        }
    }
3
And what is the student table look like? And if the table only has these four not null fields, are you sure one or more of the TextBox in your app is not empty? And last, the text fields should be inside single quotes, which you are not doing.Allen King

3 Answers

0
votes

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', '[email protected]')

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

0
votes

The problem I see may be because of malformed SQL Statement. The string values( NVARCHAR, VARCHAR) should be enclosed within single quotes which I believe is not how you're doing now with following statement

string sqlinsert = "INSERT INTO Student VALUES (" + tBStudentNum.Text + "," + tBStudentName.Text+","+ tBCellNo.Text+","+ code + ")";

Try changing the SQL Statement to

string sqlinsert = $"INSERT INTO Student VALUES ({tBStudentNum.Text}, '{tBStudentName.Text}', {tBCellNo.Text}, '{code}')";

I've made an assumption in above case that tBStudentNum.Text and tBCellNo.Text are numeric values. If not, you can make appropriate changes to put the values inside single quote.

If you're using lower version of .net/C#, replace the $ expression with string.format function.

0
votes

A number of observations:

  1. You haven't specified the parameters in the SQL so we can only assume that there are four fields in the Student table.
  2. You are not using named parameters - this is generally poor practice.
  3. You are using concatenated values and SQL - this will leave you vulnerable to a SQL Injection attack
  4. Any one of the text boxes might include a comma or other SQL formatting characters leading to SQL errors.