Hi, I have try to insert edit and delete feature in my gridview app. however, the code is not really work. when I click the edit or delete button it keep adding the blank line. It supposed show textbox with data and Update Cancel link button, when I click the edit button.

here my html code

 <form id="form1" runat="server">
        <asp:GridView ID="GridView1" DataKeyNames="ModuleID" runat="server" AutoGenerateColumns="False"
            ShowFooter="True" OnRowCommand="Add" onrowcancelingedit="GridView1_RowCancelling"  onrowdeleting="GridView1_RowDeleting" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
            <HeaderStyle BackColor="#9a9a9a" ForeColor="White" Font-Bold="true" Height="30" />
            <AlternatingRowStyle BackColor="#f5f5f5" />
            <Columns>
            <asp:TemplateField HeaderText="Module Name">
            <ItemTemplate>
                <%# Eval("ModuleName") %>
            </ItemTemplate>

            <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtModuleName" Text='<%# Eval("ModuleName") %>' />
             <asp:RequiredFieldValidator runat="server" ID="rfdModuleName"                 
             ControlToValidate="txtModuleName" ValidationGroup="ModuleName" ErrorMessage="*" />
            </EditItemTemplate>
            <asp:TemplateField HeaderText="Sub Module">
            <ItemTemplate>
                <%# Eval("SubModule") %>
            </ItemTemplate>

            <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtSubModule" Text='<%# Eval("SubModule") %>' />
             <asp:RequiredFieldValidator runat="server" ID="rfdSubModule"                 
             ControlToValidate="txtSubModule" ValidationGroup="SubModule" ErrorMessage="*" />
            </EditItemTemplate>
            <asp:TemplateField HeaderText="Add">
            <ItemTemplate>
                <%# Eval("TagAdd") %>
            </ItemTemplate>

             <EditItemTemplate>
             <asp:DropDownList runat="server" ID="ddlTagAdd" Text='<%# Eval("TagAdd") %>' />
             <asp:RequiredFieldValidator runat="server" ID="rfdTagAdd"                 
             ControlToValidate="ddlTagAdd" ValidationGroup="TagAdd" ErrorMessage="*" />
            </EditItemTemplate>

            <asp:TemplateField>
            <ItemTemplate>
             <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
             <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
            </ItemTemplate>



      <EditItemTemplate>
      <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
      <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
      </EditItemTemplate>

and here is code behind

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)



        GridView1.EditIndex = e.NewEditIndex

        Me.BindData()


    End Sub

    Protected Sub GridView1_RowCancelling(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)

        GridView1.EditIndex = -1

        Me.BindData()

    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

        Dim ModuleID As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("lblModuleID")), Label).Text
        Dim ModuleName As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtModuleName")), TextBox).Text
        Dim SubModule As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtSubModule")), TextBox).Text
        Dim TagAdd As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("ddlTagAdd")), DropDownList).SelectedValue.ToString
        Dim TagEdit As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("ddlTagEdit")), DropDownList).SelectedValue.ToString
        Dim TagDelete As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("ddlTagDelete")), DropDownList).SelectedValue.ToString

        Dim strConnString As String = ConfigurationManager.ConnectionStrings("AMS").ConnectionString
        Using con As SqlConnection = New SqlConnection(strConnString)
            Using cmd As SqlCommand = New SqlCommand
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "UPDATE ModuleDetail SET ModuleName = @ModuleName, SubModule=@SubModule, TagAdd = @TagAdd, TagEdit = @TagEdit, TagDelete = @TagDelete where ModuleID=@ModuleID  SELECT ModuleName, SubModule, TagAdd, TagEdit, TagDelete FROM ModuleDetail"

                cmd.Parameters.AddWithValue("@ModuleName", ModuleName)
                cmd.Parameters.AddWithValue("@SubModule", SubModule)
                cmd.Parameters.AddWithValue("@TagAdd", TagAdd.ToString)
                cmd.Parameters.AddWithValue("@TagEdit", TagEdit.ToString)
                cmd.Parameters.AddWithValue("@TagDelete", TagDelete.ToString)

                GridView1.EditIndex = -1

                GridView1.DataSource = cmd

                GridView1.DataBind()
            End Using
        End Using
    End Sub


    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As EventArgs)

        Dim lnkRemove As LinkButton = DirectCast(sender, LinkButton)
        Dim ModuleName As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtModuleName")), TextBox).Text
        Dim SubModule As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtSubModule")), TextBox).Text
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("AMS").ConnectionString
        Using con As SqlConnection = New SqlConnection(strConnString)
            Using cmd As SqlCommand = New SqlCommand

                cmd.CommandType = CommandType.Text

                cmd.CommandText = "delete from ModuleDetail where ModuleName=@ModuleName, SubModule=@SubModule  SELECT ModuleName, SubModule, TagAdd, TagEdit, TagDelete FROM ModuleDetail"

                cmd.Parameters.AddWithValue("@ModuleName", ModuleName)
                cmd.Parameters.AddWithValue("@SubModule", SubModule)


                GridView1.DataSource = cmd

                GridView1.DataBind()
            End Using
        End Using
    End Sub

