Solved

Problems with Shared Functions in an ASP.NET Class?

Posted on 2004-08-18
8
198 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 43
Reset asp.net login password 4 44
Winform Module - What is the ASP.Net equiv 2 23
RLDC Reporting in Visual studio 11 16
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…
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…

832 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