Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. A Null reference exception could occur at runtime

A Null reference exception could occur at runtime

Scheduled Pinned Locked Moved Visual Basic
helpsales
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thedom2
    wrote on last edited by
    #1

    OK people I am a hack programmer - think of me as the homer simpson of programming See my code below and please help me eliminate the "Null reference exception" warning/error I get.. Thanks I know this is a easy and i am a d*mb one

    Public Overloads Function LoadOrdersData() As Data.DataSet
    Dim sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConn3").ConnectionString)
    Dim sqlDa As New SqlDataAdapter("spBackOrders", sqlConn)
    Dim sqlDs As New Data.DataSet

        sqlDa.SelectCommand.CommandType = Data.CommandType.StoredProcedure
        sqlDa.SelectCommand.Parameters.AddWithValue("@Customer", Session("CustCode"))
        sqlDa.SelectCommand.Parameters.AddWithValue("@Customer2", Session("CustCode2"))
    
        Try
            sqlConn.Open()
            sqlDa.Fill(sqlDs)
            sqlConn.Close()
            Return sqlDs
        Catch
    
        Finally
            If sqlConn.State = Data.ConnectionState.Open Then
                sqlConn.Close()
            End If
        End Try
    End Function
    
    C L 2 Replies Last reply
    0
    • T thedom2

      OK people I am a hack programmer - think of me as the homer simpson of programming See my code below and please help me eliminate the "Null reference exception" warning/error I get.. Thanks I know this is a easy and i am a d*mb one

      Public Overloads Function LoadOrdersData() As Data.DataSet
      Dim sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConn3").ConnectionString)
      Dim sqlDa As New SqlDataAdapter("spBackOrders", sqlConn)
      Dim sqlDs As New Data.DataSet

          sqlDa.SelectCommand.CommandType = Data.CommandType.StoredProcedure
          sqlDa.SelectCommand.Parameters.AddWithValue("@Customer", Session("CustCode"))
          sqlDa.SelectCommand.Parameters.AddWithValue("@Customer2", Session("CustCode2"))
      
          Try
              sqlConn.Open()
              sqlDa.Fill(sqlDs)
              sqlConn.Close()
              Return sqlDs
          Catch
      
          Finally
              If sqlConn.State = Data.ConnectionState.Open Then
                  sqlConn.Close()
              End If
          End Try
      End Function
      
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      thedom2 wrote:

      think of me as the homer simpson of programming

      ROTFL !!!

      thedom2 wrote:

      If sqlConn.State = Data.ConnectionState.Open Then

      Given the rest is in a try catch I'd say you need to check if sqlConn is equal to nothing before checking it's properties. You should also put a message box in the catch to tell you the error, b/c if this is it, you won't be done, the code does not work. Probably because the connection fails, I would think,

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • T thedom2

        OK people I am a hack programmer - think of me as the homer simpson of programming See my code below and please help me eliminate the "Null reference exception" warning/error I get.. Thanks I know this is a easy and i am a d*mb one

        Public Overloads Function LoadOrdersData() As Data.DataSet
        Dim sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConn3").ConnectionString)
        Dim sqlDa As New SqlDataAdapter("spBackOrders", sqlConn)
        Dim sqlDs As New Data.DataSet

            sqlDa.SelectCommand.CommandType = Data.CommandType.StoredProcedure
            sqlDa.SelectCommand.Parameters.AddWithValue("@Customer", Session("CustCode"))
            sqlDa.SelectCommand.Parameters.AddWithValue("@Customer2", Session("CustCode2"))
        
            Try
                sqlConn.Open()
                sqlDa.Fill(sqlDs)
                sqlConn.Close()
                Return sqlDs
            Catch
        
            Finally
                If sqlConn.State = Data.ConnectionState.Open Then
                    sqlConn.Close()
                End If
            End Try
        End Function
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        There's a path where nothing will get returned. If you return something in the Catch block (or even Return Nothing) or after the End Try you won't get that warning.

        C 1 Reply Last reply
        0
        • L Lost User

          There's a path where nothing will get returned. If you return something in the Catch block (or even Return Nothing) or after the End Try you won't get that warning.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Wow - VB.NET still pulls crap like that ? What a disaster....

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          L 1 Reply Last reply
          0
          • C Christian Graus

            Wow - VB.NET still pulls crap like that ? What a disaster....

            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Unfortunately, yes. It reminds me of this gem from MSDN[^]: The code in a Finally block runs after a Return statement in a Try or Catch block is encountered, but before that Return statement executes. In this situation, a Return statement in the Finally block executes before the initial Return statement. This gives a different return value. To prevent this potentially confusing situation, avoid using Return statements in Finally blocks. (Modified because I didn't do the quoting right.)

            modified on Tuesday, June 10, 2008 11:17 AM

            T 1 Reply Last reply
            0
            • L Lost User

              Unfortunately, yes. It reminds me of this gem from MSDN[^]: The code in a Finally block runs after a Return statement in a Try or Catch block is encountered, but before that Return statement executes. In this situation, a Return statement in the Finally block executes before the initial Return statement. This gives a different return value. To prevent this potentially confusing situation, avoid using Return statements in Finally blocks. (Modified because I didn't do the quoting right.)

              modified on Tuesday, June 10, 2008 11:17 AM

              T Offline
              T Offline
              thedom2
              wrote on last edited by
              #6

              thanks everyone :) doh!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups