Solved

Object Reference Not Set Error with DB2DataReader

Posted on 2004-09-28
8
371 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net background worker crossthread 11 47
"lblTime is not declared" 3 65
Web page design problem 3 42
CHANGE FORM TEXT BEFORE IT SHOW TO USER 13 35
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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