I have a stored procedure in Sql server. This sp returns a dataset (with 49 columns). There is a column "Product_Amnt" in the dataset which has decimal values.
I am trying to retrieve this column data in asp.net application using c#.
DataTable dt = new DataTable();
try
{
SqlCommand com = new SqlCommand("sp_ProductDetails");
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.Add("@ID", SqlDbType.Int).Value = this._ID;
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(dt);
}
catch (Exception ex) { new Error(ex); }
return dt;
I want to use the "Product_Amnt" column to display the products based on conditions.
decimal pr_charges = Convert.ToDecimal(dr["Product_Amnt"]);
But it gives an error saying "object cannot be cast from dbnull to other types". So I modified the code like this,
decimal pr_charges = dr["Product_Amnt"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["Product_Amnt"]);
if (pr_charges == 0.00M)
{
counter++;
}
But it always gives me value 0 in pr_charges variable.
How to retrieve the decimal values from database and store it in a variable?