I have a stored procedure which accepts table value parameter. I would like to pass an int array to it, but right now I am getting this error:
Failed to convert parameter value from a Object[] to a IEnumerable`1.
My code:
int[] orderIds; //int array which contains 0 or more values
List<SqlParameter> queryList = new List<SqlParameter>
{
new SqlParameter("@companyId", SqlDbType.Int) {Value = companyId},
new SqlParameter("@marketplaceId", SqlDbType.Int) {Value = marketplaceId},
new SqlParameter("@marketlocalId", SqlDbType.Int) {Value = marketLocal},
new SqlParameter("@storeId", SqlDbType.Int) {Value = storeId}
};
if (orderIds != null)
{
queryList.Add(new SqlParameter("@OrderIdsSpecified", SqlDbType.Bit) {Value = 1});
// converting int array to object array
queryList.Add(new SqlParameter("@OrderIds", SqlDbType.Structured) {Value = orderIds.Select(orderId => (object)orderId).ToArray() });
}
using (SqlDataReader reader = SqlHelper.ExecuteReader(connString, CommandType.StoredProcedure, "SPName", queryList.ToArray()))
{
//reader.read()
}
I tried just passing the int array as is, without conversion to object, but that did not work either. I received the same error message, but with int[] instead of object[].
The table value parameter is just a table with one column of ints, and is already defined.
What am I doing wrong here? Any help is appreciated. Thanks!