0

I cannot figure this out for the life of me. The titled error continues to pop up but I cannot see where the problem is:

class Repository
{
    private OleDbConnection getConnection()
    {
        return new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\tb2\Users\tburmeister\Documents\Access Databases\Staff_Actions.accdb;Jet OLEDB:Database Password=ECDistrict;");
    }
    public List<ActionItem> getActionItemList()
    {
        return null;
    }

    public void insertActionItem(ActionItem actionItem)
    {
        OleDbConnection con = getConnection();

        OleDbCommand cmd = new OleDbCommand("Insert into [Actions] ([RECORD_ENTERED_DATE], [BOARD_DATE],[FIRST_NAME],[LAST_NAME],[ADDRESS],[PO_BOX],[CITY],[STATE],[ZIP],[EMP_PHONE],[LOCAL_YEARS],[OUTSIDE_YEARS],[TOTAL_YEARS],[EMP_HISTORY],[GROUP_TYPE],[TERM_TYPE],[TOTAL_FTE],[POSITION],[LOCATION],[POSITION_PCT],[SALARY],[DATE_OF_ACTION],[ACTION],[FUNDING],[REASON]) Values(@recordEnteredDate, @boardDate, @firstName, @lastName, @Addy, @poBox, @city, @state, @zip, @employeePhone, @localYears, @outsideYears,@totalYears, @employeeHistory, @groupType, @termType, @totalFTE, @position, @location, @positionPercentage, @salary, @dateOfAction, @action, @funding, @reason)", con);
        cmd.Parameters.Add("@recordEnteredDate", OleDbType.Date).Value = actionItem.RecordEnterdDate;
        cmd.Parameters.Add("@boardDate", OleDbType.Date).Value = actionItem.BoardDate;
        cmd.Parameters.Add("@firstName", OleDbType.VarChar).Value = actionItem.FirstName;
        cmd.Parameters.Add("@lastname", OleDbType.VarChar).Value = actionItem.LastName;
        cmd.Parameters.Add("@Addy", OleDbType.VarChar).Value = actionItem.Address;
        cmd.Parameters.Add("@poBox", OleDbType.VarChar).Value = actionItem.PoBox;
        cmd.Parameters.Add("@city", OleDbType.VarChar).Value = actionItem.City;
        cmd.Parameters.Add("@state", OleDbType.VarChar).Value = actionItem.State;
        cmd.Parameters.Add("@zip", OleDbType.Integer).Value = Convert.ToInt32(actionItem.Zip);
        cmd.Parameters.Add("@employeePhone", OleDbType.Integer).Value = Convert.ToInt32(actionItem.EmpPhone);
        cmd.Parameters.Add("@localYears", OleDbType.Integer).Value = Convert.ToInt32(actionItem.LocalYears);
        cmd.Parameters.Add("@outsideYears", OleDbType.Integer).Value = Convert.ToInt32(actionItem.OutsideYears);
        cmd.Parameters.Add("@totalYears", OleDbType.Integer).Value = Convert.ToInt32(actionItem.TotalYears);
        cmd.Parameters.Add("@employeeHistory", OleDbType.VarChar).Value = actionItem.EmployeeHistory;
        cmd.Parameters.Add("@groupType", OleDbType.VarChar).Value = actionItem.GroupType;
        cmd.Parameters.Add("@termType", OleDbType.VarChar).Value = actionItem.TermType;
        cmd.Parameters.Add("@totalFTE", OleDbType.VarChar).Value = actionItem.TotalFTE;
        cmd.Parameters.Add("@position", OleDbType.VarChar).Value = actionItem.Position;
        cmd.Parameters.Add("@location", OleDbType.VarChar).Value = actionItem.Location;
        cmd.Parameters.Add("@positionPercentage", OleDbType.VarChar).Value = actionItem.PositionPCT;
        cmd.Parameters.Add("@salary", OleDbType.VarChar).Value = actionItem.Salary;
        cmd.Parameters.Add("@dateOfAction", OleDbType.VarChar).Value = actionItem.DateOfAction;
        cmd.Parameters.Add("@action", OleDbType.VarChar).Value = actionItem.Action;
        cmd.Parameters.Add("@funding", OleDbType.VarChar).Value = actionItem.Funding;
        cmd.Parameters.Add("@reason", OleDbType.VarChar).Value = actionItem.Reason;
        try 
        {
            con.Open();

            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (OleDbException ex)
        {
            con.Close();
            Console.WriteLine(ex.Message);
        }
    }
}

Can someone shed some light on this please?

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
nerdzilla
  • 31
  • 3

4 Answers4

0

Maybe your FIRST_NAME column in your table is set to be NOT NULL. Check the value of actionItem.FirstName, make sure this is NOT NULL.

You should debug your code to see if actionItem.FirstName is not NULL.

And take note that parameters with a .Value of null are NOT PASSED AT ALL. So you need to do this:

cmd.Parameters.Add("@param", OleDbType.VarChar).Value = object ?? DBNull.Value;

Hope this helped!

jomsk1e
  • 3,585
  • 7
  • 34
  • 59
0

Use try catch block to know exactly where the problem lies put a debug point in start of insertactionitem method and press F10 to move step by step

use the insert query value in a text box, and then try to run the query in access

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Usman Waheed
  • 555
  • 5
  • 14
0

The similar issue has been handled in stackoverflow before. You can take a look at that one from HERE as well .

The problem can be that the name of parameter is mismatching in code and stored procedure/query.

Also the field that is giving the error is set as NOT NULL.

So you can also double check spellings to make sure this is not the case.

And adding try- catch block is also good idea to catch the exact error.

And as a last suggestion, try this and see what happens:

cmd.Parameters.Add(new OleDbParameter("@firstName", actionItem.FirstName));
Community
  • 1
  • 1
curiousBoy
  • 6,334
  • 5
  • 48
  • 56
0

I would suspect your problem lies with actionItem.BoardDate.

I'm not sure what type it is, but if it's null or invalid for some reason, then it can mess up the parameter that comes next.

Baldrick
  • 11,712
  • 2
  • 31
  • 35