Base solution for your next web application
Open Closed

The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects. #10923


User avatar
0
sumitshah created
  • What is your product version-11.0.0
  • What is your product type (Angular or MVC)-Angular(13)
  • What is product framework type (.net framework or .net core)-.net core(6.0)

Hi after successfully version migration i am trying to call some apis to fetch records for the components. But i am getting this error

I debugged the code and found that while adding parameters to sqlquery it is giving exception. command.Parameters.Add(parameter)from this line in Databasehelper.cs file in create command method it is giving exception.

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I think this is not related to AspNet Zero but you can solve it by following https://github.com/dotnet/efcore/issues/16812#issuecomment-516013245

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    sumitshah created

    Hi @ismcagdas Thanks for giving reference. but no solution is working for me.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @sumitshah

    Could you share how do you create the query ? Is it handled in DatabaseHelper ?

    Thanks,

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    sumitshah created

    Hi @ismcagdas We are generating query as commandText having "@p0" , "@p1" placeholder, commandType as Text, and passing SqlParameter as params Array. Yes, it is handled in DatabaseHelper.cs file. Please refer below code.

    public DbCommand CreateCommand(string commandText, CommandType commandType, params SqlParameter[] parameters) { var command = GetContext().Database.GetDbConnection().CreateCommand();

            command.CommandText = commandText;
            command.CommandType = commandType;
            command.Transaction = GetActiveTransaction();
            foreach (var parameter in parameters)
            {
                command.Parameters.Add(parameter); // from this line it is givinng us exception "The SqlParameterCollection                                                                                     //only accepts non-null SqlParameter type objects, not SqlParameter                                                                                     //objects"
            }
    
            return command;
        }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @sumitshah

    Have you tried adding a default value if the parameter is null ? By the way, this problem doesn't seem related to AspNet Zero.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)