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"],
}
);
}
}
}
}
From DBNull Documentation:
DBNullrepresents a nonexistent value, wherenulldenotes absence of reference to the object.
To fix your issue,
Null check before changing value's type and then assign it to the variable
result.Add (
new GetActiveUserPackagesForOpenBillingPeriodResult {
Amount = (decimal) reader["Amount"],
AcceptanceActID = Convert.IsDBNull(reader["AcceptanceActID"]) ? null : (int?) reader["AcceptanceActID"],
PackageID = (int) reader["PackageID"],
UserID = (int) reader["UserID"],
AccountID = (int) reader["AccountID"],
HasChangedPackage = (bool) reader["HasChangedPackage"],
});
Use Convert.IsDBNull() to check value is DBNull and then use value
result.Add (
new GetActiveUserPackagesForOpenBillingPeriodResult {
Amount = (decimal) reader["Amount"],
AcceptanceActID = !Convert.IsDBNull(reader["AcceptanceActID"]) ? (int?) reader["AcceptanceActID"] : null,
PackageID = (int) reader["PackageID"],
UserID = (int) reader["UserID"],
AccountID = (int) reader["AccountID"],
HasChangedPackage = (bool) reader["HasChangedPackage"],
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With