locked
table is not updated with image2 field. but image is stored in images folder. RRS feed

  • Question

  • User1493762548 posted

    here is code of image2.cshtml page i tried both var productId =urlData[0].AsInt( ); and var ProductId=Reques["Id"]; in first line after if condition.

    i think there is error in updateCommand  or db.Execute.

    addproducts.cshtml. redirect after insterting the record in products table  by the statement

    Response.Redirect(Href("Image2",db.GetLastInsertId()));

    product table has columns like productname, Image1 Image2 ....etc.

    @{
        if (!WebSecurity.IsAuthenticated)
          {
              Response.Redirect("~/Account/Login");
          }
           var ProductId = Request["Id"];
           var width=320;
        var height=320;
        var Image2="";
     
       
        if(IsPost && Validation.IsValid()){
            //-----image code--
            
          var photo = WebImage.GetImageFromRequest();
           
            if(photo == null ){
                ModelState.AddFormError("please upload jpg image!");
            }
            else{
            var FileExtension=Path.GetExtension(photo.FileName).Trim();
            if( FileExtension!=".JPEG"){  
                Validation.AddFormError("please upload jpeg image!");
            }
            
                photo.Resize(width,height);
                Image2 = Guid.NewGuid().ToString() + "_" +
                    Path.GetFileName(photo.FileName);
               var Image2Path = @"Images\" + Image2;
     
                photo.Save(@"~\" + Image2Path);
     
                }
                 var db = Database.Open("PhotoGallery");
                  var updateCommand = "UPDATE Products SET Image2=@0 where ProductId=@1";
                db.Execute(updateCommand, Image2,ProductId);
                Response.Redirect("~/Image3",db.GetLastInsertId());
                }
        
    }
     
    <!DOCTYPE html>
     
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <form>
            <fieldset>
          <legend> Upload Image </legend>
          <label for="Image">Image</label>
          <input type="file" name="Image" />
             
            &nbsp<br/>
          <input type="submit" value="Add picture" />
                &nbsp<br/>
     
        </fieldset>
      </form>
            <a href="~/"><button>Finish</button></a>
        </body>
    </html>
    
    Sunday, July 3, 2016 1:01 AM

Answers

  • User-821857111 posted

    If you are using the Href helper to generate the URL, the value should be available from UrData[0]. Double check the URL that the AddProducts page is redirecting to and ensure that the value obtained form db.GetLastInsertId is not null and is being passed correctly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 6, 2016 8:24 AM