locked
updating gridrow RRS feed

  • Question

  • User-2132746437 posted

    I have a gridview in a panel, I have an update button in the row, I can click the button, make a change, save it, then I get the following error:

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

    Now, when I go into the database or refresh the page, I can see the change for that row. When I go into event viewer, I see a warning for ASP.NET 2.0

    I'm running on IIS Express in VS 2017. I have a dozen other pages within my web app that use gridview, objectdatasource, update and delete and work fine, this is the only page having the issue. Any thoughts?  Also, I have to add code for delete and update in the code behind for those to work, if I had those methods in the objectdatasource, nothing happens. 

    objectdatasource:

    <asp:ObjectDataSource ID="GridViewDataSource" runat="server" TypeName="CarDealers.New.BOL.Autos"
    SelectMethod="GetAutos" OldValuesParameterFormatString="original_{0}" DataObjectTypeName="CarDealers.New.BLL.AutosRules" UpdateMethod="Update" DeleteMethod="Delete" >

    </asp:ObjectDataSource>

    Event code: 3005 
    Event message: An unhandled exception has occurred. 
    Event time: 10/3/2018 12:03:20 PM 
    Event time (UTC): 10/3/2018 4:03:20 PM 
    Event ID: 28bf1acc138a462bbe6e35e078b241f8 
    Event sequence: 66 
    Event occurrence: 3 
    Event detail code: 0 
    
    Exception information: 
        Exception type: TargetInvocationException 
        Exception message: Exception has been thrown by the target of an invocation. 
    
    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)
     
     
    

    Wednesday, October 3, 2018 4:10 PM

Answers

  • User-2132746437 posted

    I got the update working, it had something to do with the master page and a the correct value not being captured

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2018 3:09 PM

All replies

  • User-893317190 posted

    Hi csharpcoder,

    The exception message is not specific enough. From the exception , we could only know that the error happens in line 5.

    Since you are running on iis express in vs 2017 , please debug your page to see the detailed information of your page.

    I also have made a test using the similar datasource  to yours, but it works well.

    Below is my code.

    My model.

      public partial class Fruit
        {
            [StringLength(50)]
            public string ItemName { get; set; }
    
            [StringLength(50)]
            public string BelongsTo { get; set; }
    
            public int Qunatity { get; set; }
    
            [Key]
            public int itemid { get; set; }
        }

    My  datasource class.

     public class MyObjectDataSource
        {
    
            private static string constr = ConfigurationManager.ConnectionStrings["supplyModel"].ConnectionString;
    
            public List<Fruit> getAll()
            {
                List<Fruit> list = new List<Fruit>();
                SqlConnection con = new SqlConnection(constr);
                using (SqlCommand com = new SqlCommand("select * from fruits", con))
                {
                   
                        con.Open();
    
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Fruit fruit = new Fruit();
                            fruit.itemid = reader.GetInt32(3);
                            fruit.ItemName = reader.GetString(0);
                            fruit.Qunatity = reader.GetInt32(2);
                            fruit.BelongsTo = reader.GetString(1);
                            list.Add(fruit);
                        }
                    }
                    return list;
    
                }
            }
    
            public int update(Fruit fruit)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand("update fruits set itemname=@name,qunatity=@qunatity,belongsTo=@to where itemid=@id", con))
                    {
                        com.Parameters.AddWithValue("name", fruit.ItemName);
                        com.Parameters.AddWithValue("qunatity", fruit.Qunatity);
                        com.Parameters.AddWithValue("to", fruit.BelongsTo);
                        com.Parameters.AddWithValue("id", fruit.itemid);
                        con.Open();
    
                        return com.ExecuteNonQuery();
                    }
                }
            }
    
            public int delete(Fruit fruit) {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand("delete from fruits where itemid=@id", con))
                    {
                        com.Parameters.AddWithValue("id",fruit.itemid);
                        con.Open();
                        return com.ExecuteNonQuery();
                    }
                }
            }
        }

    My aspx page.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" DataKeyNames="itemid"  >
                    <Columns>
                        <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />
                        <asp:BoundField DataField="BelongsTo" HeaderText="BelongsTo" SortExpression="BelongsTo" />
                        <asp:BoundField DataField="Qunatity" HeaderText="Qunatity" SortExpression="Qunatity" />
                        <asp:BoundField DataField="itemid" HeaderText="itemid" SortExpression="itemid" ReadOnly="true" />
                    </Columns>
                   
                </asp:GridView>
                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getAll" TypeName="MyWebFormCases.Utils.MyObjectDataSource"
                    DataObjectTypeName="MyWebFormCases.Models.Fruit" OldValuesParameterFormatString="original_{0}" UpdateMethod="update" DeleteMethod="delete"
                    >
                    <DeleteParameters>
                        <asp:Parameter Name="id" Type="Int32" />
                    </DeleteParameters>
                </asp:ObjectDataSource>

    Maybe the problem  is not with the code you have posted.

    Please post more detailed error information and more of your code.

    Best regards,

    Ackerly Xu

    Thursday, October 4, 2018 3:29 AM
  • User-2132746437 posted

    What I posted is what I'm getting as an error. After looking at your code example, it's not much different then ours. return a list, call an update stored procedure, etc. like I said, the data is getting updated, just getting the 500 error on the page and the every page in the app is doing the same thing and works fine (only against different tables), 

    Thursday, October 4, 2018 11:13 AM
  • User-2132746437 posted

    I got the update working, it had something to do with the master page and a the correct value not being captured

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2018 3:09 PM