Solved

Object Reference Not Set Error with DB2DataReader

Posted on 2004-09-28
8
359 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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