GridView-How to check particular row

Last Reply 8 days ago By dharmendr

Posted 8 days ago

I am having a Gridview in which i am allowing users to enter the amount. I am having Multiple rows. Both the Textbox in each row can contain Zero value or Both the Text box in each row should contain Numbers greater than Zero. I need to stop users if they enter One text box Zero and another text box with Number greater than Zero.

I dont want to insert the data to Database.

I need to show message to the Users in label How to do in C# .net I need to check this validation at Final Submit

 if (Page.IsValid)
            {
                int counter1 = 0;

                if (chkConfirm.Checked == true)
                {
                    decimal TotalAmt = Decimal.Parse(lblTotalAmt.Text);
                    decimal EnterAmt = Decimal.Parse(Label2.Text);

                    if (lblTotalAccts.Text == Label1.Text && TotalAmt == EnterAmt)
                    {
                       for (int i = 0; i < gvRO.Rows.Count-1; i++)
                        {
                            GridViewRow row = gvRO.Rows[i];
                            string Account = ((TextBox)row.FindControl("txtTAcctsAdmin")).Text;
                            string Amount = ((TextBox)row.FindControl("txtTAmtAdmin")).Text;
                            int Acc = int.Parse(Account);
                            if (((Account.ToString() != "0" && (Amount.ToString() == "0.0000" || Amount.ToString() == "0"))) ||
                            (Account.ToString() == "0" && (Amount.ToString() != "0.0000" || Amount.ToString() != "0")))
                            {
                                counter1 += 1;
                            }
                            else
                            {
                                counter1 +=0;
                            }
                          
                        }
                        if (counter1 >= 1)
                        {
                            lblMessage.Text = "Both Target Accounts and Amount should be Zero or Greater Than Zero";
                            lblMessage.ForeColor = System.Drawing.Color.Red;
                            lblMessage.Visible = true;
                        }
                        else
                        {
                            string Slno = null;
                            foreach (GridViewRow g in gvRO.Rows)
                            {
                                string Salid = lblSalid.Text;
                                Label rcode = (Label)g.Cells[0].FindControl("lblRegionCode");
                                string RegionCode1 = rcode.Text;
                                TextBox tacc = (TextBox)g.Cells[4].FindControl("txtTAcctsAdmin");
                                Int32 TargetAccts = Convert.ToInt32(tacc.Text);
                                TextBox tamt = (TextBox)g.Cells[5].FindControl("txtTAmtAdmin");
                                decimal TargetAmt = Convert.ToDecimal(tamt.Text);
                                string query = @"Update tc set Status=@Status,TargetAcctAdmin=@TargetAcctAdmin, TargetAmtAdmin=@TargetAmtAdmin,
                                      ApprovedBy = @Empid, ApprovedDate = @ApprovedDate Where Salid=@Salid and IncCategory='RO' and Loccode=@LocCode;";
                                Slno = RegionCode1.ToString();
                                SqlCommand cmd2 = new SqlCommand(query, con);
                                con.Open();
                                cmd2.Parameters.AddWithValue("@Salid", Salid);
                                cmd2.Parameters.AddWithValue("@LocCode", RegionCode1);
                                cmd2.Parameters.AddWithValue("@Status", "APPR");
                                cmd2.Parameters.AddWithValue("@TargetAcctAdmin", TargetAccts);
                                cmd2.Parameters.AddWithValue("@TargetAmtAdmin", TargetAmt);
                                cmd2.Parameters.AddWithValue("@Empid", AdminID);
                                cmd2.Parameters.AddWithValue("@ApprovedDate", DateTime.Now);
                                cmd2.ExecuteNonQuery();
                                con.Close();

                            }
                            Response.Write("<script language='javascript'>window.alert('Regional Office Approved Successfully!');window.location = 'pgtest.aspx';</script>");
                        }


                    }
                    else
                    {
                        lblMessage.Text = "Total Target (Account & Amount) and Entered (Account and Amount) should match!";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                        lblMessage.Visible = true;
                    }

 

You are viewing reply posted by: dharmendr 8 days ago.
Posted 8 days ago

Use below code.

if (Page.IsValid)
{
    int counter1 = 0;

    if (chkConfirm.Checked == true)
    {
        decimal TotalAmt = Decimal.Parse(lblTotalAmt.Text);
        decimal EnterAmt = Decimal.Parse(Label2.Text);

        if (lblTotalAccts.Text == Label1.Text && TotalAmt == EnterAmt)
        {
            for (int i = 0; i < gvRO.Rows.Count - 1; i++)
            {
                GridViewRow row = gvRO.Rows[i];
                string Account = ((TextBox)row.FindControl("txtTAcctsAdmin")).Text;
                string Amount = ((TextBox)row.FindControl("txtTAmtAdmin")).Text;
                int Acc = int.Parse(Account);
                int Amo = int.Parse(Account);
                if ((Acc == 0 && Amo == 0) || (Acc > 0 && Amo > 0))
                {
                    counter1 += 1;
                }
                else
                {
                    counter1 += 0;
                }
            }
            if (counter1 >= 1)
            {
                lblMessage.Text = "Both Target Accounts and Amount should be Zero or Greater Than Zero";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Visible = true;
            }
            else
            {
                string Slno = null;
                foreach (GridViewRow g in gvRO.Rows)
                {
                    string Salid = lblSalid.Text;
                    Label rcode = (Label)g.Cells[0].FindControl("lblRegionCode");
                    string RegionCode1 = rcode.Text;
                    TextBox tacc = (TextBox)g.Cells[4].FindControl("txtTAcctsAdmin");
                    Int32 TargetAccts = Convert.ToInt32(tacc.Text);
                    TextBox tamt = (TextBox)g.Cells[5].FindControl("txtTAmtAdmin");
                    decimal TargetAmt = Convert.ToDecimal(tamt.Text);
                    string query = @"Update tc set Status=@Status,TargetAcctAdmin=@TargetAcctAdmin, TargetAmtAdmin=@TargetAmtAdmin,
                                ApprovedBy = @Empid, ApprovedDate = @ApprovedDate Where Salid=@Salid and IncCategory='RO' and Loccode=@LocCode;";
                    Slno = RegionCode1.ToString();
                    SqlCommand cmd2 = new SqlCommand(query, con);
                    con.Open();
                    cmd2.Parameters.AddWithValue("@Salid", Salid);
                    cmd2.Parameters.AddWithValue("@LocCode", RegionCode1);
                    cmd2.Parameters.AddWithValue("@Status", "APPR");
                    cmd2.Parameters.AddWithValue("@TargetAcctAdmin", TargetAccts);
                    cmd2.Parameters.AddWithValue("@TargetAmtAdmin", TargetAmt);
                    cmd2.Parameters.AddWithValue("@Empid", AdminID);
                    cmd2.Parameters.AddWithValue("@ApprovedDate", DateTime.Now);
                    cmd2.ExecuteNonQuery();
                    con.Close();

                }
                Response.Write("<script language='javascript'>window.alert('Regional Office Approved Successfully!');window.location = 'pgtest.aspx';</script>");
            }
        }
        else
        {
            lblMessage.Text = "Total Target (Account & Amount) and Entered (Account and Amount) should match!";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }
}