2

I am trying to implement a checkbox within gridview,

The job of this checkbox is to verify a record,

When this verify button is pressed, all items with a checked checkbox are inputted into the database

This is my code:

    protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
        {

             CheckBox cbox = ((CheckBox)row.FindControl("Verify"));

            if (cbox.Equals(true))
            {
                String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text;
                String TCtext = ((TextBox)row.FindControl("numTC")).Text;

                if (row.RowType == DataControlRowType.DataRow)
                {
                    //Header trs = new Header();
                    // GridView1.Rows[0].FindControl("numTC");
                    if (TCtext != "" && DraftsText != "")
                    {

                        //  try
                        //  {
                        string date = row.Cells[4].Text;

                        DateTime dateTime = Convert.ToDateTime(date);
                        string dateFormatted = dateTime.ToString("d-MMM-yy");

                        string unit = row.Cells[5].Text;
                        string currency = row.Cells[6].Text;
                        string totalFC = row.Cells[7].Text;
                        string totalDC = row.Cells[8].Text;
                        int d = Convert.ToInt32(DraftsText);
                        int tc = Convert.ToInt32(TCtext);


                        hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC);
                        hdr.InsertFCTC(hdr);
                    }

                }

            }
        }
    }

I might be going at this the wrong way but in the if (cbox.Equals(true)) its giving me an exception: Object reference not set to an instance of an object.

Any idea what i can do to solve this?

Many Thanks

3 Answers 3

1

This if (cbox.Equals(true)) should be if (cbox.Checked)

Since your cbox is a checkbox object it can't be used to compare, so you have to use the cbox Checked Property, which will return true/false

Sign up to request clarification or add additional context in comments.

3 Comments

still gives me Object reference not set to an instance of an object.
That checkbox is not found when you are trying to finding using row.FindControl... You have to post aspx markup to find the issue.
ok problem solved, i was passing the wrong textbox id & the if should have been as you said THANKS
1

You receive a NullPointerException because the suggested checkbox wasn't found! Or the direct cast into an instance of type CheckBox doesn't worked as expected.

1 Comment

ok problem solved, i was passing the wrong textbox id & the if should have been if (cbox.Checked)
1

Change your code to something like this and retry:

CheckBox cbox = ((CheckBox)row.FindControl("Verify"));

            if (cbox != null && cbox.Checked)
            {
....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.