Article: Step by Step Tutorial for performing Insert Edit Update and Delete in GridView in ASP.Net
Delete and update not working
Delete function is properly working when I am not using OnRowDataBound but it is important for javascript pop in delete
When I am clicking Update button GridView1_RowUpdating function is not getting please help I am new Asp.net
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#1f2d41" HeaderStyle-ForeColor="White" CssClass="table table-striped table-bordered shadow-lg text-center" DataKeyNames="id"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
OnPageIndexChanging="GridView1_PageIndexChanging" EnableViewState="true" EmptyDataText="No records has been added." AllowPaging="true">
<Columns>
<asp:TemplateField HeaderText="S. No.">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Container.DataItemIndex + 1 %>' Font-Bold="True"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Leave Type" SortExpression="leave_type">
<EditItemTemplate>
<asp:TextBox ID="txtLeaveType" runat="server" Text='<%# Bind("leave_type") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLeaveType" runat="server" Text='<%# Bind("leave_type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Leave Code" SortExpression="leave_code">
<EditItemTemplate>
<asp:TextBox ID="txtLeaveCode" runat="server" Text='<%# Bind("leave_code") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLeaveCode" runat="server" Text='<%# Bind("leave_code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" HeaderText="Modify" ShowEditButton="True" ShowDeleteButton="True">
<ControlStyle CssClass="btn btn-danger" />
</asp:CommandField>
</Columns>
<HeaderStyle BackColor="#1F2D41" ForeColor="White"></HeaderStyle>
</asp:GridView>
Private Sub BindGridView()
Try
Dim connectionString As String = ConfigurationManager.ConnectionStrings("dbcon").ConnectionString
Using connection As New SqlConnection(connectionString)
Dim query As String = "SELECT [id], [leave_type], [leave_code] FROM [leavetable]"
Using command As New SqlCommand(query, connection)
Dim adapter As New SqlDataAdapter(command)
Dim dataTable As New DataTable()
adapter.Fill(dataTable)
GridView1.DataSource = dataTable
GridView1.DataBind()
End Using
End Using
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
Try
GridView1.EditIndex = e.NewEditIndex
BindGridView()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Try
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Values(0))
Dim leaveType As String = DirectCast(row.FindControl("txtLeaveType"), TextBox).Text
Dim leaveCode As String = DirectCast(row.FindControl("txtLeaveCode"), TextBox).Text
Dim connectionString As String = ConfigurationManager.ConnectionStrings("dbcon").ConnectionString
Dim query As String = "UPDATE [leavetable] SET [leave_type] = @leave_type, [leave_code] = @leave_code WHERE [id] = @original_id AND (([leave_type] = @original_leave_type) OR ([leave_type] IS NULL AND @original_leave_type IS NULL)) AND (([leave_code] = @original_leave_code) OR ([leave_code] IS NULL AND @original_leave_code IS NULL))"
'Dim query As String = "UPDATE leavetable SET leave_type = @leave_type, leave_code = @leave_code WHERE id = @id"
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@leave_type", leaveType)
command.Parameters.AddWithValue("@leave_code", leaveCode)
command.Parameters.AddWithValue("@original_id", id)
command.Parameters.AddWithValue("@original_leave_type", row.Cells(2).Text)
command.Parameters.AddWithValue("@original_leave_code", row.Cells(3).Text)
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
GridView1.EditIndex = -1
BindGridView()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
Try
GridView1.EditIndex = -1
BindGridView()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Try
GridView1.PageIndex = e.NewPageIndex
BindGridView()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Try
Dim rowIndex As Integer = e.RowIndex
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Values(0))
' Dim leaveType As String = (TryCast(row.FindControl("TextBox1"), TextBox)).Text
'Dim leaveCode As String = (TryCast(row.FindControl("TextBox2"), TextBox)).Text
Dim leaveType As String = GridView1.DataKeys(rowIndex).Value.ToString()
Dim leaveCode As String = GridView1.DataKeys(rowIndex).Value.ToString()
Dim connectionString As String = ConfigurationManager.ConnectionStrings("dbcon").ConnectionString
' Dim query As String = "DELETE FROM [leavetable] WHERE [id] = @id AND (([leave_type] = @leave_type) OR ([leave_type] IS NULL AND @leave_type IS NULL)) AND (([leave_code] = @leave_code) OR ([leave_code] IS NULL AND @leave_code IS NULL))"
Dim query As String = "DELETE FROM [leavetable] WHERE [id] = @id "
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@id", id)
' command.Parameters.AddWithValue("@leave_type", leaveType)
' command.Parameters.AddWithValue("@leave_code", leaveCode)
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
BindGridView()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowIndex <> GridView1.EditIndex Then
TryCast(e.Row.Cells(3).Controls(2), Button).Attributes("onclick") = "return confirm('Do you want to delete this row?');"
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub