locked
When page get load it run the query without submit the form RRS feed

  • Question

  • User2072242043 posted

    Hi everyone,

    I have a form which has a submit button i get load data from database and want to update that record when i submit the form in that page but it is automatically submitted when page gets load.

    Here is my input type submit through which i am trying to submit the form

    <input type="submit"/>

    Here is my code

     var db=Database.Open("Task6");
    
         
     var empID= "";
        
    
        if(!IsPost){
            if(!Request.QueryString["emp_id"].IsEmpty() && Request.QueryString["emp_id"].IsInt()){
                
                empID = Request.QueryString["emp_id"];
                
              
              
           
        }
        }
    
    
    
    
    
                var SelectPersonalInfo = "SELECT * FROM emp_info WHERE emp_id = '"+empID+"'";
                var SelectedEmpInfo=db.Query(SelectPersonalInfo);
                
             if(IsPost)
             {
                  var updateEmpInfo="UPDATE emp_info SET status='"+1+"' WHERE emp_id='"+empID+"'";
                    var updatedEmpInfo=db.Query(updateEmpInfo);
             }

    this block checks and assign value to empID  received from another form

    if(!IsPost){
            if(!Request.QueryString["emp_id"].IsEmpty() && Request.QueryString["emp_id"].IsInt()){
                
                empID = Request.QueryString["emp_id"];
                
              
              
           
        }
        }

    This query select the record from database which i want to update

    var SelectPersonalInfo = "SELECT * FROM emp_info WHERE emp_id = '"+empID+"'";
                var SelectedEmpInfo=db.Query(SelectPersonalInfo);

    My main question comes here, when i submit the form it should run the update query through if(IsPost) method means after form get submitted. but it don't do so. this query runs when page get load without submitting the form.

    if(IsPost)
             {
                  var updateEmpInfo="UPDATE emp_info SET status='"+1+"' WHERE emp_id='"+empID+"'";
                    var updatedEmpInfo=db.Query(updateEmpInfo);
             }

    Please help me what different i should try.

    Help me ASAP.

    Waiting 

    Radhesham

    Thursday, July 23, 2015 2:07 PM

