locked
500 error when inline updating RRS feed

  • Question

  • User-2051535749 posted

    My web app is using user controls and a master page. I have over 10 user controls with the gridview and all have an edit command field.  I just added a new user control and created a new data source for it. I used existing code for the HTML itself from an existing one.  When I click the edit button on the row, it takes me to edit mode, I then click the save button and I get the: even though the database is updated.

    Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

    however, when I click the cancel button, the grid refreshes and I can see the updated data. I checked everything in the HTML, code behind, etc. what would cause this user control to get this error when clicking save, but it works when clicking the cancel and the data is being updated.

    One thing I did try is if I show all columns in the grid, I can inline update with no issues, however, I only want 3 of the data columns shown in the grid. I don't want to show last updated by or last updated columns, if these are visible it works ,if they are not in the grid, I get the error above. With them not visible in the grid, I can still see the values when I step through the update code

    At a loss on this one

    Tuesday, May 21, 2019 2:33 PM

All replies

  • User475983607 posted

    We can only assume without code that reproduces the actual exception.  My assumption is the visibility property to false which stops the inline inputs from being sent to the browser.  The missing input values causes the exception.  

    Have you tried setting a break point and single stepping through the code?  Perhaps add a try...catch block to catch the error.

    Tuesday, May 21, 2019 3:13 PM
  • User-2051535749 posted

    I have 10 other grids within the app that only show a few data values and lastmodiedby isn't a value being shown to the user via the GUI and all work as expected:

    Yes, I set a break point and the data is being updated but the page/grid isn't refreshing, however, if I click cancel, it refreshes the grid and shows the updated data. 

    this is the code for the grid for the usercontrol I'm having the issue with, this code is also used within other user controls and works as expected

    <asp:GridView ID="GridView" runat="server" CssClass="Grid"
     AutoGenerateColumns="False" DataSourceID="GridViewDataSource" DataKeyNames="id"
        GridLines="None" >
        <Columns>
             <asp:CommandField HeaderText="Edit" ButtonType="Image" CancelImageUrl="~/images/undo.gif" EditImageUrl="~/images/edit.gif" ShowEditButton="True" UpdateImageUrl="~/images/save.gif">
                <ItemStyle CssClass="GridViewEdit" />
            </asp:CommandField>
            <asp:BoundField DataField="Id" HeaderText="Id"  />
            <asp:BoundField DataField="Name" HeaderText="Name"  />
             <asp:TemplateField HeaderText="Description" />
                 <ItemTemplate>
                    <%# Eval("Description") %>
                 </ItemTemplate>
                 <EditItemTemplate>
                     <asp:TextBox ID="EditDescription" runat="server" TextMode="MultiLine" Rows="5" CssClass="TextArea" Text='<%# Bind("Description") %>'></asp:TextBox>
                 </EditItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:ImageButton ID="Delete" runat="server" AlternateText="Delete" CommandName="Delete" ImageUrl="~/images/delete.gif" OnClientClick="return confirm('Are you sure you want to delete this row?);" />
                </ItemTemplate>
            </asp:TemplateField>  
        </Columns>
         <EmptyDataTemplate>No codes have been retrieved.</EmptyDataTemplate>
            <HeaderStyle CssClass="GridViewHeader" />
    </asp:GridView>
    
    <asp:ObjectDataSource ID="GridViewDataSource" runat="server" DataObjectTypeName="" 
        DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
        TypeName="" UpdateMethod="Update">
    <UpdateParameters>
    <asp:FormParameter Name="Id" />
    </UpdateParameters>
    </asp:ObjectDataSource>

    Tuesday, May 21, 2019 3:57 PM
  • User475983607 posted

    The community needs enough code to reproduce the 500 error.  Otherwise; there's not much we can do but guess which is not productive.  

    Is there anyway you can provide sample code that reproduces this issue?  Or the methods that fetch or update along with the event handler?  Have you tried wrapping code in a try catch so you are not throwing a 500 error to the UI?

    When you set the break point, which line of code caused the exception?

    Tuesday, May 21, 2019 5:03 PM
  • User-2051535749 posted

    That's the code, it's the grid with the data source.  I dragged and dropped the gridview to the page, the object data source to the page connected the 2 and removed columns that don't need to be viewed in the GUI

    Tuesday, May 21, 2019 5:11 PM
  • User665608656 posted

    Hi IGotYourDotNet,

    According to the code you provided, I can't reproduce the question you described.

    <asp:ObjectDataSource ID="GridViewDataSource" runat="server" DataObjectTypeName="" 
        DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
        TypeName="" UpdateMethod="Update">
    <UpdateParameters>
                <asp:FormParameter Name="Id" />
        </UpdateParameters>
    </asp:ObjectDataSource>

    The TypeName = "" in this statement, how do you bind GridView to data? 

    Could you please provide these two methods  SelectMethod = "GetData"  and UpdateMethod = "Update" in code behind ?

    And I want to know what your gridview's outer user controls look like.

    I hope you can provide more detailed front-end code and code behind.

    If you could post more details information, it will be more easily for us to reproduce the issue and find out the solution.

     

    Best Regards,

    YongQing.

    Wednesday, May 22, 2019 10:16 AM
  • User-2051535749 posted

    The GetData is pulling the data and the update is well, updating via SQL.

    Again, if I show the LastUpdated and LastUpdatedBy in the grid, it works 100%, If I remove them from the grid, I get the 500 error. I stepped through the code and the values are being passed in both scenarios.

    Wednesday, May 22, 2019 11:55 AM
  • User753101303 posted

    Hi,

    The server side exception message should be written to the Windows event log. It could help to better understand what happens. Your intent is to provide values programmatically rather than from the UI ?

    Wednesday, May 22, 2019 12:06 PM
  • User-2051535749 posted

    The EV shows this: 

    Exception information:
    Exception type: TargetInvocationException
    Exception message: Exception has been thrown by the target of an invocation.

    Request information:
    Request URL: http://localhost:49573/admin.aspx
    Request path: /admin.aspx
    User host address: ::1
    User:
    Is authenticated: False
    Authentication Type:


    Thread information:
    Thread ID: 11

    Is impersonating: False
    Stack trace: at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
    at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance)
    at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method)
    at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
    at System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback)
    at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
    at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
    at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
    at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
    at System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e)
    at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    and the update logic is this (which is the same as the other 10 working grids/usercontrols within the app, only the table and columns change), and as I mentioned, if I click cancel, the data is updated and the grid refreshes and this works IF the lastupdated and lastupdatedby are visible in the grid in the gui, I can't even set them to visible=false, I get the 500 error with that as well.

    And [rows] returns 1, so a row is being returned in the update, this is on a user control being used within a master page and the app has 10 other usercontrols using the same code and all work just fine (different tables obviously)

     public static int Update(CarSales item)
            {
                int rows = 0;
                using (SqlConnection myConnection = new SqlConnection(Connections.ConnectionString))
                {
                    string sql = "";
                    sql += "UPDATE [table1] SET ";
                    sql += "Name = @Name, Description = @Description, LastUpdated = @LastUpdated, LastUpdatedBy = @LastUpdatedBy ";
                    sql += "WHERE CategoryId = @Id";
                    SqlCommand myCommand = new SqlCommand(sql, myConnection);
                    myCommand.CommandType = CommandType.Text;
    
                    myCommand.Parameters.AddWithValue("@Id", item.Id);
                    myCommand.Parameters.AddWithValue("@Name", item.Name);
                    myCommand.Parameters.AddWithValue("@Description", item.Description);
                    myCommand.Parameters.AddWithValue("@LastUpdated", item.LastUpdated);
                    myCommand.Parameters.AddWithValue("@LastUpdatedBy",item.LastUpdatedBy);
    
                    myConnection.Open();
                    rows = Convert.ToInt32(myCommand.ExecuteNonQuery());
                    myConnection.Close();
                }
                return rows;
            }

    Wednesday, May 22, 2019 12:16 PM
  • User475983607 posted

    if I click cancel, the data is updated and the grid refreshes and this works IF the lastupdated and lastupdatedby are visible in the grid in the gui, I can't even set them to visible=false, I get the 500 error with that as well.

    As stated above, the framework does not render the HTML when the Visible property is set to false.  How are you populating the CarSales argument? 

    Try setting a break point in the Update method and verify the LastUpdated and LastUpdatedBy properties are populated.

    Wednesday, May 22, 2019 1:16 PM
  • User-2051535749 posted

    I did a work around for now by just adding those 2 columns and hiding the header and making the font white so it wouldn't appear in the grid. As stated before the columns are being populated in when updating, however, it kicks out the 500 error.

    I'm done with it for now. I'll loop back to it at a later time. 

    Wednesday, May 22, 2019 1:49 PM
  • User753101303 posted

    A bit weird it shows a problem when calling Update but doesn't show which problem happened. Would have to check that.

    My guess is that when you don't include this column, item.LastUpdated is populated with the default date  (1/1/0001) which is out of range for SQL Server causing then an exception.

    A quick fix could be to assign yourself a value such as DateTime.Now or UtcNow  to replace the default 1//1/1 value.

    Wednesday, May 22, 2019 2:05 PM
  • User-2051535749 posted

    A bit weird it shows a problem when calling Update but doesn't show which problem happened. Would have to check that.

    My guess is that when you don't include this column, item.LastUpdated is populated with the default date  (1/1/0001) which is out of range for SQL Server causing then an exception.

    A quick fix could be to assign yourself a value such as DateTime.Now or UtcNow  to replace the default 1//1/1 value.

    I know, and if I click the "cancel" when updating, it works, data is updated, grid refreshes. In the update code  I have it as:

    this is called when the in the onRowUpdated method within the gridview. When I step through it, I can see today's date and the user id

    protected void RowUpdated(object sender, GridViewUpdatedEventArgs e)
            {
                int id = Convert.ToInt32(e.Keys["Id"]);
                CarSales.SaleCategories item = new  CarSales.SaleCategories();
                item.Id = id;
                item.Name = e.NewValues["Name"].ToString();
                item.Description = e.NewValues["Description"].ToString();
                item.LastUpdated = DateTime.Now;
                item.LastUpdatedBy = Environment.UserName;
                CarSales.SaleCategories.Update(item);
            }

    Wednesday, May 22, 2019 6:32 PM
  • User665608656 posted

    Hi IGotYourDotNet,

    According to the code and description you provided , I still have no way to reproduce your question.

    Could you please provide all the detailed code for your function? This will help us solve the issue more easierly.


    Best Regards,

    YongQing.

    Friday, May 24, 2019 7:55 AM