locked
Uploaded image path name and file disappear upon edit page RRS feed

  • Question

  • User-1557173817 posted

    Hello i uploaded an image to an image folder and stored the file name and path in my database but each time i edit that column or page, the image path and file name stored in the database disappears. please any help.

    Thursday, October 15, 2015 8:22 AM

Answers

  • User753101303 posted

    So as I said if no photo is uploaded imagePath is blank but you still update it in your UPDATE query. You have several way to fix this. You could try :

      var updateCommand = "UPDATE patient SET Name=@0, Address=@1, Email=@2, Phone=@3, Genotype=@4, BloodGroup=@5, Sex=@6, DOB=@7, HMO=@8, CardType=@9,";
    if (photo!=null)
    {
    	updateCommand+="newFileName=@10, imagePath=@11,";
    }
    updateCommand+="RegNo=@12 WHERE PiD=@13";
    db.Execute(updateCommand, Name, Address, Email,phone, Genotype, BloodGroup, Sex, Age, HMO, Cardtype, newFileName, imagePath, reg,ID);

    I don't remember to have tried but I believe it should work (the point is that you still pass newFileName and imagePath but those values won't always be used in your SQL statement).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 10:20 AM

All replies

  • User753101303 posted

    Hi,

    Double check your code. More likely your code currently ALWAYS update this column even if not file is posted ending up with a NULL or empty path. You should likely have a test in your code so that if no file is posted this column is not taken into account at all in your update statement to left this column unchanged.

    You'll likely have to show some code (just the relevant portion please) if you need further help.

    Thursday, October 15, 2015 9:07 AM
  • User-1557173817 posted

    ok, thanks.

    razor code:

    @{
        var Name = "";
        var Address = "";
        var Email = "";
        var phone = "";
        var Genotype = "";
        var BloodGroup = ""; 
        var Sex = "";
        var Age ="";
        var HMO = "";
        var reg="";
        var Cardtype="";
        var imagePath="";
        var newFileName="";
        WebImage photo=null;
        var ID = "";
     
        if(!IsPost){
            if(!Request.QueryString["ID"].IsEmpty() && Request.QueryString["ID"].IsInt()){
                ID = Request.QueryString["ID"];
                var db = Database.Open("StarterSite");
                var dbCommand = "SELECT * FROM patient WHERE PiD = @0";
                var row = db.QuerySingle(dbCommand, ID);
                if(row != null) {
                    Name = row.Name;
                    Address = row.Address;
                    Email = row.Email;
                    phone = row.phone;
                    Genotype = row.Genotype;
                    BloodGroup = row.BloodGroup;
                    Sex= row.Sex;
                    Age = row.DOB;
                    HMO =row.HMO;
                    reg = row.RegNo;
                    Cardtype = row.Cardtype;
                    imagePath= row.imagePath;
                    newFileName=row.newFileName;
     
                    
                }
                else{
                    Validation.AddFormError("No Patient  was found for that ID.");
                }
            }
            else{
                Validation.AddFormError("No Patient was found for that ID.");
            }
        }
          if(IsPost) {
     
              Validation.RequireFields("Name", "Address", "Email", "phone", "Genotype", "BloodGroup", "Sex", "Age", "Cardtype");
     
               Name = Request.Form["Name"];
               Address = Request.Form["Address"];
               Email = Request.Form["Email"];
               phone = Request.Form["phone"];
               Genotype = Request.Form["Genotype"];
               BloodGroup = Request.Form["BloodGroup"];
               Sex = Request.Form["Sex"];
               Age = Request.Form["Age"];
               HMO = Request.Form["HMO"];
               Cardtype = Request.Form["Cardtype"];
               reg = Request.Form["reg"];
              
              
          photo=WebImage.GetImageFromRequest();
           if(photo !=null)
           {
               newFileName=Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName);
               imagePath=@"Photos\" + newFileName;
               photo.Save(@"~\" + imagePath);
           }
     
            ID = Request.Form["ID"];
     
            if(Validation.IsValid()){
            var db = Database.Open("StarterSite");
            var updateCommand = "UPDATE patient SET Name=@0, Address=@1, Email=@2, Phone=@3, Genotype=@4, BloodGroup=@5, Sex=@6, DOB=@7, HMO=@8, CardType=@9, newFileName=@10, imagePath=@11, RegNo=@12 WHERE PiD=@13";
            db.Execute(updateCommand, Name, Address, Email,phone, Genotype, BloodGroup, Sex, Age, HMO, Cardtype, newFileName, imagePath, reg, ID);
            Response.Redirect("Patient");
        }
    }
          }
       
    html code:
     <form method="post">
                             <fieldset>
                                  <label for="imagePath">Upload a Photo : </label>
                        <input type="file" name="imagePath" />
                        <br></br>
                        <div style="padding: 10px;">
                        @if (imagePath !="" )
                        {
                           <img src="../@imagePath" alt="photo" width="150px" height="150px" />
                        }
                        </div>
                                            
                        
                                 <div class="col-lg-6">
                                    <label for=" Name"> Name</label>
                                    <input class="form-control" type="text" name="Name" value="@Name" style="width: 420px;" />
                                    <p class="help-block">       
              @Html.ValidationMessage("Name")
                                    </p>
                                     </div>
                                 <div class="col-lg-6">
                                    <label for="reg"> Reg No</label>
                                    <input class="form-control" type="text" name="reg" value="@reg" style="width: 420px;" />
                                    <p class="help-block">       
              @Html.ValidationMessage("reg")
                                    </p>
                                     </div>
                        
                                 <div class="col-lg-6">
                                 <label for=" Email">Email</label>
                         <input class="form-control" type="text" name="Email" value="@Email" style="width: 420px;" />
                         <p class="help-block"> @Html.ValidationMessage("Email")</p>
                                     </div>
     
                                 <div class="col-lg-6">
                                 <label for="phone">Phone Number</label>
                         <input class="form-control" type="text" name="phone" value="@phone" style="width: 420px;" />
                         <p class="help-block"> @Html.ValidationMessage("phone")</p>
                                     </div>
     
                                 <div class="col-lg-6">
                                 <label for="Genotype"> Genotype</label>
                         <p></p><select name="Genotype" id="Genotype" class="form-control" style="width: 420px;"> 
                         <option selected=@(Request.Form["Genotype"] == "AA") value="AA">AA</option>
                         <option selected=@(Request.Form["Genotype"] == "AS") value="AS">AS</option>
                         <option selected=@(Request.Form["Genotype"] == "SS") value="SS">SS</option>
                         <option selected="@Genotype" value="@Genotype">@Genotype</option>
                         </select>
                         <p class="help-block"> @Html.ValidationMessage("Genotype")</p>
                                     </div>
     
                                 <div class="col-lg-6">
                                 <label for="BloodGroup">Blood Group</label>
                        <p></p> <select name="BloodGroup" id="BloodGroup" class="form-control" style="width: 420px;">
                         <option selected=@(Request.Form["BloodGroup"] == "A Positive") value="A Positive">A Positive</option>
                         <option selected=@(Request.Form["BloodGroup"] == "A Negative") value="A Negative">A Negative</option>
                         <option selected=@(Request.Form["BloodGroup"] == "B Positive") value="B Positive">B Positive</option>
                         <option selected=@(Request.Form["BloodGroup"] == "B Negative") value="B Negative">B Negative</option>
                          <option selected=@(Request.Form["BloodGroup"] == "AB Positive") value="AB Positive">AB Positive</option>
                          <option selected=@(Request.Form["BloodGroup"] == "AB Negative") value="AB Negative">AB Negative</option>
                          <option selected=@(Request.Form["BloodGroup"] == "O Positive") value="O Positive">O Positive</option>
                          <option selected=@(Request.Form["BloodGroup"] == "O Negative") value="O Negative">O Negative</option>
                         <option selected="@BloodGroup" value="@BloodGroup">@BloodGroup</option>
                              </select>
                         <p class="help-block"> @Html.ValidationMessage("BloodGroup")</p>
                                     </div>
     
                                 <div class="col-lg-6">
                                 <label for="Sex">Sex</label>
                       <p></p>  <select name="Sex" id="Sex" class="form-control" style="width: 420px;" >
                         <option selected=@(Request.Form["Sex"] == "Male") value="Male" >Male</option>
                         <option selected=@(Request.Form["Sex"] == "Female") value="Female">Female</option>
                          <option selected="@Sex" value="@Sex">@Sex</option>
                         </select>
                         <p class="help-block"> @Html.ValidationMessage("Sex")</p>
                                     </div>
                                 <div class="col-lg-6">
                                 <label for="Age">Date Of Birth</label>
                         <input class="form-control" type="text" name="Age" value="@Age" style="width: 420px;" />
                         <p class="help-block"> @Html.ValidationMessage("Age")</p>
                                 </div>
                                 <div class="col-lg-6">
                                  <label for="HMO">HMO</label>
                         <input class="form-control" type="text" name="HMO" value="@HMO" style="width: 420px;" />
                         <p class="help-block"> @Html.ValidationMessage("HMO")</p>
                                  </div>
                                 
                                 <div class="col-lg-6">
                          <label for="Cardtype">Card Type</label>
                       <p>  <select name="Cardtype" id="Cardtype" class="form-control" style="width: 420px;" >
                         <option selected=@(Request.Form["Cardtype"] == "Personal") value="Personal" >Personal</option>
                         <option selected=@(Request.Form["Cardtype"] == "Family") value="Family">Family</option>
                         <option selected=@(Request.Form["Cardtype"] == "Company") value="Company">Company</option>
                          <option selected="@Cardtype" value="@Cardtype">@Cardtype</option>
                         </select>
                           <p class="help-block"> @Html.ValidationMessage("Cardtype")</p>
                                     </div>
     
                                &nbsp; &nbsp;  <label for="Address">Address</label>
                         &nbsp; &nbsp; &nbsp; <input class="form-control" type="text" name="Address" value="@Address" style="width: 435px;" />
                         <p class="help-block"> @Html.ValidationMessage("Address")</p>
     
                                                   
                                 
                       
     
     
                                 <input type="hidden" name="ID" value="@ID" />
     
                         <p><input type="submit" name="ButtonSubmit" class="btn btn-primary" value="Save Changes" /></p>
                                </div>
                    </fieldset>
                    
             </form>
    Thursday, October 15, 2015 9:17 AM
  • User753101303 posted

    So as I said if no photo is uploaded imagePath is blank but you still update it in your UPDATE query. You have several way to fix this. You could try :

      var updateCommand = "UPDATE patient SET Name=@0, Address=@1, Email=@2, Phone=@3, Genotype=@4, BloodGroup=@5, Sex=@6, DOB=@7, HMO=@8, CardType=@9,";
    if (photo!=null)
    {
    	updateCommand+="newFileName=@10, imagePath=@11,";
    }
    updateCommand+="RegNo=@12 WHERE PiD=@13";
    db.Execute(updateCommand, Name, Address, Email,phone, Genotype, BloodGroup, Sex, Age, HMO, Cardtype, newFileName, imagePath, reg,ID);

    I don't remember to have tried but I believe it should work (the point is that you still pass newFileName and imagePath but those values won't always be used in your SQL statement).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 10:20 AM
  • User-1557173817 posted

    Thanks a million...it worked....Smile

    Thursday, October 15, 2015 10:31 AM