Sujikanth
Sujikanth

Reputation: 133

Grid Link Button Deletes row instead of updating it

My Grid has 2 link buttons, delete button has to delete a row and confirm button has to update a BIT data type column to True from False. The delete Link button works properly as intended but the confirm Link Button deletes the row instead of updating it. I have included both my grid and C# code.

<div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="report_num" OnRowDataBound="GridView1_RowDataBound" Width="995px">
<Columns>
    <asp:BoundField DataField="report_num" HeaderText="report_num" />
    <asp:BoundField DataField="location" HeaderText="location" />
    <asp:BoundField DataField="lat" HeaderText="lat" />
    <asp:BoundField DataField="lng" HeaderText="lng" />
    <asp:BoundField DataField="severity_level" HeaderText="severity_level" />
    <asp:TemplateField HeaderText="Confirm">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Confirm Report</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Delete Report</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
            </asp:GridView>
        </div>

C# code

protected void LinkButton1_Click(object sender, EventArgs e)
        {
                LinkButton lnk1 = sender as LinkButton;
                GridViewRow gridrow1 = lnk1.NamingContainer as GridViewRow;
                int report_num = Convert.ToInt16(GridView1.DataKeys[gridrow1.RowIndex].Value.ToString());
                con.Open();
                cmd.CommandText = "UPDATE [temporary_markers] SET confirmation ='True' WHERE report_num =" + report_num;
                cmd.Connection = con;
                int a = cmd.ExecuteNonQuery();
                con.Close();
                if (a > 0)
                {
                    bindGridView();
                }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton lnk = sender as LinkButton;
            GridViewRow gridrow = lnk.NamingContainer as GridViewRow;
            int report_num = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
            con.Open();
            cmd.CommandText = "DELETE FROM [temporary_markers] WHERE report_num=" + report_num;
            cmd.Connection = con;
            int a = cmd.ExecuteNonQuery();
            con.Close();
            if (a > 0)
            {
                bindGridView();
            }
        }

Upvotes: 0

Views: 53

Answers (1)

Jawad
Jawad

Reputation: 11364

The delete method is working but not the update

When looking at your code, it seems like you are doing the opposite of what you intend to do with the buttons.

Confirm: Confirm Button calls Method LinkButton2_Click which deletes the report.

<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Confirm Report

cmd.CommandText = "DELETE FROM [temporary_markers] WHERE report_num=" + report_num;

Delete: Delete button calls the method that sets confirmation to True

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Delete Report

cmd.CommandText = "UPDATE [temporary_markers] SET confirmation ='True' WHERE report_num =" + report_num;

Solution: Switch the labels on the button from Confirm to Delete and vice versa.

    <asp:TemplateField HeaderText="Delete">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Delete Report</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Confirm">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Confirm Report</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>

Also, if your delete method is working, its probably updating the table correctly with confirmation BIT. Other difference I see in the two methods is the conversion of the string to Int16 / Int32 for report number. Change that to the one that works (probably Convert.ToInt32 for the range of numbers it covers).

Upvotes: 2

Related Questions