0

My code is this ![enter image description here][1]

But I am getting a Null exception error :(

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim textquestionselected As New List(Of Integer)
        Dim imagequestionselected As New List(Of Integer)
        Dim text_question_id As Integer
        Dim image_question_id As Integer
        For Each gridviewrow As GridViewRow In GridView1.Rows
            If (CType(gridviewrow.FindControl("CheckBox1"), CheckBox).Checked) Then

                text_question_id = Convert.ToInt16(CType(gridviewrow.FindControl("small_int_question_id"), Label).Text)
                textquestionselected.Add(text_question_id)
            End If
        Next
        For Each gridviewrow1 As GridViewRow In GridView2.Rows
            If (CType(gridviewrow1.FindControl("CheckBox2"), CheckBox).Checked) Then

                image_question_id = CType(gridviewrow1.FindControl("small_int_question_id"), Label).Text
                imagequestionselected.Add(image_question_id)
            End If
        Next

enter image description here

I used bound Field instead Label .. but same error

4
  • 1
    Can you actually post the code instead of an image? Commented Oct 31, 2014 at 20:37
  • When you have code, please copy/paste it into your answer as actual text, don't embed it as an image. You can search text within an image, images are on different domains and can be blocked, and it's hard to re-use your code in an answer. Commented Oct 31, 2014 at 20:38
  • We need to be able to see the error log clearly. Commented Oct 31, 2014 at 20:38
  • I have posted the Code instead of Image .. Thanks all Commented Nov 1, 2014 at 4:38

1 Answer 1

1

You have to check the type of row it is; it's probably the header or footer row. Check the RowType property to make sure it's a DataRow.

if (row.RowType == DataControlRowType.DataRow)
{
   //Check control 
}

Also according to your subject, make sure the type you are converting is the same. It would really help to see a snippet of the markup.

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

2 Comments

I think the error is in 'Label' keyword in this line .. Convert.ToInt16(CType(gridviewrow.FindControl("small_int_question_id"), Label).Text)
Right but without checking the type of row it is, you could be trying to find it in the header row, which doesn't exist. Also try using gridviewrow.Cells[index].FindControl to see if that makes a difference.

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.