Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to convert parameter value from a List`1 to a IEnumerable`1

Tags:

c#

sql

list

I am passing table valued parameter to StoredProcedure.

Please check my code below

CREATE TYPE TempTable AS TABLE 
(A nvarchar(50), B nvarchar(50), C nvarchar(500))


 SqlParameter[] param = new SqlParameter[3];
 param[0] = new SqlParameter("@A", A);
 param[1] = new SqlParameter("@B", B);
 param[2] = new SqlParameter("@C", lstC);
 param[2].SqlDbType = SqlDbType.Structured;
 param[2].TypeName = "dbo.TempTable ";

 DataSet ds = SqlHelper.ExecuteDataset("StoredProcedureName", param);

Here , lstC is List object of class.

But getting error "Failed to convert parameter value from a List1 to a IEnumerable1."

EDIT

 public static DataSet ExecuteDataset(string spName, params object[] parameterValues)
    {
        if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
        if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

        // If we receive parameter values, we need to figure out where they go
        if ((parameterValues != null) && (parameterValues.Length > 0))
        {
            // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
            SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

            // Assign the provided values to these parameters based on parameter order
            AssignParameterValues(commandParameters, parameterValues);

            // Call the overload that takes an array of SqlParameters
            return ExecuteDataset(CommandType.StoredProcedure, spName, commandParameters);
        }
        else
        {
            // Otherwise we can just call the SP without params
            return ExecuteDataset(CommandType.StoredProcedure, spName);
        }
  }



 public static DataSet ExecuteDataset(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
        if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");

        // Create & open a SqlConnection, and dispose of it after we are done
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            // Call the overload that takes a connection in place of the connection string
            return ExecuteDataset(connection, commandType, commandText, commandParameters);
        }
    }

   public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType,   String commandText, params SqlParameter[] commandParameters)
    {
        if (connection == null) throw new ArgumentNullException("connection");

        // Create a command and prepare it for execution
        SqlCommand cmd = new SqlCommand();
        bool mustCloseConnection = false;
        PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

        // Create the DataAdapter & DataSet
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            DataSet ds = new DataSet();
            cmd.CommandTimeout = 0;
            // Fill the DataSet using default values for DataTable names, etc
            da.Fill(ds);

            // Detach the SqlParameters from the command object, so they can be used again
            cmd.Parameters.Clear();

            if (mustCloseConnection)
                connection.Close();

            // Return the dataset
            return ds;
        }
    }

PREPARE COMMAND

private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection)
    {
        if (command == null) throw new ArgumentNullException("command");
        if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");

        // If the provided connection is not open, we will open it
        if (connection.State != ConnectionState.Open)
        {
            mustCloseConnection = true;
            connection.Open();
        }
        else
        {
            mustCloseConnection = false;
        }

        // Associate the connection with the command
        command.Connection = connection;

        // Set the command text (stored procedure name or SQL statement)
        command.CommandText = commandText;

        // If we were provided a transaction, assign it
        if (transaction != null)
        {
            if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            command.Transaction = transaction;
        }

        // Set the command type
        command.CommandType = commandType;

        // Attach the command parameters if they are provided
        if (commandParameters != null)
        {
            AttachParameters(command, commandParameters);
        }
        return;
    }
like image 298
Lajja Thaker Avatar asked Jan 02 '13 11:01

Lajja Thaker


1 Answers

Came here during a project update to .Net Core 3.1 and started getting this error on previously-working code. This might only help you if you are going to Core 3.1+

Turns out it was a problem with the type of SqlDataRecord I was using when creating the structured data to send to my sproc.

old: Microsoft.SqlServer.Server.SqlDataRecord
new: Microsoft.Data.SqlClient.Server.SqlDataRecord

Note I had also moved from System.Data.SqlClient to Microsoft.Data.SqlClient based on their advice for .Net Core 3.1 and beyond and to clear up another error prior to finding the one that started this post.

like image 132
imukai Avatar answered Sep 22 '22 10:09

imukai