Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        TextBox Name = (TextBox)row.FindControl("TestName");
        TextBox Dep = (TextBox)row.FindControl("Dept");
        TextBox Pric = (TextBox)row.FindControl("Price");
        TextBox Typ = (TextBox)row.FindControl("Type");
        TextBox Statu = (TextBox)row.FindControl("Status");
        GridView1.EditIndex = -1;
        con.Open();
        SqlCommand cmd = new SqlCommand("update Test set TestName=@TestName,Dept=@Dept,Price=@Price, Type=@Type Status=@Status where ID=@ID", con);
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
        cmd.Parameters.Add("@TestName", SqlDbType.VarChar).Value = Name.Text.Trim();
        cmd.Parameters.Add("@Dept", SqlDbType.VarChar).Value = Dep.Text.Trim();
        cmd.Parameters.Add("@Price", SqlDbType.VarChar).Value = Pric.Text.Trim();
        cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = Typ.Text.Trim();
        cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = Statu.Text.Trim();
        cmd.ExecuteNonQuery();
        con.Close();
        Foo();
    }

ASP.NET
<pre lang="xml"><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
           OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
           onrowcommand="GridView1_RowCommand"
           onrowcancelingedit="GridView1_RowCancelingEdit"
           onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
           onrowupdating="GridView1_RowUpdating" DataKeyNames="TestID">
           <Columns>
               <asp:TemplateField HeaderText="ID">
                   <ItemTemplate>
                       <asp:Label ID="lblTestID" runat="server" Text='<%# Eval("TestID") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Test Name">
                   <ItemTemplate>
                       <asp:Label ID="lblTestName" runat="server" Text='<%# Eval("TestName") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Department">
                   <ItemTemplate>
                       <asp:Label ID="lblDept" runat="server" Text='<%# Eval("Dept") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Price">
                   <ItemTemplate>
                       <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Type">
                   <ItemTemplate>
                       <asp:Label ID="lblType" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Status">
                   <ItemTemplate>
                       <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
                <asp:TemplateField>
                   <ItemTemplate>
                       <asp:LinkButton ID="lbtnItems" runat="server" CommandName="Items" Text="Items"></asp:LinkButton>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:CommandField ShowEditButton="true" />
               <asp:CommandField ShowDeleteButton="true" />
           </Columns>
       </asp:GridView>
Posted
Comments
Deepu S Nair 18-Feb-15 7:53am    
what error you got?
vickycode4 18-Feb-15 7:54am    
System.NullReferenceException: Object reference not set to an instance of an object.
Deepu S Nair 18-Feb-15 7:57am    
Debug code and add Watch items to see which one is NULL.
King Fisher 18-Feb-15 7:57am    
what you mean not working properly?
ZurdoDev 18-Feb-15 7:57am    
Fix the null.

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