I am trying to execute a stored procedure from DB. However, I am getting an exception:
InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.Nullable`1[System.Int32]'.
Exception is thrown by "results.Add" line.
var result = new List<GetActiveUserPackagesForOpenBillingPeriodResult> ();
using (var conn = new NpgsqlConnection ("Host=localhost;Port=xxx;Database=xxx;Username=postgres;Password=xxx;TrustServerCertificate=true;ApplicationName=xxx;")) {
using (var cmd = new NpgsqlCommand ("\"GetActiveUserPackagesForOpenBillingPeriod\"", conn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue ("somedate", DateTime.Today);
conn.Open ();
var reader = cmd.ExecuteReader ();
string x = DBNull.Value.Equals (reader) ? " " : reader.ToString ();
if (x != null)
{
while (reader.Read ()) {
result.Add (
new GetActiveUserPackagesForOpenBillingPeriodResult {
Amount = (decimal) reader["Amount"],
AcceptanceActID = (int?) reader["AcceptanceActID"],
PackageID = (int) reader["PackageID"],
UserID = (int) reader["UserID"],
AccountID = (int) reader["AccountID"],
HasChangedPackage = (bool) reader["HasChangedPackage"],
}
);
}
}
}
}