I have a list of float? objects.
List<float?> myList = new List<float?>{1,null,3};
I'm trying to insert this objects in a table.
using (SqlCommand oCommand = myConnection.CreateCommand())
{
oCommand.CommandType = CommandType.Text;
oCommand.CommandText = "INSERT INTO myTable (col1) values(@col1)";
foreach (var oData in myList)
{
oCommand.Parameters.Clear();
oCommand.Parameters.Add((oData == null) ? new SqlParameter("@col1", DBNull.Value) : new SqlParameter("@col1", SqlDbType.Float));
oCommand.Parameters[oCommand.Parameters.Count - 1].Value = oData;
if (oCommand.ExecuteNonQuery() != 1)
{
throw new InvalidProgramException();
}
}
myConnection.Close();
}
When I'm running this code I have an exception which said that the parameter @col1 is expected but it was not supplied.