the issues is it show logic error,which it add the blank line everytime I click edit or delete button.

Do not find the execution of command of the command object in both editing and deleting procedure.
After asigning parameters you will have to use cmd.ExecuteNonQuery() method to update your datatable.
And also you will have to use a semicolon ; to run multiple SQL query by a single command object one by one.cmd.CommandText = "UPDATE ModuleDetail SET ModuleName = @ModuleName, SubModule=@SubModule, TagAdd = @TagAdd, TagEdit = @TagEdit, TagDelete = @TagDelete where ModuleID=@ModuleID ; SELECT ModuleName, SubModule, TagAdd, TagEdit, TagDelete FROM ModuleDetail" and cmd.CommandText = "delete from ModuleDetail where ModuleName=@ModuleName, SubModule=@SubModule ; SELECT ModuleName, SubModule, TagAdd, TagEdit, TagDelete FROM ModuleDetail".

hope it can help you to solve your problem.

hi satyameba jayate, I have put the semicolon within 2 SQL query and put the cmd.ExecuteNonQuery() after paramenter assigning, it still shows the same result

Hi!

In both command text you used two types of SQL Statements, First one is DML(Data Manipulating Language) and Second One is DQL(Data Query Language).
For DML you must use ExecuteNonQuery() method to update your table but for DQL you have to read the table through a DataReader or load the table to a data Table varable and then asign it as datasource to the datagridview. Before to do it set DataGridView's DataSource property to Nothing.

Thanx.

Hi, we got same problem but then found no exact answer and so I switched to listview instead of datagridview. I will just show you how I programmed my edit button for my listview data after clicking specific data to be edited, and show it on my textboxes in other form.
Just hoping you will get some idea though...

Private Sub edit_Click(sender As Object, e As EventArgs) Handles edit.Click
        editForm.Show()
        If Not Listview1.SelectedItems.Count = 0 Then
            With Listview1.SelectedItems.Item(0)
                editForm.x.Text = .Text
                editForm.TextBox1.Text = .SubItems(1).Text
                editForm.TextBox2.Text = .SubItems(2).Text
                editForm.TextBox3.Text = .SubItems(3).Text
                editForm.TextBox4.Text = .SubItems(4).Text
                editForm.TextBox5.Text = .SubItems(5).Text
                editForm.DateTimePicker2.Text = .SubItems(6).Text
                editForm.TextBox6.Text = .SubItems(7).Text
                editForm.TextBox7.Text = .SubItems(8).Text
                editForm.DateTimePicker3.Text = .SubItems(9).Text
                editForm.DateTimePicker4.Text = .SubItems(10).Text
                editForm.y.Text = .SubItems(11).Text
                editForm.TextBox8.Text = .SubItems(12).Text
                editForm.TextBox9.Text = .SubItems(13).Text
                editForm.TextBox10.Text = .SubItems(14).Text
            End With
        End If
        Me.Hide()
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.