Hi
I have a funtion within a class in my App_Code folder that returns a SQLDataReader.
When I reference this function from within my CodeBehind to populate a DataList, I get the Object reference not set to an instance of an object error. However, if I put the function inside my code behind it works fine.
Here is the class and function:
1 Imports Microsoft.VisualBasic
2 Imports System
3 Imports System.Data
4 Imports System.Data.Sql
5 Imports System.Data.SqlClient
6
7
8 Namespace ULT
9
10 Public Class CMSTools
11
12 Public objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("SQL2005_317692_simoneConnectionString").ConnectionString)
13
14 Function GetSegmentsByParentID(ByVal parentID) As SqlDataReader
15
16 Dim objCmd As SqlCommand
17
18 objCmd = New SqlCommand("cms_GetSegmentsByParentID", objConn)
19 objCmd.CommandType = CommandType.StoredProcedure
20
21 objCmd.Parameters.Add(New SqlParameter("@parentID", SqlDbType.Int))
22 objCmd.Parameters("@parentID").Value = parentID
23
24 Try
25 objConn.Open()
26 Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)
27 Catch ex As SqlException
28 Throw ex
29 End Try
30
31 End Function
32
33 End Class
34
35 End Namespace
36
Here is my Code Behind:
1 Imports System
2 Imports System.Data
3 Imports System.Data.Sql
4 Imports System.Data.SqlClient
5
6
7 Partial Class admin_CMS_Editor
8 Inherits System.Web.UI.Page
9
10 Public objCMSTools As ULT.CMSTools
11
12 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
13 Dim parentID As Integer
14 parentID = 1
15
16 'get the list of current segments for this page
17
18 ListCurrentSegments(parentID)
19
20 End Sub
21
22
23 Sub ListCurrentSegments(ByVal parentID)
24
25 Dim objReader As SqlDataReader = objCMSTools.GetSegmentsByParentID(parentID)
26
27 DataList1.DataSource = objReader
28 DataList1.DataBind()
29
30
31 End Sub
If I copy the funtion GetSegmentsByParentID from the class file into my CodeBehind and reference it like so
Dim objReader As SqlDataReader = GetSegmentsByParentID(parentID)
it seems to work fine. Any ideas why it is returning this error when I try to use it from within my class?
Thanks