Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing this code to update data in data base from gridviewupdat button but i am gettin some error.


C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
       
        TextBox txtname = (TextBox)row.FindControl("txtname");
        TextBox txtage = (TextBox)row.FindControl("txtage");


        int userid = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        string name = txtname.Text;
        decimal age = Decimal.Parse(txtage.Text);


        UpdateProduct(userid, name, age);
    }


-----------------
Error is :

SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Posted
Updated 5-Jan-14 3:35am
v2
Comments
Alok.singh1166 5-Jan-14 9:54am    
still i am getting same error ( actuly it is showing row index value =0
David Dekanozishvili 5-Jan-14 10:30am    
Ok, solution provided was not the solution when I checked, I got it wrong sorry. First problem was DataKeyNames property was not set. Now Post the markup correctly please and I will analyze. :)
P.S. Added new solution...
Alok.singh1166 5-Jan-14 11:12am    
hi David Please check solution 4 for markup

and do the needfull.
David Dekanozishvili 5-Jan-14 11:18am    
Check Solution 3 and tell me if the error occurs again...
Alok.singh1166 5-Jan-14 11:33am    
for solution 3 this error is coming

in this code for var age = e.newValues["age"] as decimal .Error is coming like the as operator must be use with reference type or null type decimal is not null type

Start by checking the value of RowIndex at the start of the method:
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
   if (e.RowIndex >= 0 && e.RowIndex < GridView1 > Rows.Count)
      {
      GridViewRow row = GridView1.Rows[e.RowIndex];
      ...
      }
   }
Certainly with other events, you can get a negative value at the start to indicate "no row" - it may be the same with this event.
 
Share this answer
 
Set DataKeyNames property to "userid", use bind on textboxes and rewrite code like this.

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    var name = e.NewValues["name"] as string;
    var age = e.NewValues["age"] as string;

    var userid = (string)e.Keys[0];

    UpdateProduct(userid, name, age);
}


XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowcancelingEdit" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="userid" OnRowEditing="GridView1_RowEditing" >
           <Columns>
               <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="true" />
               <asp:TemplateField HeaderText="name">
               <ItemTemplate>
           <%# Eval("name")%>
         </ItemTemplate>
         <EditItemTemplate>
           <asp:TextBox runat="server" ID="txtname" Text='<%# Bind("name")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="age">
               <ItemTemplate>
             <%# Eval("age")%>
         </ItemTemplate>
         <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtage" Text='<%# Bind("age")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:CommandField ShowEditButton="True" ButtonType="Button" />
           </Columns>
       </asp:GridView>
 
Share this answer
 
v11
Comments
Alok.singh1166 5-Jan-14 11:18am    
in this code for var age = e.newValues["age"] as decimal .Error is coming like the as operator must be use with reference type or null type decimal is not null type
David Dekanozishvili 5-Jan-14 11:19am    
fixed. :)
Alok.singh1166 5-Jan-14 12:03pm    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var name = e.NewValues["name"] as string;
string age = (string)e.NewValues["age"];

string userid = (string)e.Keys[0];

UpdateProduct(userid, name, age);
}


This is working fine Thank you so much
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowcancelingEdit" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="userid" OnRowEditing="GridView1_RowEditing" >
           <Columns>
               <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="true" />
               <asp:TemplateField HeaderText="name">
               <ItemTemplate>
           <%# Eval("name")%>
         </ItemTemplate>
         <EditItemTemplate>
           <asp:TextBox runat="server" ID="txtname" Text='<%# Eval("name")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="age">
               <ItemTemplate>
             <%# Eval("age")%>
         </ItemTemplate>
         <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtage" Text='<%# Eval("age")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:CommandField ShowEditButton="True" ButtonType="Button" />
           </Columns>
       </asp:GridView>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900