locked
Unable to edit or update page database RRS feed

  • Question

  • User-64994166 posted

    Hi every one and many many thanks to all of you for helping and learning each other.

    here i've also a question and i am unable to resolve this.  actually i dont know what the issue with the code that i cant update my database.

    Here is my code. please have a look.

      WebSecurity.RequireAuthenticatedUser();
        Layout = "~/Admin/_AdminLayout.cshtml";
        Page.Title = "Edit Products";
    
    var id=0;
    var title = "";
        var url = "";
        var home = "";
        var metad ="";
        var metak = "";
        var content = "";
        var Prid ="";
        //var contentId = UrlData[0].AsInt();
    if(!IsPost){
            if(!Request.QueryString["PageId"].IsEmpty()){
                Prid = Request.QueryString["PageId"];
                var dbd = Database.Open("gee1");
                var dbCommand = "SELECT Pages.PageId, Title, Url, Content, IsHomePage, MetaDescription, MetaKeywords FROM Pages INNER JOIN PagesContent ON Pages.PageId = PagesContent.PageId  WHERE ContentId  = @0";
                var row = dbd.QuerySingle(dbCommand, Prid);
                title = row.Title;
                url = row.Url;
                content = row.Content;
                metad = row.MetaDescription;
                metak = row.MetaKeywords;
            }
            else{
                Validation.AddFormError("No Product was selected.");
            }
        }
        if(IsPost){
              Validation.Add("title", Validator.Required("You must provide a Title"),Validator.StringLength(maxLength: 150, minLength: 5,
                         errorMessage: "You must provide at least 6 characters Title"));
        Validation.Add("url", Validator.Required("You must provide a Page url for ur page") , Validator.StringLength(150));
        Validation.Add("metad",Validator.Required("You must provide a lil Meta description for ur page ") , Validator.StringLength(250));
        Validation.Add("metak",Validator.Required("You must provide a lil Meta keywords for ur page ") , Validator.StringLength(1000));
        Validation.Add("content",Validator.Required("You must provide a lil content for ur page ") , Validator.StringLength(2500));
            title = Request.Form["title"];
            url = Request.Form["url"];
            content = Request.Unvalidated["content"];
            metad = Request.Unvalidated["metad"];
            metak = Request.Unvalidated["metak"];
            Prid= Request.Form["Prid"];
            //contentId = Request.Form["contentId"];
            if(Validation.IsValid()){
                var dbu = Database.Open("gee1");
                var updateCommand = "UPDATE Pages SET Title=@0,Url=@1,MetaDescription=@2,MetaKeywords=@3 WHERE PageId=@4";
                dbu.Execute(updateCommand,title,url,metad,metak, Prid);
                //if(IsPost){
                //updateCommand = "UPDATE PagesContent SET Content=@0 WHERE ContentId=@1";
                //dbu.Execute(updateCommand,content,contentId);
               Response.Redirect("~/Admin/manage/pages/managepage");
    //}
                
           }
        }
    
    }
    
       
    <section class="pdl-adm">
        <p>Click <a href="~/Admin/manage/products">HERE</a> to go to main</p>
      <form id="tetimonials" method="post" action="">
    <fieldset>@Html.ValidationSummary("Errors with your submission:")
    <legend>Add New Page</legend>
        
    <div><label for="catn">Title:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input id="cn"  type="text"value="@title" name="title" @Validation.For("title") /><br>@Html.ValidationMessage("title")</div></div>
    <div><label for="catn">Page Url:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input style="text-transform: lowercase" id="cau1"  type="text" value="@url" name="url" @Validation.For("url") /><br>@Html.ValidationMessage("url")</div></div>
    <div><label for="catn">Is this home page?:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input value="False"  type="text"  name="home" @Validation.For("home")/><br>@Html.ValidationMessage("home")</div></div>
    <div><label for="catd">Content:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="content" @Validation.For("content")>@content</textarea><br>@Html.ValidationMessage("content")</div></div>
    <div><label for="catd">Meta Description:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="metad" @Validation.For("metad")>@metad</textarea><br>@Html.ValidationMessage("metad")</div></div>
    <div><label for="catd">Meta Kewords:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="metak" @Validation.For("metak")>@metak</textarea><br>@Html.ValidationMessage("metak")</div></div>
    <div><button type="submit">Submit</button></div>
    </fieldset>
    </form>

    Thursday, April 7, 2016 10:41 AM

