Search code examples
razorsql-server-cewebmatrixdbnull

If, else statement to override a null in webmatrix


I thought this would be the proper code to replace a null but it gives me an error "The name 'subt2' does not exist in the current context"

var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=@0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);

if(sub2 == null){
    var subt2 = 0.00;
    }
else{
    var subt2 = sub2;
    }

and in the page I have @subt2 I decided to try to explain what I am doing to hopefully get a better response. I have a page that you can add products to. It makes an entry into the table with the TransID as a reference to this page. This page pulls the SUM from Price where rows have this TransID. The problem lies when there is not a product entered in the table with this TransID. I need it to output a 0.00 when there is not data to be displayed.


Solution

  • took me forever but I found a few articles and put them together. This is the code:

    var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=@0";
    var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
    
    if(String.IsNullOrEmpty(Convert.ToString(sub2)))
    {
        sub2 = 0.00;
    }