Search code examples
asp.netwebmatrix-3

Binding error in web pages Asp.net using webmatrix


i am working on web pages under webmatrix, i have tried this code and facing this error Cannot perform runtime binding on a null reference I have a query which fetch the record from database and another to update that record.

var SelectEmpInfo = "SELECT * FROM emp_info WHERE emp_id =@0";
     var SelectedEmpInfo = db.QuerySingle(SelectEmpInfo,empID);



   if(IsPost)
   {
       if(Request.Form["approve"]!=null)
    {
        var updateStatus = "UPDATE emp_info SET status='"+1+"' WHERE emp_id=@0";
        db.Execute(updateStatus,empID);


         <h1>Successfully Updated</h1>   


    }
   }

and i fetch each column associated with this id in a table like

<thead>
        <tr class="info">

            <th>Full Name</th>
            <th>Fathers Name</th>
            <th>CNIC </th>
            <th>DOB</th>
            <th>Gender</th>
            <th>Self Status</th>
            <th>Religion</th>
            <th>Nationality</th>


        </tr>
    </thead>

    <tbody>




    <tr class="active">

            <td>@SelectedEmpInfo.fullName</td>
            <td>@SelectedEmpInfo.fatherName</td>
            <td>@SelectedEmpInfo.cnic</td>
            <td>@SelectedEmpInfo.dob</td>
            <td>@SelectedEmpInfo.gender</td>
            <td>@SelectedEmpInfo.selfStatus</td>
            <td>@SelectedEmpInfo.religion</td>
            <td>@SelectedEmpInfo.nationality</td>


        </tr>





    </tbody> 
</table>
    </div>
   </div>

I face this error

Server Error in '/' Application.

Cannot perform runtime binding on a null reference

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

   <tr class="active">
       <td>@SelectedEmpInfo.fullName</td>
       <td>@SelectedEmpInfo.fatherName</td>
       <td>@SelectedEmpInfo.cnic</td>

I don't get to know why i am facing this kind of error. Please someone help me out there. Thanks in advance


Solution

  • This will happen if the row with the emp_id you passed doesn't exists. Check for null row and you're good to go.

    <tr class="active">
    @if(SelectedEmpInfo!=null)
    {
           <td>@SelectedEmpInfo.fullName</td>
           <td>@SelectedEmpInfo.fatherName</td>
           <td>@SelectedEmpInfo.cnic</td>
    }
    else
    {
           <td></td>
           <td></td>
           <td></td>
    }