Solved

Problems with Shared Functions in an ASP.NET Class?

Posted on 2004-08-18
8
193 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
 

Author Comment

by:OnError_Fix
ID: 11830353
strRoles is:

NTStaff,CModerators
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now