Hi, there, I am trying to come up with a createuserwizard with a role checklist. I am getting this error.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 30:
Line 31: ' Bind the set of roles to RoleList
Line 32: RoleList.DataSource = Roles.GetAllRoles()
Line 33: RoleList.DataBind()
Line 34: End If

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub RegisterUserWithRoles_ActiveStepChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RegisterUserWithRoles.ActiveStepChanged
        'Have we JUST reached the Complete step? 
        If RegisterUserWithRoles.ActiveStep.Title = "Complete" Then
            ' Reference the SpecifyRolesStep WizardStep 
            Dim SpecifyRolesStep As WizardStep = CType(RegisterUserWithRoles.FindControl("SpecifyRolesStep"), WizardStep)

            ' Reference the RoleList CheckBoxList 
            Dim RoleList As CheckBoxList = CType(SpecifyRolesStep.FindControl("RoleList"), CheckBoxList)

            ' Add the checked roles to the just-added user 
            For Each li As ListItem In RoleList.Items
                If li.Selected Then
                    Roles.AddUserToRole(RegisterUserWithRoles.UserName, li.Text)
                End If
            Next
        End If

    End Sub
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ' Reference the SpecifyRolesStep WizardStep 
            Dim SpecifyRolesStep As WizardStep = CType(RegisterUserWithRoles.FindControl("SpecifyRolesStep"), WizardStep)

            ' Reference the RoleList CheckBoxList 
            Dim RoleList As CheckBoxList = CType(SpecifyRolesStep.FindControl("RoleList"), CheckBoxList)

            ' Bind the set of roles to RoleList 
            [B]RoleList.DataSource = Roles.GetAllRoles()[/B]
            RoleList.DataBind()
        End If

        


    

    End Sub
End Class

Recommended Answers

All 5 Replies

The statement 'SpecifyRolesStep.FindControl("RoleList")' may not return a control.

Check that by putting a break point.

i am still getting the error. i ll insert it in as below..
Compiler Error Message: BC30201: Expression expected.

Source Error:

Line 27:
Line 28: ' Reference the RoleList CheckBoxList
Line 29: Dim RoleList As CheckBoxList = CType(, CheckBoxList)
Line 30:
Line 31: ' Bind the set of roles to RoleList

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub RegisterUserWithRoles_ActiveStepChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RegisterUserWithRoles.ActiveStepChanged
        'Have we JUST reached the Complete step? 
        If RegisterUserWithRoles.ActiveStep.Title = "Complete" Then
            ' Reference the SpecifyRolesStep WizardStep 
            Dim SpecifyRolesStep As WizardStep = CType(RegisterUserWithRoles.FindControl("SpecifyRolesStep"), WizardStep)

            ' Reference the RoleList CheckBoxList 
            Dim RoleList As CheckBoxList = CType(SpecifyRolesStep.FindControl("RoleList"), CheckBoxList)

            ' Add the checked roles to the just-added user 
            For Each li As ListItem In RoleList.Items
                If li.Selected Then
                    Roles.AddUserToRole(RegisterUserWithRoles.UserName, li.Text)
                End If
            Next
        End If

    End Sub
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ' Reference the SpecifyRolesStep WizardStep 
            Dim SpecifyRolesStep As WizardStep = CType(RegisterUserWithRoles.FindControl("SpecifyRolesStep"), WizardStep)

            ' Reference the RoleList CheckBoxList 
            Dim RoleList As CheckBoxList = CType(, CheckBoxList)

            ' Bind the set of roles to RoleList 
            RoleList.DataSource = Roles.GetAllRoles()
            RoleList.DataBind()
        End If

        


    

    End Sub
End Class

This error might also be occured due to HTML code. Post your HTML code. It will be helpful to identify the root cause of the error.

dude....your code loks okey....

put the break point...or send me your aspx code...

hi,
there, i have included both my behind VB code and HTML code.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Transaction2.aspx.vb" Inherits="Transaction2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" AutoGenerateEditButton="True"  runat="server">
        
        <Columns>
        <asp:TemplateField> 

    <HeaderTemplate> 

        <asp:CheckBox ID="chkAll" runat="server" 

            onclick = "checkAll(this);" /> 

    </HeaderTemplate> 

    <ItemTemplate> 

        <asp:CheckBox ID="CheckBox1" runat="server" 

            onclick = "Check_Click(this)"/> 

    </ItemTemplate> 

</asp:TemplateField> 


        
        
        
        <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "CusID" HeaderText = "Customer ID" /> 

<asp:BoundField ItemStyle-Width = "150px" 

    DataField = "CusFName" HeaderText = "First Name"/> 

<asp:BoundField ItemStyle-Width = "150px" 

    DataField = "CusLName" HeaderText = "Last Name"/> 

