A Null reference exception could occur at runtime
-
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.DataSetsqlDa.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
-
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.DataSetsqlDa.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
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 )
-
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.DataSetsqlDa.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
-
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.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 )
-
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 )
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
-
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