Aniket
Aniket

Reputation: 120

Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.UserControl

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

Upvotes: 0

Views: 1989

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

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.

Upvotes: 1

Related Questions