Answers

  • User541108374 posted

    Hi,

    you can make use of the built in objects for debugging in Razor: http://www.asp.net/web-pages/overview/testing-and-debugging/introduction-to-debugging to see what's going on.

    It's been a while since I worked with WebMatrix but I don't recall you can place breakpoints.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 1:45 AM
  • User1724605321 posted

    Hi Radhesham,

    Radhesham Khatri

    How can i put breakpoints in my code and double check the code?

    Debugger in webmatrix:http://stackoverflow.com/questions/5112859/how-to-debug-and-or-trace-execution-flow-in-webmatrix

    If you are using visual studio to develop your application ,you could debug in visual studio ,please refer to links below for detail steps:

    https://msdn.microsoft.com/en-us/library/sc65sadd.aspx .

    https://msdn.microsoft.com/en-us/library/vstudio/sc65sadd(v=vs.110).aspx .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 6:19 AM
  • User325035487 posted

    What is the data type of 'status' column in your database?

    Any way the null reference is coming because you put like this SelectedEmpInfo.status, when there is no data in status or some of the other columns

    <tr class="active">
          
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fullName)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fatherName)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.cnic)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.dob)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.gender)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.selfStatus)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.religion)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.nationality)</td>
                  
                
                </tr>

    Instead of all that

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

    Instead of

    if(IsPost && SelectedEmpInfo.status=='0')
           {
    bool status = false;
    if(SelectedEmpInfo != null)
     {
       // I am assuming that your status column is an integer of value o or 1 only
       status = SelectedEmpInfo.status.AsBool();
     // If your status column in database is already a boolean you can put... status = SelectedEmpInfo.status;
     }
    
    if(IsPost && status)
        {
    //Update Status code here
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 25, 2015 5:08 PM
  • User2072242043 posted

    Hi, Thanks everyone, specially Jkjhse
    You really helped me allot, I appreciate your kinds.
    Now to the problem i finally reached to the solution.
     as i was wonder why query don't work when i try to update it inside the if/ if(IsPost) method.
    as i earlier said i am a new bay here in ASP.net, so i did a silly mistake and finally after 5 days i found the solution with the help of internet.

    trying to achieve:
    I was trying to do that, when i have all records of employees inside the table with viewMore and Approve button besides each record to perform some functionality.
    When i click ViewMore button, page should redirect to viewmore page where i again fetch single row record of selected employee id.
    after that i wish to edit that record when admin click approve button.

    Solution to the problem:

    @{
        var db=Database.Open("Task6");
    
         
     
       
        if(Request["emp_id"]!=null){
            if(!Request.QueryString["emp_id"].IsEmpty() && Request.QueryString["emp_id"].IsInt())
                    {
                
               var emp_update_id =Convert.ToInt32(Request.QueryString["emp_id"]);
                
                int status=1;
                 
                 var updateStatus = "UPDATE emp_info SET status='"+status+"' WHERE emp_id='"+emp_update_id+"'";
                 db.Execute(updateStatus); 
                 Response.Redirect("~/panel");            
                    }
                   } 
                 
    
    
    
            
    
     }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    		<meta charset="utf-8">
    		<title>Welcome to My panel</title>
    		<meta name="generator" content="Bootply" />
    		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    		<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
            <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
            <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
           
        </head>
        <body>
         
              
            <div class="container">
                <div class="panel-group" style="  margin-top: 27px;">
            <div class="panel panel-success">
            <div class="panel-heading" style="text-align: center;">Welcome @Request["Admin_Name"] </div>
            
            </div>
            </div>
    
    
                <ul class="breadcrumb">
        
        <li><a href="panel.cshtml">Home</a></li>
         <li><a href="panel.cshtml">Back to panel</a></li>
        <li class="active">Permission</li>
    </ul>
                
                
                
      
      <ul class="nav nav-tabs">
        <li class="active"><a data-toggle="tab" href="#details">Details</a></li>
        
      </ul>
    
      <div class="tab-content">
        <div id="details" class="tab-pan e fade in active">
          <h3>Detail information</h3>
          
            <div class="panel panel-default">
        <!-- Default panel contents -->
        
        <div class="panel-body">
           
    
        </div>
        <!-- Table -->
                @{
                    
           
       
    
     //<h2>@empID</h2>
             
        
                if(Request["get_emp_id"]!=null)
                {
                    if(!Request.QueryString["get_emp_id"].IsEmpty() && Request.QueryString["get_emp_id"].IsInt())
                    {
                
               var empID =Request.QueryString["get_emp_id"];
                   
                var SelectEmpInfo = "SELECT * FROM emp_info WHERE emp_id ='"+empID+"'";
                var SelectedEmpInfo =db.QuerySingle(SelectEmpInfo);    
      
                   
                
           
       <div class="bs-example">
        <div class="table-responsive">   
                   
        <table class="table table-bordered">
            <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">
          @if(SelectedEmpInfo!=null)
             {
                    <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> 
            
            <thead>
                
                <tr class="info">
    
                    <th>Current Address</th>
                    <th>City</th>
                    <th>Zip Code </th>
                    <th>Contact Number</th>
                    <th>Intermediate Bise</th>
                    <th>Intermediate Passing Year</th>
                    <th>Intermediate Grade</th>
                    
                    
    
                </tr>
            </thead>
            <tbody>
           
                        
                       
                           
            <tr class="active">
    
                @if(SelectedEmpInfo!=null)
             {
                    <td>@SelectedEmpInfo.currentAddress</td>
                    <td>@SelectedEmpInfo.city</td>
                    <td>@SelectedEmpInfo.zipCode</td>
                    <td>@SelectedEmpInfo.contactNumber</td>
                    <td>@SelectedEmpInfo.intermediateBise</td>
                    <td>@SelectedEmpInfo.intermediatePassingYear</td>
                    <td>@SelectedEmpInfo.intermediateGrade</td>
                    
              }    
                
                </tr>
            </tbody>
    
             <thead>
                <tr class="info">
    
                    <th>Graduate From</th>
                    <th>Graduate In</th>
                    <th>Graduation Year </th>
                    <th>Graduation GPA</th>
                    <th>Master From</th>
                    <th>Master Desiplane</th>
                    <th>Year Of Master</th>
                    <th>Master Score</th>
    
                    
                    
    
                </tr>
            </thead>
            <tbody>
           
                        
                       
                           
            <tr class="active">
                  @if(SelectedEmpInfo!=null)
                {
                    
                    <td>@SelectedEmpInfo.graduateFrom</td>
                    <td>@SelectedEmpInfo.graduationIn</td>
                    <td>@SelectedEmpInfo.graduationYear</td>
                    <td>@SelectedEmpInfo.graduationGPA</td>
                    <td>@SelectedEmpInfo.masterFrom</td>
                    <td>@SelectedEmpInfo.masterDeciplane</td>   
                    <td>@SelectedEmpInfo.yearOfMaster</td>
                    <td>@SelectedEmpInfo.masterScore</td>
                    
                }      
                
                </tr>
               
                     
                 
    
                               
            </tbody> 
    
    
            <thead>
                <tr class="info">
    
                    <th>Other Education</th>
                    <th>Year Of Other Education</th>
                    <th>Score Of Other Education </th>
                    <th>Skills</th>
                    <th>Years of Skills Experience</th>
                    <th>CV</th>
                    <th>Status</th>
                    
    
                    
                    
    
                </tr>
            </thead>
            <tbody>
            
                      
                           
            <tr class="active">
                    @if(SelectedEmpInfo!=null)
                {
                    <td>@SelectedEmpInfo.otherEducation</td>
                    <td>@SelectedEmpInfo.yearOfOtherEducation</td>
                    <td>@SelectedEmpInfo.scoreOfEducation</td>
                    <td>@SelectedEmpInfo.skills</td>
                    <td>@SelectedEmpInfo.yearsOfExperience</td>
                    <td><a href="@SelectedEmpInfo.cv" target="_blank">Download CV</a></td>   
                            if(SelectedEmpInfo.status==1)
                            {
                                 <td><a href="#" class="btn btn-success disabled" role="button" data-value="approve" >Approved</a></td>
                                
                            }
                            else
                            {
                               
                              <td><a href="viewMore.cshtml?emp_id=@SelectedEmpInfo.emp_id" class="btn btn-info" role="button" >Approve</a></td> 
                            }
                    
                }
                    
                
                </tr>
                                          
            </tbody> 
    
    
     </table>
           
            </div>
           </div>
                
     }
       
        }
                }
    
                
    
    
    
        
      </div>
            </div>   
          </div>
                
       
    
                
    
            </div>
              
        </body>
    </html>
    

    Thanks everyone
    Kind regards, 

    Radhesham Khatri
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2015 10:30 AM

All replies

  • User753101303 posted

    Hi,

    Put  a breakpoint in your code and double check this code is really hit. If you are writing the url on the web bar browser it should just not be a Post.

    Thursday, July 23, 2015 2:28 PM
  • User2072242043 posted

    How can i put breakpoints in my code and double check the code?
    any idea any suggestion? where should i make changes??

    Friday, July 24, 2015 12:43 AM
  • User541108374 posted

    Hi,

    you can make use of the built in objects for debugging in Razor: http://www.asp.net/web-pages/overview/testing-and-debugging/introduction-to-debugging to see what's going on.

    It's been a while since I worked with WebMatrix but I don't recall you can place breakpoints.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 1:45 AM
  • User1724605321 posted

    Hi Radhesham,

    Radhesham Khatri

    How can i put breakpoints in my code and double check the code?

    Debugger in webmatrix:http://stackoverflow.com/questions/5112859/how-to-debug-and-or-trace-execution-flow-in-webmatrix

    If you are using visual studio to develop your application ,you could debug in visual studio ,please refer to links below for detail steps:

    https://msdn.microsoft.com/en-us/library/sc65sadd.aspx .

    https://msdn.microsoft.com/en-us/library/vstudio/sc65sadd(v=vs.110).aspx .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 6:19 AM
  • User325035487 posted

    Try putting a name for the submit button

    <input type="submit" name="buttonname"/>

    and checking its value is null after the

    if(IsPost) {

    if (Request["buttonname" != null] {

    ----

    }

    }

    Friday, July 24, 2015 6:43 AM
  • User2072242043 posted

    Thanks for your guidance. 

    If i put query inside the if it output as Record successfully updated but but record is not updated means query don't run only the h1 says Record successfully.

    if(IsPost)
            {
              if(Request.Form["approve"]!=null)
            {
                 var updateStatus="UPDATE emp_info SET status='"+1+"' WHERE emp_id='"+empID+"'";
                 var updatedStatus=db.Execute(updateStatus);
                 <h1>Successfully Updated</h1>   
            }
                
            }

     

    If i put query outside these it works fine. 

    var updateStatus="UPDATE emp_info SET status='"+1+"' WHERE emp_id='"+empID+"'";
                 var updatedStatus=db.Execute(updateStatus);
                 <h1>Successfully Updated</h1>


     if(IsPost)
            {
              if(Request.Form["approve"]!=null)
            {
                //Blank 
    } }

    Friday, July 24, 2015 9:24 AM
  • User325035487 posted

    Maybe you can post complete code. something else might be up.

    Friday, July 24, 2015 11:16 AM
  • User2072242043 posted

    Here is my complete code through which i want to first fetch all the record and than want to update by submitting the approve button.

    The whole code gives no single error but there might be logical errors. when i submit the approve button it works fine gives me output as Record successfully updated but it actually do not run the query it only prints h1.

    @{
        var db=Database.Open("Task6");
    
         
     var empID= "";
        
    
        if(!IsPost){
            if(!Request.QueryString["emp_id"].IsEmpty() && Request.QueryString["emp_id"].IsInt()){
                
                empID = Request.QueryString["emp_id"];
                
              
              
           
        }
        }
        <h1>@empID</h1>
        
                
           if(IsPost)
           {
               if(Request.Form["approve"]!=null)
            {
                 var updateStatus="UPDATE emp_info SET status='"+1+"' WHERE emp_id='"+empID+"'";
                 var updatedStatus=db.Query(updateStatus);
                 <h1>Successfully Updated</h1>   
          
                
            }
           }
              
           
          
        
    
       
    
    
                var SelectPersonalInfo = "SELECT * FROM emp_info WHERE emp_id = '"+empID+"'";
                var SelectedEmpInfo=db.Query(SelectPersonalInfo);
                
             
                  
           
                   
          }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    		<meta charset="utf-8">
    		<title>Welcome to My panel</title>
    		<meta name="generator" content="Bootply" />
    		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    		<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
            <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
            <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
           
        </head>
        <body>
          <form action="" method="post">
            <div class="container-fluid">
                <div class="panel-group" style="  margin-top: 27px;">
            <div class="panel panel-success">
            <div class="panel-heading" style="text-align: center;">Welcome @Request["Admin_Name"] </div>
            
            </div>
            </div>
    
    
                <ul class="breadcrumb">
        <li><a href="#">Home</a></li>
        <li><a href="#">Mantaine</a></li>
        <li class="active">Permission</li>
    </ul>
                
                
                
      
      <ul class="nav nav-tabs">
        <li class="active"><a data-toggle="tab" href="#details">Details</a></li>
        
      </ul>
    
      <div class="tab-content">
        <div id="details" class="tab-pan e fade in active">
          <h3>Detail information</h3>
          
            <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading"></div>
        <div class="panel-body">
           
        </div>
        <!-- Table -->
       <div class="bs-example">
        <div class="table-responsive">          
        <table class="table table-bordered">
            <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>
            @foreach(var personal in SelectedEmpInfo)
                    { 
                        
                       
                           
            <tr class="active">
                    
                    <td>@personal.fullName</td>
                    <td>@personal.fatherName</td>
                    <td>@personal.cnic</td>
                    <td>@personal.dob</td>
                    <td>@personal.gender</td>
                    <td>@personal.selfStatus</td>
                    <td>@personal.religion</td>
                    <td>@personal.nationality</td>
                    
                
                </tr>
               
                       }
                 
    
                               
            </tbody> 
        </table>
            </div>
           </div>
                <br/>
    
    
                <div class="bs-example">
                <div class="table-responsive"> 
                <table class="table table-bordered">
            <thead>
                <tr class="info">
    
                    <th>Current Address</th>
                    <th>City</th>
                    <th>Zip Code </th>
                    <th>Contact Number</th>
                    <th>Intermediate Bise</th>
                    <th>Intermediate Passing Year</th>
                    <th>Intermediate Grade</th>
                    
                    
    
                </tr>
            </thead>
            <tbody>
            @foreach(var personal in SelectedEmpInfo)
                    { 
                        
                       
                           
            <tr class="active">
                    
                    <td>@personal.currentAddress</td>
                    <td>@personal.city</td>
                    <td>@personal.zipCode</td>
                    <td>@personal.contactNumber</td>
                    <td>@personal.intermediateBise</td>
                    <td>@personal.intermediatePassingYear</td>
                    <td>@personal.intermediateGrade</td>
                    
                    
                
                </tr>
               
                       }
                 
    
                               
            </tbody> 
        </table>
    </div>
         </div>
                <br/>
                <div class="bs-example">
                <div class="table-responsive">
                <table class="table table-bordered">
            <thead>
                <tr class="info">
    
                    <th>Graduate From</th>
                    <th>Graduate In</th>
                    <th>Graduation Year </th>
                    <th>Graduation GPA</th>
                    <th>Master From</th>
                    <th>Master Desiplane</th>
                    <th>Year Of Master</th>
                    <th>Master Score</th>
    
                    
                    
    
                </tr>
            </thead>
            <tbody>
            @foreach(var personal in SelectedEmpInfo)
                    { 
                        
                       
                           
            <tr class="active">
                    
                    <td>@personal.graduateFrom</td>
                    <td>@personal.graduationIn</td>
                    <td>@personal.graduationYear</td>
                    <td>@personal.graduationGPA</td>
                    <td>@personal.masterFrom</td>
                    <td>@personal.masterDeciplane</td>   
                    <td>@personal.yearOfMaster</td>
                    <td>@personal.masterScore</td>
                    
                    
                
                </tr>
               
                       }
                 
    
                               
            </tbody> 
        </table>
                    </div>
                    </div>
                <div class="bs-example">
                <div class="table-responsive">
                <table class="table table-bordered">
            <thead>
                <tr class="info">
    
                    <th>Other Education</th>
                    <th>Year Of Other Education</th>
                    <th>Score Of Other Education </th>
                    <th>Skills</th>
                    <th>Years of Skills Experience</th>
                    <th>CV</th>
                    <th>Status</th>
                    
    
                    
                    
    
                </tr>
            </thead>
            <tbody>
            @foreach(var personal in SelectedEmpInfo)
                    { 
                        
                       
                           
            <tr class="active">
                    
                    <td>@personal.otherEducation</td>
                    <td>@personal.yearOfOtherEducation</td>
                    <td>@personal.scoreOfEducation</td>
                    <td>@personal.skills</td>
                    <td>@personal.yearsOfExperience</td>
                    <td><a href="@personal.cv" target="_blank">Download CV</a></td>   
                   <td><input type="submit" value="Approve" name="approve"></td>
                    
                    
                
                </tr>
               
                       }
                 
    
                               
            </tbody> 
        </table>
                    </div>
                    </div>
    
                    </div>
    
    
    
    
        
      </div>
               
          </div>
                
       
    
                
    
            </div>
              </form>
        </body>
    </html>
    

    Friday, July 24, 2015 11:21 AM
  • User325035487 posted
                var updateStatus = "UPDATE emp_info SET status='" + 1 + "' WHERE emp_id='" + empID + "'";
                var updatedStatus = db.Query(updateStatus);

    Replace that with

    var updateStatus = "UPDATE emp_info SET status=@0 WHERE emp_id=@0";
    db.Execute(updateStatus,true,empID);

    See that i replace the second line totally.. I never seen db.Query for update. Only db.Execute

    Assuming 'status' is a boolean value. Other wise replace true with 1. Cant think of anything else. Lol

    Since it is printing the <h1> tag it shows the Is post with form check is working fine.

    Also I reccomend to bring the Input tag just after the table. Before the form tag. Currently you must be getting approve buttons in each row.

    If only one row is being returned, then in the data load query. Use QuerySingle. So that you dont need to write foreach loop for every display html

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

    You could do this then

    <tr class="active">               
                    <td>@SelectedEmpInfo.otherEducation</td>
                    <td>@SelectedEmpInfo.yearOfOtherEducation</td>
                    <td>@SelectedEmpInfo.scoreOfEducation</td>
                    <td>@SelectedEmpInfo.skills</td>
                    <td>@SelectedEmpInfo.yearsOfExperience</td>
                    <td><a href="@SelectedEmpInfo.cv" target="_blank">Download CV</a></td>   
                    <td><input type="submit" value="Approve" name="approve"></td>                      
    </tr>

    Friday, July 24, 2015 1:53 PM
  • User2072242043 posted

    Many thanks for your guidance.

    I am new to ASP.NET i am actually a PHP developer since last 3 years but now i decided to move here at Microsoft platform. making many mistakes and learning from them is the meaning of life.

    As you filterd my code and really i learn single record query format from your side and will never forget you.

    in my case status is a string and want to put one manually. i edited my code and removed foreach loop and call fetch the data by way of yours. but still i face an error when i submit the form i face this error

    Server Error in '/' Application.

    0 :  - Input string was not in a correct format.
    
    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. 
    
    Exception Details: System.FormatException: 0 :  - Input string was not in a correct format.
    
    Source Error: 
    
    
    Line 20:     }
    Line 21:          var SelectEmpInfo = "SELECT * FROM emp_info WHERE emp_id =@0";
    Line 22:          var SelectedEmpInfo = db.QuerySingle(SelectEmpInfo,empID);
    Line 23: 
    Line 24:     

    Here is my code:

    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>   
          
                
            }
           }

    Friday, July 24, 2015 3:15 PM
  • User2072242043 posted

    I found the solution to this problem.

    When page get loaded second time than id has no value inside it and whole fetched columns became empty that's why it gave an error.
    I overcame with this problem by putting that code in each column.

    <tr class="active">
          
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fullName)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fatherName)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.cnic)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.dob)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.gender)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.selfStatus)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.religion)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.nationality)</td>
                  
                
                </tr>

    But still my query don't work it only prints h1 as successfully updated but nothing is updated.
    Please some one help me.

    Saturday, July 25, 2015 2:35 AM
  • User325035487 posted

    Your way of query works but is open to sql injection attacks.. Read http://www.mikesdotnetting.com/article/113/preventing-sql-injection-in-asp-net

    In fact i learned Web Pages from his site. You would find most of the article extremely useful.

    Saturday, July 25, 2015 6:51 AM
  • User2072242043 posted

    I also learned many thing  from his site. SQL Injection is another thing which relates but in my code i don't know still why query don't work. Ok as you said it is open to SQL injection i am going to read this article carefully once again.
    Beside it i have analyzed the things which might go wrong but how to handle these things is became hard.
    I have tried this code

    if(IsPost && SelectedEmpInfo.status=='0')
           {
              
               
              var updateStatus = "UPDATE emp_info SET status='1' WHERE emp_id='"+empID+"'";
              db.Execute(updateStatus);   
                    
                <h1>Successfully Updated</h1> 
                
                
            
           }

    It generates same error again as

    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. 
    
    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
    
    Source Error: 
    
    
    Line 23: 
    Line 24:          
    Line 25:        if(IsPost && SelectedEmpInfo.status=='0')
    Line 26:        {
    Line 27:           

    i think select query returns null values and that's why i am facing this error but don't know how to handle it.

    Saturday, July 25, 2015 7:05 AM
  • User325035487 posted

    If you did not have solution and your query is still not working. Why mark as answer?

    Saturday, July 25, 2015 10:49 AM
  • User2072242043 posted

    I am still new here and don't understand the strategies of this site and how they provide points etc.
    Sorry for my mark.
    Have you found any reason why it don't work??

    Saturday, July 25, 2015 2:40 PM
  • User325035487 posted

    What is the data type of 'status' column in your database?

    Any way the null reference is coming because you put like this SelectedEmpInfo.status, when there is no data in status or some of the other columns

    <tr class="active">
          
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fullName)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.fatherName)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.cnic)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.dob)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.gender)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.selfStatus)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.religion)</td>
                    <td>@(SelectedEmpInfo==null?String.Empty:SelectedEmpInfo.nationality)</td>
                  
                
                </tr>

    Instead of all that

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

    Instead of

    if(IsPost && SelectedEmpInfo.status=='0')
           {
    bool status = false;
    if(SelectedEmpInfo != null)
     {
       // I am assuming that your status column is an integer of value o or 1 only
       status = SelectedEmpInfo.status.AsBool();
     // If your status column in database is already a boolean you can put... status = SelectedEmpInfo.status;
     }
    
    if(IsPost && status)
        {
    //Update Status code here
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 25, 2015 5:08 PM
  • User2072242043 posted
     var db=Database.Open("Task6");
     
         
     var empID=0;
       
     
     <h2>@empID</h2>
             
        
        if(!IsPost){
            if(!Request.QueryString["emp_id"].IsEmpty() && Request.QueryString["emp_id"].IsInt()){
                
                empID =Convert.ToInt32(Request.QueryString["emp_id"]);
                
              
              
           
        }
        }
        <h2>@empID</h2>
     
             var SelectEmpInfo = "SELECT * FROM emp_info WHERE emp_id =@0";
             var SelectedEmpInfo = db.QuerySingle(SelectEmpInfo,empID);
     
                
                 
     
        
        int status =0; 
        if(SelectedEmpInfo != null)
            {
       
       status =SelectedEmpInfo.status;
     
            }
            <h2>@status</h2>
     
    if(IsPost)
            {
                
                 status=1;
                 var updateStatus = "UPDATE emp_info SET status=@0 WHERE emp_id=@1";
                 db.Execute(updateStatus,status,empID);  
                 <h2>Record Succesfully Updated</h2>  
                
                 
            } 
     
           
            <h2>@status</h2>  
           
        }
    But eventually result is same, QUERY DON't WORK.
    I Debugged The code with many variations since start. i also passed it from SQL injection.
    Finally, Is there anyone who can get me out of this bugg???
    Sunday, July 26, 2015 1:33 AM
  • User325035487 posted

    Take Screen shot of table definition and post

    Sunday, July 26, 2015 5:31 AM
  • User2072242043 posted

    I am unable to upload image here. it only prompt and ask insert url of image whereas i have image on my machine.

    Sunday, July 26, 2015 8:57 AM
  • User325035487 posted

    Undecided http://imgur.com/

    upload there and copy the Direct Link with the extension.

    Sunday, July 26, 2015 9:49 AM
  • User2072242043 posted

    http://imgur.com/SPVsz9Z

    Sunday, July 26, 2015 10:38 AM
  • User2072242043 posted

    Have you found the bug in my code???

    Monday, July 27, 2015 1:27 PM
  • User325035487 posted

    if(IsPost)
            {           
                 status=1;
                 var updateStatus = "UPDATE emp_info SET status=@0 WHERE emp_id=@1";
                 db.Execute(updateStatus,status,empID);  
    <h2>Record Succesfully Updated</h2> }

    That one you have should work.

    Can you check if this query is returning value. First set one value to 1 and another employee to 0 manually from database to see for both cases

    var sqlcheck = "SELECT status FROM emp_info WHERE emp_id=@1";

    var status = db.QueryValue(sqlcheck,empID);

    <p>@status<p>

    this should display 1 and 0 as is the case for the two employees you added manually above. 

    Tuesday, July 28, 2015 1:50 AM
  • User2072242043 posted

    Hi, Thanks everyone, specially Jkjhse
    You really helped me allot, I appreciate your kinds.
    Now to the problem i finally reached to the solution.
     as i was wonder why query don't work when i try to update it inside the if/ if(IsPost) method.
    as i earlier said i am a new bay here in ASP.net, so i did a silly mistake and finally after 5 days i found the solution with the help of internet.

    trying to achieve:
    I was trying to do that, when i have all records of employees inside the table with viewMore and Approve button besides each record to perform some functionality.
    When i click ViewMore button, page should redirect to viewmore page where i again fetch single row record of selected employee id.
    after that i wish to edit that record when admin click approve button.

    Solution to the problem:

    @{
        var db=Database.Open("Task6");
    
         
     
       
        if(Request["emp_id"]!=null){
            if(!Request.QueryString["emp_id"].IsEmpty() && Request.QueryString["emp_id"].IsInt())
                    {
                
               var emp_update_id =Convert.ToInt32(Request.QueryString["emp_id"]);
                
                int status=1;
                 
                 var updateStatus = "UPDATE emp_info SET status='"+status+"' WHERE emp_id='"+emp_update_id+"'";
                 db.Execute(updateStatus); 
                 Response.Redirect("~/panel");            
                    }
                   } 
                 
    
    
    
            
    
     }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    		<meta charset="utf-8">
    		<title>Welcome to My panel</title>
    		<meta name="generator" content="Bootply" />
    		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    		<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
            <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
            <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
           
        </head>
        <body>
         
              
            <div class="container">
                <div class="panel-group" style="  margin-top: 27px;">
            <div class="panel panel-success">
            <div class="panel-heading" style="text-align: center;">Welcome @Request["Admin_Name"] </div>
            
            </div>
            </div>
    
    
                <ul class="breadcrumb">
        
        <li><a href="panel.cshtml">Home</a></li>
         <li><a href="panel.cshtml">Back to panel</a></li>
        <li class="active">Permission</li>
    </ul>
                
                
                
      
      <ul class="nav nav-tabs">
        <li class="active"><a data-toggle="tab" href="#details">Details</a></li>
        
      </ul>
    
      <div class="tab-content">
        <div id="details" class="tab-pan e fade in active">
          <h3>Detail information</h3>
          
            <div class="panel panel-default">
        <!-- Default panel contents -->
        
        <div class="panel-body">
           
    
        </div>
        <!-- Table -->
                @{
                    
           
       
    
     //<h2>@empID</h2>
             
        
                if(Request["get_emp_id"]!=null)
                {
                    if(!Request.QueryString["get_emp_id"].IsEmpty() && Request.QueryString["get_emp_id"].IsInt())
                    {
                
               var empID =Request.QueryString["get_emp_id"];
                   
                var SelectEmpInfo = "SELECT * FROM emp_info WHERE emp_id ='"+empID+"'";
                var SelectedEmpInfo =db.QuerySingle(SelectEmpInfo);    
      
                   
                
           
       <div class="bs-example">
        <div class="table-responsive">   
                   
        <table class="table table-bordered">
            <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">
          @if(SelectedEmpInfo!=null)
             {
                    <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> 
            
            <thead>
                
                <tr class="info">
    
                    <th>Current Address</th>
                    <th>City</th>
                    <th>Zip Code </th>
                    <th>Contact Number</th>
                    <th>Intermediate Bise</th>
                    <th>Intermediate Passing Year</th>
                    <th>Intermediate Grade</th>
                    
                    
    
                </tr>
            </thead>
            <tbody>
           
                        
                       
                           
            <tr class="active">
    
                @if(SelectedEmpInfo!=null)
             {
                    <td>@SelectedEmpInfo.currentAddress</td>
                    <td>@SelectedEmpInfo.city</td>
                    <td>@SelectedEmpInfo.zipCode</td>
                    <td>@SelectedEmpInfo.contactNumber</td>
                    <td>@SelectedEmpInfo.intermediateBise</td>
                    <td>@SelectedEmpInfo.intermediatePassingYear</td>
                    <td>@SelectedEmpInfo.intermediateGrade</td>
                    
              }    
                
                </tr>
            </tbody>
    
             <thead>
                <tr class="info">
    
                    <th>Graduate From</th>
                    <th>Graduate In</th>
                    <th>Graduation Year </th>
                    <th>Graduation GPA</th>
                    <th>Master From</th>
                    <th>Master Desiplane</th>
                    <th>Year Of Master</th>
                    <th>Master Score</th>
    
                    
                    
    
                </tr>
            </thead>
            <tbody>
           
                        
                       
                           
            <tr class="active">
                  @if(SelectedEmpInfo!=null)
                {
                    
                    <td>@SelectedEmpInfo.graduateFrom</td>
                    <td>@SelectedEmpInfo.graduationIn</td>
                    <td>@SelectedEmpInfo.graduationYear</td>
                    <td>@SelectedEmpInfo.graduationGPA</td>
                    <td>@SelectedEmpInfo.masterFrom</td>
                    <td>@SelectedEmpInfo.masterDeciplane</td>   
                    <td>@SelectedEmpInfo.yearOfMaster</td>
                    <td>@SelectedEmpInfo.masterScore</td>
                    
                }      
                
                </tr>
               
                     
                 
    
                               
            </tbody> 
    
    
            <thead>
                <tr class="info">
    
                    <th>Other Education</th>
                    <th>Year Of Other Education</th>
                    <th>Score Of Other Education </th>
                    <th>Skills</th>
                    <th>Years of Skills Experience</th>
                    <th>CV</th>
                    <th>Status</th>
                    
    
                    
                    
    
                </tr>
            </thead>
            <tbody>
            
                      
                           
            <tr class="active">
                    @if(SelectedEmpInfo!=null)
                {
                    <td>@SelectedEmpInfo.otherEducation</td>
                    <td>@SelectedEmpInfo.yearOfOtherEducation</td>
                    <td>@SelectedEmpInfo.scoreOfEducation</td>
                    <td>@SelectedEmpInfo.skills</td>
                    <td>@SelectedEmpInfo.yearsOfExperience</td>
                    <td><a href="@SelectedEmpInfo.cv" target="_blank">Download CV</a></td>   
                            if(SelectedEmpInfo.status==1)
                            {
                                 <td><a href="#" class="btn btn-success disabled" role="button" data-value="approve" >Approved</a></td>
                                
                            }
                            else
                            {
                               
                              <td><a href="viewMore.cshtml?emp_id=@SelectedEmpInfo.emp_id" class="btn btn-info" role="button" >Approve</a></td> 
                            }
                    
                }
                    
                
                </tr>
                                          
            </tbody> 
    
    
     </table>
           
            </div>
           </div>
                
     }
       
        }
                }
    
                
    
    
    
        
      </div>
            </div>   
          </div>
                
       
    
                
    
            </div>
              
        </body>
    </html>
    

    Thanks everyone
    Kind regards, 

    Radhesham Khatri
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2015 10:30 AM