Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problems with Shared Functions in an ASP.NET Class?

Posted on 2004-08-18
8
Medium Priority
?
206 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 520 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

971 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