Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My question is regarding a checkbox and validation event. The code below works fine if I comment out this section:
VB
If e.Cancel = False Then
    If checkbox.Checked = False Then
        e.Cancel = True
        label1.Visible = True
        label1.Text = "select a prod"
    Else
        label1.Visible = False


I get object reference not set to instance of an object error when I leave the code in. Is there a different way to use checkbox.checked = false? I am just trying to check if any checkbox on gridview 1 is not checked, and if it is not checked show an error message. If it is checked, then print out the values of my dropdownlist and the values of the row I checked on my gridview.

any ideas on how to fix this? or what am I doing wrong?
here is the full code:

VB
    Protected Sub LitFormView_ItemInserting(sender As Object, e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles LitFormView.ItemInserting

Dim checkbox As CheckBox = CType(GridView1.FindControl("Checkbox1"), CheckBox)
Dim lit As DropDownList = CType(LitFormView.FindControl("DropDownList1"), DropDownList)
Dim label1 As Label = CType(LitFormView.FindControl("Label1"), Label)

If lit.SelectedValue = "" Then
    e.Cancel = True
    label1.Visible = True
    label1.Text = "select a lit"
Else
    label1.Visible = False
'below is where i get the error
    If e.Cancel = False Then
        If checkbox.Checked = False Then
            e.Cancel = True
            label1.Visible = True
            label1.Text = "select a prod"
        Else
            label1.Visible = False
'above is where i get the error
            If e.Cancel = False Then
                Label4.Text = ""
                Label5.Text = ""
                For Each gvr As GridViewRow In GridView1.Rows
                    If gvr.RowType = DataControlRowType.DataRow Then
                        Dim cb As CheckBox = DirectCast(gvr.FindControl("CheckBox1"), CheckBox)
                        Dim literature As DropDownList = CType(LitFormView.FindControl("DropDownList1"), DropDownList)
                        If cb.Checked = True Then
                            Label2.Text = literature.SelectedItem.Text
                            Label3.Text = literature.SelectedValue
                            Label4.Text = Label4.Text + gvr.Cells(1).Text
                            Label5.Text = Label5.Text + gvr.Cells(2).Text
                        End If
                    End If
                Next
                Label4.Text = (Label4.Text).Substring(0)
                Label5.Text = (Label5.Text).Substring(0)
            End If
        End If
    End If
End If
end sub
Posted
Updated 17-Oct-12 4:03am
v3
Comments
Akinmade Bond 17-Oct-12 9:35am    
Looks like checkbox is null. Inside what event did you copy your full code from?
PTG.Jake 17-Oct-12 9:40am    
my code is on formview iteminserting
Akinmade Bond 17-Oct-12 9:50am    
The event. Something like

Public Sub MyForm_Click(Sender a Object, e as EventArgs)
...
EndSub

1 solution

Try this:

VB
If e.Cancel = False Then
       If checkbox <> Nothing And Not checkbox.Checked Then
           e.Cancel = True
           label1.Visible = True
           label1.Text = "select a prod"
       End If
End If


else
while debugging check that checkbox or e is not null.
 
Share this answer
 
v2
Comments
manak chand 17-Oct-12 9:43am    
check box may be null..
Akinmade Bond 17-Oct-12 9:46am    
Yeah but you didn't add that to your code.
manak chand 17-Oct-12 9:48am    
yeah, thnx
Akinmade Bond 17-Oct-12 9:51am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900