Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problems with Shared Functions in an ASP.NET Class?

Posted on 2004-08-18
8
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

721 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