Unusual "Object reference not set to an instance of an object"

Last post 08-28-2007 10:24 AM by Contributor. 6 replies.

Sort Posts:

  • Unusual "Object reference not set to an instance of an object"

    01-25-2007, 4:42 PM
    Locked
    • Member
      553 point Member
    • shauns1
    • Member since 10-19-2005, 2:09 PM
    • Posts 125

    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 
  • Re: Unusual "Object reference not set to an instance of an object"

    01-25-2007, 6:01 PM
    Answer
    Locked
    • All-Star
      22,373 point All-Star
    • longhorn2005
    • Member since 05-08-2003, 11:59 PM
    • Perth, Western Australia
    • Posts 1,347
    • Moderator

    Hi Shauns1,

     

    You are getting this error message as you are trying to access a method directly from your page without create new instance of the class in which the definition of  method exists.

    You will need to use something like this in your code behind file

      

    Public objCMSTools As NEW ULT.CMSTools

    Dim objReader As SqlDataReader = objCMSTools.GetSegmentsByParentID(parentID)
    DataList1.DataSource = objReader
    DataList1.DataBind()

     

    Hope this helps

    Sunny NAGI
    Proper Preparation Prevents Poor Performance

    My Blog
  • Re: Unusual Object reference not set to an instance of an object

    02-15-2007, 12:54 AM
    Locked
    • Member
      12 point Member
    • sherryLes
    • Member since 07-27-2006, 5:31 AM
    • India
    • Posts 8

    Hi,

     You can get this explanation well here

    Object Reference Not Set to an instance of a object

     

  • Re: Unusual Object reference not set to an instance of an object

    03-10-2007, 6:04 PM
    Locked
    • Member
      106 point Member
    • raikkonen
    • Member since 09-29-2006, 9:43 AM
    • Posts 151

    Hi,

    I get the same error when I try to do the following:

    1    Dim conn As New SqlClient.SqlConnection
    2            conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\WebSites\Tavlikos\App_Data\Database.mdf;Integrated Security=True;User Instance=True"
    3            Dim comm As New SqlCommand("SELECT ImageFileName FROM Projects WHERE Description=@Description", conn)
    4            comm.Parameters.Add("@Description", SqlDbType.NVarChar, 50).Value = "μονοκατοικία στον Σταυρό Θεσσαλονίκης"
    5    
    6            Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
    7            Dim ds As DataSet = New DataSet
    8            Dim count As Integer
    9            conn.Open()
    10           count = da.Fill(ds, "Projects")
    11           conn.Close()
    12           Dim i As Integer = 0
    13   
    14           Dim dr As DataRow
    15           For Each dr In ds.Tables("Projects").Rows
    16               imgButtons(i).ImageUrl = Me.ResolveUrl("images/" + dr.Item("ImageFileName").ToString)
    17               i = i + 1
    18           Next
    19       End Sub
    

     To explain this,I have the image filename to the sql database,which I retrieve with the above query....The real images are in the folder "images",so I combine in line 16 the url of the image...

    Any idea would be helpful...

    Thanks!

  • Re: Unusual Object reference not set to an instance of an object

    03-10-2007, 8:31 PM
    Locked
    • All-Star
      96,984 point All-Star
    • mbanavige
    • Member since 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 10,259
    • Moderator
      TrustedFriends-MVPs

    i would suspect this:

    imgButtons(i).ImageUrl

    do you actually have an array of ImageButtons?
    and if so, does it have enough entries?

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Unusual Object reference not set to an instance of an object

    03-11-2007, 6:53 AM
    Locked
    • Member
      106 point Member
    • raikkonen
    • Member since 09-29-2006, 9:43 AM
    • Posts 151

    Yes,I have already created an array of ImageButtons as following:

    1     Public Sub SetImageButtons()
    2            imgButtons(0) = ImageButton1
    3            imgButtons(1) = ImageButton2
    4            imgButtons(2) = ImageButton3
    5            imgButtons(3) = ImageButton4
    6            imgButtons(4) = ImageButton5
    7            imgButtons(5) = ImageButton6
    8            imgButtons(6) = ImageButton7
    9        End Sub
    
     

    But,wait a minuteConfused......I just realised that I called the function nowhere in the code as far as I can see....

    ..Ok,everything works fine now...here is the code(quite simple at last):

     

    1    Public Sub SetImageButtons()
    2            imgButtons(0) = ImageButton1
    3            imgButtons(1) = ImageButton2
    4            imgButtons(2) = ImageButton3
    5            imgButtons(3) = ImageButton4
    6            imgButtons(4) = ImageButton5
    7            imgButtons(5) = ImageButton6
    8            imgButtons(6) = ImageButton7
    9        End Sub
    10   
    11       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    12           SetImageButtons()
    13           Dim conn As New SqlClient.SqlConnection
    14           conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\WebSites\Tavlikos\App_Data\Database.mdf;Integrated Security=True;User Instance=True"
    15           Dim comm As New SqlCommand("SELECT ImageFileName FROM Projects WHERE Description=@Description", conn)
    16           comm.Parameters.Add("@Description", SqlDbType.NVarChar, 50).Value = "μονοκατοικία στον Σταυρό Θεσσαλονίκης"
    17   
    18           Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
    19           Dim ds As DataSet = New DataSet
    20           Dim count As Integer
    21           conn.Open()
    22           count = da.Fill(ds, "Projects")
    23           conn.Close()
    24           Dim i As Integer = 0
    25   
    26           Dim dr As DataRow
    27           For Each dr In ds.Tables("Projects").Rows
    28               imgButtons(i).ImageUrl = "images/" + dr.Item("ImageFileName")
    29               i = i + 1
    30           Next
    31       End Sub
    Thanks for the help!!!
    
     

  • Re: Unusual Object reference not set to an instance of an object

    08-28-2007, 10:24 AM
    Locked
    • Member
      40 point Member
    • Contributor
    • Member since 08-10-2007, 10:20 AM
    • Posts 32

    Problems that can occur with this sort of given a specific array size before assigning their respectuve value(s) to the button , what of if the records returned is more or less than what expected, what will happen to the system. From my perceptive, I thing the best is to get the total record count and pass that to teh method to create the button list and later use that to populate the button value that is

    public  SUb SetImageButtons(cnt)

    for i as Integer=0 to cnt - 1

    imgButtons(i) = ImageButton & (i+1)

    end Sub

    Dim numOfCount as Integer = 0

    Dim conn As New SqlClient.SqlConnection
    14           conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\WebSites\Tavlikos\App_Data\Database.mdf;Integrated Security=True;User Instance=True"
    15           Dim comm As New SqlCommand("SELECT count(ImageFileName) FROM Projects WHERE Description=@Description", conn)
    16           comm.Parameters.Add("@Description", SqlDbType.NVarChar, 50).Value = "μονοκατοικία στον Σταυρό Θεσσαλονίκης"
    17  
    18           numOfCount = comm.ExecuteScalar()

    SetImageButtons(numOfCount)

    and consequent statement follows.

    thanks


     

    Filed under: ,
Page 1 of 1 (7 items)