Update column status from 0 to 1 on Button Click in ASP.Net

Last Reply 10 days ago By pandeyism

Posted 10 days ago

While Updating status is not changing from 0 to 1

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvData.Rows)
            {
                CheckBox status = (row.Cells[1].FindControl("chk") as CheckBox);

                if (status.Checked)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("Update AddRaw_199 set status=1 where Name= + '" + row.Cells[2].Text + "'", con);
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        con.Open();
                        int suc = cmd.ExecuteNonQuery();

                        if (suc > 0)
                        {
                            SendMail(row.Cells[1].Text);
                            gvData.DataBind();
                            if (gvData.Rows.Count == 0)
                            {
                                lbl_app.CssClass = "text-center text-primary";
                                lbl_app.Text = "All Users are approved ";
                                Button1.Visible = false;
                            }
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        if (con.State == ConnectionState.Open)
                            con.Close();
                    }
                }
            }

            lbl_app.CssClass = "text-center text-success";
            Response.Redirect("ViewFarmer.aspx");
            lbl_app.Text = "Status Updated Successfully";
        } 

 

 

<asp:Label ID="lbl_app" runat="server"></asp:Label>
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False" HeaderStyle-Height="100px"
    ShowFooter="True" Width="100%" Font-Size="25px" HorizontalAlign="Center" CellPadding="4"
    ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Approve">
            <HeaderTemplate>
                <div class="text-center">
                    <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />Approve/Reject
                </div>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>                
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Add" ForeColor="Red" OnClick="Button1_Click"
    Font-Size="20px" CssClass="bg-primary" Width="100px" Height="50px" />

 

You are viewing reply posted by: pandeyism 10 days ago.
Posted 10 days ago

Hi VinitaAkha,

You have taken TemplateField in GridView so you need to find the controls which control you want. Try below code.

Code

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    foreach (GridViewRow row in gvData.Rows)
    {
        CheckBox status = (row.Cells[1].FindControl("chk") as CheckBox);
        if (status.Checked)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Update AddRaw_199 set status = 1 where Name= + '" + (row.FindControl("Name") as Label).Text + "'");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                int suc = cmd.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }
    }
    lbl_app.CssClass = "text-center text-success";
    Response.Redirect("ViewFarmer.aspx");
    lbl_app.Text = "Status Updated Successfully";
}