Solved

Object Reference Not Set Error with DB2DataReader

Posted on 2004-09-28
8
352 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
8 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 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
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12175317
try using dataset. lets see where is the real error ..
-Baan
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now