简体   繁体   English

参数@firstName没有默认值

[英]Parameter @firstName has no default value

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? 有人可以对此有所启发吗?

Maybe your FIRST_NAME column in your table is set to be NOT NULL . 也许表中的FIRST_NAME列设置为NOT NULL Check the value of actionItem.FirstName , make sure this is NOT NULL . 检查actionItem.FirstName的值,确保它NOT NULL

You should debug your code to see if actionItem.FirstName is not NULL. 您应该调试代码以查看actionItem.FirstName是否不为NULL。

And take note that parameters with a .Value of null are NOT PASSED AT ALL. 并请注意根本没有通过.value为null的参数。 So you need to do this: 因此,您需要这样做:

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

Hope this helped! 希望这对您有所帮助!

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 使用try catch块确切知道问题出在哪里,将调试点放在insertactionitem方法的开始,然后按F10逐步移动

use the insert query value in a text box, and then try to run the query in access 在文本框中使用插入查询值,然后尝试在Access中运行查询

The similar issue has been handled in stackoverflow before. 之前在stackoverflow中已经解决了类似的问题。 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. 同样,将给出错误的字段设置为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. 并且添加try-catch块也是捕获确切错误的好主意。

And as a last suggestion, try this and see what happens: 最后建议,尝试一下,看看会发生什么:

cmd.Parameters.Add(new OleDbParameter("@firstName", actionItem.FirstName));

I would suspect your problem lies with actionItem.BoardDate . 我怀疑您的问题出在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. 我不确定它是什么类型,但是如果由于某种原因它为null或无效,那么它会弄乱接下来出现的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM