Solved

Problems with Shared Functions in an ASP.NET Class?

Posted on 2004-08-18
8
199 Views
Last Modified: 2010-04-07
Hi,

Cutting straight to the chase... I've got an ASPX page attempting to use a public shared function I've written called "IsMemberOfRole". This I know works. However, some simple code I've put inside of this function causes the following error:

-----

An error was encountered on page /loggedin/OrganisationPost.aspx.
The error was: System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.

-----

Here's the code snippet causing the problem:

-----

        SQLGetAllUserRoles.CommandType = System.Data.CommandType.StoredProcedure
        SQLGetAllUserRoles.CommandText = ("dbo.[GetAllUserRoles]")
        SQLGetAllUserRoles.Connection = SQLConnection1
        SQLGetAllUserRoles.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Username", System.Data.SqlDbType.VarChar, 1))
        SQLGetAllUserRoles.Parameters("@Username").Value = Username
        SQLConnection1.Open()

        Dim strRoles As String
        strRoles = SQLGetAllUserRoles.ExecuteScalar

        Dim arrAllRoles As Array
        arrAllRoles = strRoles.Split(",")        ******************** 'WHAT'S WRONG WITH THIS? It works in other pages.

        Dim Role As String
        For Each Role In arrAllRoles
            If UCase(Role) = UCase(RoleName) Then
                Return True
            End If
        Next

        SQLConnection1.Close()

------

So this is my problem! I think I'm doing this right... I can DEFINATELY say that the SQL Stored Procedure dbo.[GetAllUserRoles] works correctly, and definately returns a comma separate list of values for the user I am testing it with.

Anyone shed any light?

Points to the first person who correctly provides a step-by-step fix!
0
Comment
Question by:OnError_Fix
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 11830007
Hi,

have you tried this :

 Dim arrAllRoles As String() = Nothing


Regards,
B..M
0
 

Author Comment

by:OnError_Fix
ID: 11830048
Just have mate, still no joy.

I have left this modification in for future attempts! Increased points from 125 to 130.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11830090
can you post what strRoles is before the split ?

Regards,
B..M
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:OnError_Fix
ID: 11830353
strRoles is:

NTStaff,CModerators
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11830526
i've tested your code and it's working ok
may be the problem is at this line but not in this case

so i suggest you to debug the application and check if there is some other method that call this code and return something else
this works fine
        Dim strRoles As String = "NTStaff,CModerators"
        Dim arrAllRoles As Array
        arrAllRoles = strRoles.Split(",")

        Dim s As String

        For Each s In arrAllRoles
            Console.WriteLine(s)

        Next

Regards,
B..M
0
 

Author Comment

by:OnError_Fix
ID: 11830679
Should it make any difference if this code resides in a separate class.vb file?
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 130 total points
ID: 11830765
As i know - NO
try to tested like this
instead of
strRoles = SQLGetAllUserRoles.ExecuteScalar
use
strRoles = "NTStaff,CModerators"

then you will know that evry time when this code is called it will be the same and it should work and check if there is a problem
if there is not - then some times the stored procedure does not return a value ( you can track the parameters by Sql Profiler



Regards,
B..M
0
 

Author Comment

by:OnError_Fix
ID: 11831066
Hi mate,

That worked fine --- it turned out that when I had programatically created the SQL Parameter 'username' I had not correctly specified it's size. Hence, the username paramater only accepted the FIRST character of the username -- so SQL would return no records that matched. One quick glance at the SQL Profiler tool revealed this.

Thanks mate, enjoy your points.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question