protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e) { SqlConnection con = new SqlConnection("Data Source=VAIO-VAIO;Initial Catalog=CMS;Integrated Security=True"); string name = GridView2.DataKeys[e.RowIndex].Values["Name"].ToString(); TextBox txtName = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtName"); string size = GridView2.DataKeys[e.RowIndex].Values["Size"].ToString(); TextBox txtSize = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtSize"); con.Open(); SqlCommand cmd = new SqlCommand("update Tab2 set Name='" + txtName + "' and Size= '" + txtSize + "' ", con); cmd.ExecuteNonQuery(); con.Close(); //lblresult.ForeColor = Color.Green; //lblresult.Text = sitename + " Details Updated successfully"; GridView2.EditIndex = -1; GridView2.DataBind(); }
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Width="1132px" AllowPaging="True" onrowediting="EditCustomer" onrowupdating="UpdateCustomer" onrowcancelingedit="CancelEdit" DataKeyNames="name, size" > <PagerStyle ForeColor="Black" HorizontalAlign="Center" BackColor="#C6C3C6"></PagerStyle> <Columns> <%-- <asp:TemplateField HeaderText="Stockcat"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> <asp:ListItem Value="des" Text="des" /> <asp:ListItem Value="bms" Text="bms" /> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> --%> <asp:BoundField DataField="Stockcat" HeaderText="Stockcat" SortExpression="Stockcat" /> <asp:BoundField DataField="Stockid" HeaderText="Stockid" SortExpression="Stockid" /> <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" /> <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" /> <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Name"> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Size"> <ItemTemplate> <asp:Label ID="lblSize" runat="server" Text='<%# Eval("Size")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtSize" runat="server" Text='<%# Eval("Size")%>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtSize" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <%-- <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Size" HeaderText="Size" SortExpression="Size" /> --%> <asp:CommandField HeaderText="Update" ShowEditButton="True" /> </Columns> </asp:GridView>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)