Solved

Object Reference Not Set Error with DB2DataReader

Posted on 2004-09-28
8
363 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
VS.net 2010 11 48
Regex validation 2 28
VB.net Open Folder in Windows Explorer 3 56
VB.NET 2008 - Windows Protected Your PC 7 41
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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