locked
The given key was not present in the dictionary RRS feed

  • Question

  • User1126057398 posted

    I was trying to insert Data in Users Table but it is giving error 'The given key was not present in the dictionary'. I am using Dapper. Which key it is referring to?

    public static U StoredProcInsertWithID<T, U>(string procName, DynamicParameters parms,string outParamName)
    {
    using (IDbConnection connection = OpenConnection())
    {
    var x = connection.Execute(procName, (object)parms, commandType: CommandType.StoredProcedure);
    return parms.Get<U>("outParamName");
    }
    }

    public virtual int SaveUsers(Users user)
    {
    var result = 0;
    using (var transactionScope = new TransactionScope())
    {
    try
    {
    var parms = new DynamicParameters();
    parms.Add("@UserId", user.UserId, dbType: DbType.Int32, direction: ParameterDirection.InputOutput);
    parms.Add("@UserName", user.UserName);
    parms.Add("@Password", user.Password);
    parms.Add("@IsActive", user.IsActive);
    parms.Add("@ActiveDate", user.ActiveDate);
    parms.Add("@PasswordUpdatedOn", user.PasswordUpdatedOn);
    parms.Add("@IsPasswordRequest", user.IsPasswordRequest);
    parms.Add("@DeActivateDate", user.DeActivateDate);
    parms.Add("@PasswordExpirationDays", user.PasswordExpirationDays);
    parms.Add("@IsPasswordExpired", user.IsPasswordExpired);
    parms.Add("@IsAccountLocked", user.IsAccountLocked);
    parms.Add("@AccessFailedCount", user.AccessFailedCount);
    parms.Add("@IsLockedOutEnabled", user.IsLockedOutEnabled);
    // parms.Add("@LockoutEndDate", user.LockoutEndDate);
    parms.Add("@IsPhoneNumberConfirmed", user.IsPhoneNumberConfirmed);
    parms.Add("@SecurityStamp", user.SecurityStamp);
    parms.Add("@IsEmailConfirmed", user.IsEmailConfirmed);
    parms.Add("@IsTwoFactorEnabled", user.IsTwoFactorEnabled);
    parms.Add("@MobilePhoneNumberChangedOn", user.MobilePhoneNumberChangedOn);
    parms.Add("@IsPasswordReset", user.IsPasswordReset);
    parms.Add("@PasswordResetToken", user.PasswordResetToken);
    parms.Add("@CreatedBy", user.CreatedBy);
    result = StoredProcInsertWithID<Users, int>("spInsertUsers", parms, "@UserId");
    user.UserId = parms.Get<int>("@UserId");
    if (result > 0)
    {
    user.UserId = user.UserId;
    user.UserDetail.UserId = user.UserId;
    result = SaveUsersDetail(user);
    if (user.Roles != null && user.Roles.Count > 0)
    result = UpdateUserRole(user);
    if (result > 0)
    transactionScope.Complete();
    else
    transactionScope.Dispose();
    }
    else
    transactionScope.Dispose();
    }
    catch (Exception ex)
    {
    transactionScope.Dispose();
    throw ex;
    }
    }
    return result;
    }

    Thursday, March 1, 2018 2:39 PM

Answers

  • User753101303 posted

    Hi,

    What if you try parms.Get<U>(outParamName) rather than return parms.Get<U>("outParamName") ?

    Always check the call stack. It is always useful to know where an exception is thrown in your code and even to see in which internal library method it happens...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 4:27 PM

All replies

  • User753101303 posted

    Hi,

    What if you try parms.Get<U>(outParamName) rather than return parms.Get<U>("outParamName") ?

    Always check the call stack. It is always useful to know where an exception is thrown in your code and even to see in which internal library method it happens...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 4:27 PM
  • User1126057398 posted

    Thanks a lot PatriceSc. It worked :)

    Thursday, March 1, 2018 5:15 PM