<asp:BoundField ItemStyle-Width = "150px" 

    DataField = "Address" HeaderText = "Address"/> 
<asp:BoundField ItemStyle-Width = "150px" 

    DataField = "City" HeaderText = "City"/> 
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "State" HeaderText = "State"/>
     
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "HP_Num" HeaderText = "HP_Num"/>
    
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "receiver" HeaderText = "Receiver Name"/>
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "Address1" HeaderText = "Address"/>
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "Poscode" HeaderText = "Poscode"/>
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "City1" HeaderText = "City"/>
    
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "State1" HeaderText = "State"/>
    <asp:BoundField ItemStyle-Width = "150px" 

    DataField = "Contact_Num" HeaderText = "Contact Number"/>
    
    
    
        
        
        </Columns>
        </asp:GridView>
        
        
         <asp:Button ID="Button1" runat="server" Text="Assign" />
         
         
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CourierSystemConnectionString %>" 
        
        UpDateCommand="UpDate PickUp_CusRegis_Table Set CusID=@CusID, CusFName=@CusFName, CusLName=@CusLName, City=@City, Address=@Address, State=@State,HP_Num=@HP_Num, Receiver=@Receiver, Address1=@Address1, City1=City1, State1=@State1, Contact_Num=@Contact_Num, Poscode=@Poscode"
        OldValuesParameterFormatString="original_{0}"
        SelectCommand="SELECT [CusID], [CusFName], [CusLName], [Password2], [RetypePass2], [Address], [City], [State], [HP_Num], [Fax_Num], [Receiver], [Address1], [City1], [State1], [Contact_Num], [Poscode] FROM [PickUp_CusRegis_Table]"></asp:SqlDataSource>
    




    </form>
</body>
</html>
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Partial Class Transaction2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strQuery As String = "select CustomerID,City,Country,PostalCode from customers"
        Dim cmd As New SqlCommand(strQuery)
        Dim dt As DataTable = GetData(cmd)
        Dim CheckBoxArray As ArrayList
        If ViewState("CheckBoxArray") IsNot Nothing Then
            CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
        Else
            CheckBoxArray = New ArrayList()
        End If

        If IsPostBack Then
            Dim CheckBoxIndex As Integer
            Dim CheckAllWasChecked As Boolean = False
            Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)

            Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex

            If chkAll.Checked Then
                If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then
                    CheckBoxArray.Add(checkAllIndex)
                End If
            Else
                If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
                    CheckBoxArray.Remove(checkAllIndex)
                    CheckAllWasChecked = True
                End If
            End If
            For i As Integer = 0 To GridView1.Rows.Count - 1
                If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
                    Dim chk As CheckBox = _
                     DirectCast(GridView1.Rows(i).Cells(0) _
                     .FindControl("CheckBox1"), CheckBox)
                    CheckBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1)
                    If chk.Checked Then
                        If CheckBoxArray.IndexOf(CheckBoxIndex) = -1 And _
                            Not CheckAllWasChecked Then
                            CheckBoxArray.Add(CheckBoxIndex)
                        End If
                    Else
                        If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Or _
                            CheckAllWasChecked Then
                            CheckBoxArray.Remove(CheckBoxIndex)
                        End If
                    End If
                End If
            Next
        End If
        ViewState("CheckBoxArray") = CheckBoxArray
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Private Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable()
        Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter()
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    End Function
    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
        If ViewState("CheckBoxArray") IsNot Nothing Then
            Dim CheckBoxArray As ArrayList = _
            DirectCast(ViewState("CheckBoxArray"), ArrayList)
            Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex

            If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
                Dim chkAll As CheckBox = _
                DirectCast(GridView1.HeaderRow.Cells(0) _
                .FindControl("chkAll"), CheckBox)
                chkAll.Checked = True
            End If
            For i As Integer = 0 To GridView1.Rows.Count - 1
                If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
                    If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
                        Dim chk As CheckBox = _
                        DirectCast(GridView1.Rows(i).Cells(0) _
                        .FindControl("CheckBox1"), CheckBox)
                        chk.Checked = True
                        GridView1.Rows(i).Attributes.Add("style", "background-color:aqua")
                    Else
                        Dim CheckBoxIndex As Integer = GridView1.PageSize * (GridView1.PageIndex) + (i + 1)
                        If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Then
                            Dim chk As CheckBox = _
                            DirectCast(GridView1.Rows(i).Cells(0) _
                            .FindControl("CheckBox1"), CheckBox)
                            chk.Checked = True
                            GridView1.Rows(i).Attributes.Add("style", "background-color:aqua")
                        End If
                    End If
                End If
            Next
        End If
    End Sub

    Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)")
            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)")
        End If
    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.