Reputation: 781
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
Upvotes: 2
Views: 1063
Reputation: 52241
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
Upvotes: 1
Reputation: 6802
Change your code to something like this and retry:
CheckBox cbox = ((CheckBox)row.FindControl("Verify"));
if (cbox != null && cbox.Checked)
{
....
}
Upvotes: 1
Reputation: 2683
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.
Upvotes: 1