[thelist] ASP.NET: Specified cast is not valid.

Casey Crookston caseyc at netgainhosting.com
Mon May 15 16:58:23 CDT 2006

When trying to use roles authentication, I receive this error in the
global.asax file:


Exception Details: System.InvalidCastException: Specified cast is not

Source Error: 

Line 57:             Dim arrRoles As String()
Line 58:             conWiese.Open()
Line 59:             arrRoles = cmdSelect.ExecuteScalar()
Line 60:             conWiese.Close()
Line 61: 

Source File: C:\Sites\local.wieseford.com\Global.asax.vb    Line: 59


Line 59 is: arrRoles = cmdSelect.ExecuteScalar()


Any idea?  Thanks!



    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e
As System.EventArgs)

        If Context.Request.IsAuthenticated() Then

            ' extract the user ID from HttpApplication

            Dim userID As String = Context.User.Identity.Name


            ' query the user role from the dastabase bases on user ID

            Dim conWiese As New

            Dim cmdSelect As New SqlCommand("SELECT role FROM Employees
WHERE UserName = @userName", conWiese)

            cmdSelect.Parameters.Add(New SqlParameter("@userName",
SqlDbType.NVarChar, 500))

            cmdSelect.Parameters.Item("@userName").Value = userID

            Dim arrRoles As String()


            arrRoles = cmdSelect.ExecuteScalar()



            ' assign the user a role based on the query

            Context.User = New GenericPrincipal(Context.User.Identity,

        End If

    End Sub

More information about the thelist mailing list