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

x
?
Solved

Object Reference Not Set Error with DB2DataReader

Posted on 2004-09-28
8
Medium Priority
?
379 Views
Last Modified: 2011-08-18
HELP!!

I'm getting the error illustrated by the stack trace below, the offending line has an arrow pointing to it, and the calling and component code follows.  I can't understand why I'm getting this error.  The exact same code works fine if I use it on SQLServer with a SqlDataReader.  This is driving me nuts, can someone help?

Line 48:         Dim dr As DB2DataReader = oRepDb.Db2Report(oRep)
Line 49:         dgWebView.DataSource = dr
Line 50:         dgWebView.DataBind() <------ Error Line
Line 51:         If Not dr Is Nothing Then
Line 52:             dr.Close()

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   IBM.Data.DB2.DB2DataReader.Read() +185
   System.Data.Common.DbEnumerator.MoveNext() +44
   System.Web.UI.WebControls.DataGrid.CreateAutoGeneratedColumns(PagedDataSource dataSource) +268
   System.Web.UI.WebControls.DataGrid.CreateColumnSet(PagedDataSource dataSource, Boolean useDataSource) +190
   System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) +682
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +49
   System.Web.UI.WebControls.BaseDataList.DataBind() +23
   cfss.reports_e.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\cfss\home_e.aspx.vb:50
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277
   
Calling Code:
   
Private Sub btnDb2WebView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDb2WebView.Click
    Dim oRep As New CfssReport()
    Dim oRepDb As New CfssReportDB()
    'populate the report object to pass to the db class
    With oRep
        .Name = "scac32"
        .Category = "purch"
        .Format = "web"
        .Source = "db2"
        .Sql = "SELECT DISTINCT T2.DSTRCT_CODE, T2.WHOUSE_ID, FROM BS6TSTW.MSF23101 T2 WHERE T2.WHOUSE_ID = 'MAIN' WITH UR"
    End With
    Dim dr As DB2DataReader = oRepDb.Db2Report(oRep)
    dgWebView.DataSource = dr
    dgWebView.DataBind()
    dr.Close()
End Sub

Component Code:

Public Class CfssReportDB

    Public Function Db2Report(ByVal oCfssReport As CfssReport) As DB2DataReader

        ' Create Instance of Connection and Command Objects
        Dim oConn As DB2Connection = New DB2Connection(ConfigurationSettings.AppSettings("db2ConnString"))
        Dim oComm As DB2Command = New DB2Command(oCfssReport.Sql, oConn)
        Try
            If oConn.State = ConnectionState.Closed Then
                oConn.Open()
            End If
            Dim dr As DB2DataReader = oComm.ExecuteReader(CommandBehavior.CloseConnection)

            ' Return the datareader result
            Return dr

        Catch e As DB2Exception

            If oConn.State = ConnectionState.Open Then
                oConn.Close()
            End If
            oConn.Dispose()
           
        Finally
       
            oComm.Dispose()
        End Try

    End Function
   
End Class
0
Comment
Question by:brunka
[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
8 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 12169918
Sounds like a bug with the DB2DataReader to me.

Bob
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12171093
Bob,

It is possible to bind a DataReader to a DataGrid?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12171103
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!

 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12175317
try using dataset. lets see where is the real error ..
-Baan
0
 

Author Comment

by:brunka
ID: 12179438
Looks like it could very well be a bug with the DB2DataReader.  Using the exact same code, I switched to an OleDbDataReader and the function returns the reader just fine.

To respond to Baan's comment I also tried a dataset only making necessary modifications to the code to use a DB2Connection and return a dataset, the function returned the dataset just fine as well.

I'm going to get a hold of IBM, and see what they say.

Next question then, what's the ideal solution in the meantime for data retreival, using a fast OleDbDataReader with it's comparitively slow OLEDB provider or a slower dataset using the faster DB2.NET provider knowing the app can often return 1000's of records at a time?

Thanks,

Adam
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12179470
What kind of operations are you looking to do with the data?  If you are doing a one-time read operation, then a DataReader would be better.  But if you are looking for Updates, Insert, and Deletes, then a DataSet would be the better option.

Bob
0
 

Author Comment

by:brunka
ID: 12179586
It's for one-time reads.

Adam
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12180237
Then I would go with the DataReader.

Bob
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

670 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