locked
Help me rectify a Null error? RRS feed

  • Question

  • User743508062 posted

    Hi, I have spent many days for a way to rectify a Null binding error I am getting. Hence why I am posting this Q here. I am getting my ideas and code from Mike's book tutorial and an evonet tutorial.

    The problem I have is that if I put the code inside an if...else statment, the variables are not visible to modal form I am trying to generate.

    This is the code at the top of the page (DiscountCustomers.cshtml)

    /* ------------  variables to edit discount data   ------------*/
    
       var query4 = "Select * FROM dbo.tblPromos Where CustomerCode=@0";
       var data4 = db.QuerySingle(query4, CustomerCode);
           
       var sid = data4.SalesID;       
       var emdiscount = data4.Discount;
       var emvat = data4.VAT;
       var emvatrate = data4.VATRate;
       var emiscription = data4.iscription;
       var emsrpbasedon = data4.SRPBasedOn;
    
       if(IsPost){   
             
         emdiscount = Request["discount"];
         emvat = Request["VAT"];
         emvatrate = Request["vatrate"];
         emsrpbasedon = Request["srpbasedon"];
         emiscription = Request["iscription"];
        
       var emsql = "UPDATE dbo.tblPromos SET Discount=@0, VAT=@1, VATRate=@2, SRPBasedOn=@3, iScription=@4 WHERE SalesID=@5";      
         
       db.Execute(emsql, emdiscount, emvat, emvatrate, emiscription, emsrpbasedon, sid);
    
              }

    This is the code I am using for the modal form also in DiscountCustomers.cshtml:

    <div id="dialogform3" title="Edit Discounts">
    <form id="editdiscountform" action= "@Href("~/DiscountCustomers")"> 
    
                 <div class="row">
                    <span class="label"><label for="sid">Sales ID:</label></span>
                    <input type="text" name="sid" id="sid" value="@sid" size="5" />
                </div>
                           
                <div class="row">
                    <span class="label"><label for="ediscount">Discount(%):</label></span>
                    <input type="text" name="ediscount" id="ediscount" value="@emdiscount" size="5" />
                </div>
    
                 <div class="row">
                    <span class="label"><label for="vat">VAT (Y/N):</label></span>
                    <input type="checkbox" name="vat" id="vat" value="true" />
                </div>
       
                <div class="row">
                    <span class="label"><label for="vatrate">VAT Rate(%):</label></span>
                    <input type="text" name="vatrate" id="evatrate" value="@emvatrate" size="5" />
                </div>
    
                <div class="row">
                    <span class="label"><label for="iscription">iScription Fee:</label></span>
                    <input type="text" name="iscription" id="eiscription" value="@emiscription"size="5" />
                </div>
    
                <div class="row">
                    <span class="label"><label for="srpbasedon">SRP Based On:</label></span>
                    <select name="srpbasedon" id="esrpbasedon"> value="@emsrpbasedon"
                    <option value="">--- Select SRP Based On ---</option>
                                  
                        @{
                        foreach(var srpbasedon in ListSRP){
                            <option value="@srpbasedon.SRPBasedOn">@srpbasedon.SRPBasedOn</option>
                            }
                        }
                        </select>
                </div>
    
          </form>
       </div>

    I really need someone to help/guide me.

    Thank you.

     

    Wednesday, May 15, 2013 5:39 AM

All replies

  • User-821857111 posted

    What null errror? Which line of code causes it? If it is any of the variable assignments being made from data4, that suggests that no data was returned from the database. You should check to see if CustomerCode has the vale that you expect it to have.

    Wednesday, May 15, 2013 7:57 AM
  • User-1482720155 posted
    Request["discount"] or Request["emdiscount"]  Check if that is the null value.
    Monday, July 8, 2013 6:13 AM
  • User1526116210 posted

    Maybe you should check for null values in your database table

    Tuesday, July 9, 2013 2:31 AM
  • User895691971 posted

    Its because you mainly need to create public variables out of your if else block like 

    var public = "";
    if(publicImage == true) {//just assume it, don't go for reason
    public == true;
    }

    This way, if your publicImge is not true still public will have an empty value! This way the database will have an empty column but not null. Nulls are created when you don't work on a col! They can be handled by

    if(publicImage == null) {
    public = false;
    }

    This way you won't get the error because the code will check whether the value is null or not! This way it only write the code on page if its empty or filled not null! Don't miss Null and Empty. (Someone please correct me about empty and nulls, Mike you're here so please)

    Null can be seen on the col value written in italics, but empty is just an empty field!

    So next time the null error shows up please bind that code block in an if statement with this code

    if(variable != null) {
    // your work..
    }

    Tuesday, July 9, 2013 1:16 PM