Answers

  • User379720387 posted

    Is there an error message?

    What is it supposed to do and what is it doing?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 11:43 AM
  • User-64994166 posted

    Cannot implicitly convert type 'string' to 'int'

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 11:54 AM
  • User379720387 posted

    Which line?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 12:39 PM
  • User-64994166 posted
    <a href="~/manage/pages/edit-page?id=@item.ContentId">Edit</a>)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 12:56 PM
  • User-1598917946 posted

    Hi every one and many many thanks to all of you for helping and learning each other.

    here i've also a question and i am unable to resolve this.  actually i dont know what the issue with the code that i cant update my database.

    Here is my code. please have a look.

      WebSecurity.RequireAuthenticatedUser();
        Layout = "~/Admin/_AdminLayout.cshtml";
        Page.Title = "Edit Products";
    
    var id=0;
    var title = "";
        var url = "";
        var home = "";
        var metad ="";
        var metak = "";
        var content = "";
        var Prid ="";
        //var contentId = UrlData[0].AsInt();
    if(!IsPost){
            if(!Request.QueryString["PageId"].IsEmpty()){
                Prid = Request.QueryString["PageId"];
                var dbd = Database.Open("gee1");
                var dbCommand = "SELECT Pages.PageId, Title, Url, Content, IsHomePage, MetaDescription, MetaKeywords FROM Pages INNER JOIN PagesContent ON Pages.PageId = PagesContent.PageId  WHERE ContentId  = @0";
                var row = dbd.QuerySingle(dbCommand, Prid);
                title = row.Title;
                url = row.Url;
                content = row.Content;
                metad = row.MetaDescription;
                metak = row.MetaKeywords;
            }
            else{
                Validation.AddFormError("No Product was selected.");
            }
        }
        if(IsPost){
              Validation.Add("title", Validator.Required("You must provide a Title"),Validator.StringLength(maxLength: 150, minLength: 5,
                         errorMessage: "You must provide at least 6 characters Title"));
        Validation.Add("url", Validator.Required("You must provide a Page url for ur page") , Validator.StringLength(150));
        Validation.Add("metad",Validator.Required("You must provide a lil Meta description for ur page ") , Validator.StringLength(250));
        Validation.Add("metak",Validator.Required("You must provide a lil Meta keywords for ur page ") , Validator.StringLength(1000));
        Validation.Add("content",Validator.Required("You must provide a lil content for ur page ") , Validator.StringLength(2500));
            title = Request.Form["title"];
            url = Request.Form["url"];
            content = Request.Unvalidated["content"];
            metad = Request.Unvalidated["metad"];
            metak = Request.Unvalidated["metak"];
            Prid= Request.Form["Prid"];
            //contentId = Request.Form["contentId"];
            if(Validation.IsValid()){
                var dbu = Database.Open("gee1");
                var updateCommand = "UPDATE Pages SET Title=@0,Url=@1,MetaDescription=@2,MetaKeywords=@3 WHERE PageId=@4";
                dbu.Execute(updateCommand,title,url,metad,metak, Prid);
                //if(IsPost){
                //updateCommand = "UPDATE PagesContent SET Content=@0 WHERE ContentId=@1";
                //dbu.Execute(updateCommand,content,contentId);
               Response.Redirect("~/Admin/manage/pages/managepage");
    //}
                
           }
        }
    
    }
    
       
    <section class="pdl-adm">
        <p>Click <a href="~/Admin/manage/products">HERE</a> to go to main</p>
      <form id="tetimonials" method="post" action="">
    <fieldset>@Html.ValidationSummary("Errors with your submission:")
    <legend>Add New Page</legend>
        
    <div><label for="catn">Title:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input id="cn"  type="text"value="@title" name="title" @Validation.For("title") /><br>@Html.ValidationMessage("title")</div></div>
    <div><label for="catn">Page Url:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input style="text-transform: lowercase" id="cau1"  type="text" value="@url" name="url" @Validation.For("url") /><br>@Html.ValidationMessage("url")</div></div>
    <div><label for="catn">Is this home page?:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input value="False"  type="text"  name="home" @Validation.For("home")/><br>@Html.ValidationMessage("home")</div></div>
    <div><label for="catd">Content:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="content" @Validation.For("content")>@content</textarea><br>@Html.ValidationMessage("content")</div></div>
    <div><label for="catd">Meta Description:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="metad" @Validation.For("metad")>@metad</textarea><br>@Html.ValidationMessage("metad")</div></div>
    <div><label for="catd">Meta Kewords:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="metak" @Validation.For("metak")>@metak</textarea><br>@Html.ValidationMessage("metak")</div></div>
    <div><button type="submit">Submit</button></div>
    </fieldset>
    </form>

    Change the below

     dbu.Execute(updateCommand,title,url,metad,metak, Prid);

    to

     dbu.Execute(updateCommand,title,url,metad,metak, int.Parse(Prid));

    I guess your parameter type is wrong
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:02 PM
  • User-64994166 posted

    but the issue is now  

    • No Product was selected.

    as i mentioned in the code 

    else{
                Validation.AddFormError("No Product was selected.");
            }

    means there is form isn't filled  as it should be to update data 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:16 PM
  • User-1598917946 posted

    That means your query string is coming in empty

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:18 PM
  • User-64994166 posted

    yes you are right,

    string is emty

    but i changed 

    if(!Request.QueryString["PageId"].IsEmpty()){
                Prid = Request.QueryString["PageId"];

    to

    if(!Request.QueryString["Id"].IsEmpty()){
                Prid = Request.QueryString["Id"];

    form is filled as it should be 

    but after execution i getting error again  som thing like this:

    Server Error in '/' Application.


    Value cannot be null.
    Parameter name: String

    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.ArgumentNullException: Value cannot be null.
    Parameter name: String

    Source Error: 

    Line 46:             var dbu = Database.Open("gee1");
    Line 47:             var updateCommand = "UPDATE Pages SET Title=@0,Url=@1,MetaDescription=@2,MetaKeywords=@3 WHERE PageId=@4";
    Line 48:             dbu.Execute(updateCommand,title,url,metad,metak, int.Parse(Prid));
    Line 49:             //if(IsPost){
    Line 50:             //updateCommand = "UPDATE PagesContent SET Content=@0 WHERE ContentId=@1";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:27 PM

All replies

  • User379720387 posted

    Is there an error message?

    What is it supposed to do and what is it doing?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 11:43 AM
  • User-64994166 posted

    Cannot implicitly convert type 'string' to 'int'

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 11:54 AM
  • User379720387 posted

    Which line?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 12:39 PM
  • User-64994166 posted
    <a href="~/manage/pages/edit-page?id=@item.ContentId">Edit</a>)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 12:56 PM
  • User-1598917946 posted

    Hi every one and many many thanks to all of you for helping and learning each other.

    here i've also a question and i am unable to resolve this.  actually i dont know what the issue with the code that i cant update my database.

    Here is my code. please have a look.

      WebSecurity.RequireAuthenticatedUser();
        Layout = "~/Admin/_AdminLayout.cshtml";
        Page.Title = "Edit Products";
    
    var id=0;
    var title = "";
        var url = "";
        var home = "";
        var metad ="";
        var metak = "";
        var content = "";
        var Prid ="";
        //var contentId = UrlData[0].AsInt();
    if(!IsPost){
            if(!Request.QueryString["PageId"].IsEmpty()){
                Prid = Request.QueryString["PageId"];
                var dbd = Database.Open("gee1");
                var dbCommand = "SELECT Pages.PageId, Title, Url, Content, IsHomePage, MetaDescription, MetaKeywords FROM Pages INNER JOIN PagesContent ON Pages.PageId = PagesContent.PageId  WHERE ContentId  = @0";
                var row = dbd.QuerySingle(dbCommand, Prid);
                title = row.Title;
                url = row.Url;
                content = row.Content;
                metad = row.MetaDescription;
                metak = row.MetaKeywords;
            }
            else{
                Validation.AddFormError("No Product was selected.");
            }
        }
        if(IsPost){
              Validation.Add("title", Validator.Required("You must provide a Title"),Validator.StringLength(maxLength: 150, minLength: 5,
                         errorMessage: "You must provide at least 6 characters Title"));
        Validation.Add("url", Validator.Required("You must provide a Page url for ur page") , Validator.StringLength(150));
        Validation.Add("metad",Validator.Required("You must provide a lil Meta description for ur page ") , Validator.StringLength(250));
        Validation.Add("metak",Validator.Required("You must provide a lil Meta keywords for ur page ") , Validator.StringLength(1000));
        Validation.Add("content",Validator.Required("You must provide a lil content for ur page ") , Validator.StringLength(2500));
            title = Request.Form["title"];
            url = Request.Form["url"];
            content = Request.Unvalidated["content"];
            metad = Request.Unvalidated["metad"];
            metak = Request.Unvalidated["metak"];
            Prid= Request.Form["Prid"];
            //contentId = Request.Form["contentId"];
            if(Validation.IsValid()){
                var dbu = Database.Open("gee1");
                var updateCommand = "UPDATE Pages SET Title=@0,Url=@1,MetaDescription=@2,MetaKeywords=@3 WHERE PageId=@4";
                dbu.Execute(updateCommand,title,url,metad,metak, Prid);
                //if(IsPost){
                //updateCommand = "UPDATE PagesContent SET Content=@0 WHERE ContentId=@1";
                //dbu.Execute(updateCommand,content,contentId);
               Response.Redirect("~/Admin/manage/pages/managepage");
    //}
                
           }
        }
    
    }
    
       
    <section class="pdl-adm">
        <p>Click <a href="~/Admin/manage/products">HERE</a> to go to main</p>
      <form id="tetimonials" method="post" action="">
    <fieldset>@Html.ValidationSummary("Errors with your submission:")
    <legend>Add New Page</legend>
        
    <div><label for="catn">Title:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input id="cn"  type="text"value="@title" name="title" @Validation.For("title") /><br>@Html.ValidationMessage("title")</div></div>
    <div><label for="catn">Page Url:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input style="text-transform: lowercase" id="cau1"  type="text" value="@url" name="url" @Validation.For("url") /><br>@Html.ValidationMessage("url")</div></div>
    <div><label for="catn">Is this home page?:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <input value="False"  type="text"  name="home" @Validation.For("home")/><br>@Html.ValidationMessage("home")</div></div>
    <div><label for="catd">Content:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="content" @Validation.For("content")>@content</textarea><br>@Html.ValidationMessage("content")</div></div>
    <div><label for="catd">Meta Description:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="metad" @Validation.For("metad")>@metad</textarea><br>@Html.ValidationMessage("metad")</div></div>
    <div><label for="catd">Meta Kewords:</label>
        <div style="  background: #FFFFFF; float: left; width: 80%;">
            <textarea  name="metak" @Validation.For("metak")>@metak</textarea><br>@Html.ValidationMessage("metak")</div></div>
    <div><button type="submit">Submit</button></div>
    </fieldset>
    </form>

    Change the below

     dbu.Execute(updateCommand,title,url,metad,metak, Prid);

    to

     dbu.Execute(updateCommand,title,url,metad,metak, int.Parse(Prid));

    I guess your parameter type is wrong
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:02 PM
  • User-64994166 posted

    but the issue is now  

    • No Product was selected.

    as i mentioned in the code 

    else{
                Validation.AddFormError("No Product was selected.");
            }

    means there is form isn't filled  as it should be to update data 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:16 PM
  • User-1598917946 posted

    That means your query string is coming in empty

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:18 PM
  • User-64994166 posted

    yes you are right,

    string is emty

    but i changed 

    if(!Request.QueryString["PageId"].IsEmpty()){
                Prid = Request.QueryString["PageId"];

    to

    if(!Request.QueryString["Id"].IsEmpty()){
                Prid = Request.QueryString["Id"];

    form is filled as it should be 

    but after execution i getting error again  som thing like this:

    Server Error in '/' Application.


    Value cannot be null.
    Parameter name: String

    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.ArgumentNullException: Value cannot be null.
    Parameter name: String

    Source Error: 

    Line 46:             var dbu = Database.Open("gee1");
    Line 47:             var updateCommand = "UPDATE Pages SET Title=@0,Url=@1,MetaDescription=@2,MetaKeywords=@3 WHERE PageId=@4";
    Line 48:             dbu.Execute(updateCommand,title,url,metad,metak, int.Parse(Prid));
    Line 49:             //if(IsPost){
    Line 50:             //updateCommand = "UPDATE PagesContent SET Content=@0 WHERE ContentId=@1";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 1:27 PM