0

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

Radhe Sham
  • 141
  • 10

1 Answers1

0

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>
}
  • I have tried similar as you said like @(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fullName) In each column which i am fetching. Now this error is no more but my query dont work now. How can i show you my whole code so that you can guide me in better way? – Radhe Sham Jul 25 '15 at 06:22
  • I have sent you an email please check – Radhe Sham Jul 25 '15 at 06:54
  • form the code i can find the error in this line var updateStatus = "UPDATE emp_info SET status='"+1+"' WHERE emp_id=@0"; 1 outside the quotes will make the compiler search for a variable named 1. keep it inside the string. var updateStatus = "UPDATE emp_info SET status='1' WHERE emp_id=@0"; –  Jul 25 '15 at 07:12
  • It still don't work. If i put the query outside the if means when page loads it automatically updates without submitting the approve button. but i don't want to do so. i want to run the query when button is pressed. – Radhe Sham Jul 25 '15 at 07:36