Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm having trouble in passing a data from datagridview using datatable to subreport, I always get an error of

Object reference not set to an instance of an object. 
I am new with crystal report and I am using vb.Net using visual studio 2012. I really need your help to finish our system, a response will be appreciated. Thank you :) 


What I have tried:

<pre>    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Try
            Dim frm As New Printform
            Dim rpt As New mainreport
            Dim dt1 As New DataTable
            Dim dt2 As New DataTable
            Dim dt3 As New DataTable
            Dim dt4 As New DataTable
            Dim dt5 As New DataTable
            With dt1.Columns
                .Add("Date")
                .Add("Attended By")
                .Add("Last name")
                .Add("First name")
                .Add("Initial")
                .Add("Street")
                .Add("City")
                .Add("Zipcode")
                .Add("Dob")
                .Add("Age")
                .Add("Height")
                .Add("Weight")
                .Add("Gender")
                .Add("Status")
                .Add("Number")
                .Add("Type")
            End With
            With dt2.Columns
                .Add("Attended By")
                .Add("Patient")
                .Add("Patient Type")
                .Add("Date")
                .Add("Complaints")
                .Add("Type")
                .Add("Medicine")
                .Add("Quantity")
                .Add("Remarks")
            End With
            With dt3.Columns
                .Add("Attended By")
                .Add("Patient")
                .Add("Type")
                .Add("Date")
                .Add("rep")
                .Add("flo")
                .Add("lun")
                .Add("hea")
                .Add("sys")
                .Add("dia")
                .Add("pul")
                .Add("sit")
                .Add("agi")
                .Add("dig")
                .Add("gen")
                .Add("uri")
                .Add("ski")
                .Add("loc")
                .Add("ner")
                .Add("eye")
                .Add("col")
                .Add("vis")
                .Add("far")
                .Add("fal")
                .Add("ear")
                .Add("hear")
                .Add("nose")
                .Add("thro")
                .Add("rema")
                .Add("reco")
            End With
            With dt4.Columns
                .Add("Attended By")
                .Add("Patient")
                .Add("Type")
                .Add("Date")
                .Add("phy")
                .Add("hea")
                .Add("hig")
                .Add("low")
                .Add("cir")
                .Add("ner")
                .Add("rad")
                .Add("exc")
                .Add("ane")
                .Add("sin")
                .Add("dia")
                .Add("epi")
                .Add("mal")
                .Add("rhe")
                .Add("thy")
                .Add("tub")
                .Add("hep")
                .Add("ven")
                .Add("chi")
                .Add("his")
                .Add("cli")
                .Add("xray")
                .Add("diag")
                .Add("trea")
                .Add("t1")
                .Add("t2")
                .Add("t3")
                .Add("t4")
                .Add("t5")
                .Add("t6")
                .Add("t7")
                .Add("t8")
                .Add("t9")
                .Add("t10")
                .Add("t11")
                .Add("t12")
                .Add("t13")
                .Add("t14")
                .Add("t15")
                .Add("t16")
                .Add("t17")
                .Add("t18")
                .Add("t19")
                .Add("t20")
                .Add("t21")
                .Add("t22")
                .Add("t23")
                .Add("t24")
                .Add("t25")
                .Add("t26")
                .Add("t27")
                .Add("t28")
                .Add("t29")
                .Add("t30")
                .Add("t31")
                .Add("t32")
            End With
            With dt5.Columns
                .Add("name")
                .Add("immu")
                .Add("alle")
                .Add("prob")
            End With
            For i As Integer = 0 To frmviewallrecords.dv1.RowCount - 1
                dt1.Rows.Add(frmviewallrecords.dv1.Rows(i).Cells(0).Value, frmviewallrecords.dv1.Rows(i).Cells(2).Value, frmviewallrecords.dv1.Rows(i).Cells(3).Value, frmviewallrecords.dv1.Rows(i).Cells(4).Value, frmviewallrecords.dv1.Rows(i).Cells(5).Value, _
                           frmviewallrecords.dv1.Rows(i).Cells(6).Value, frmviewallrecords.dv1.Rows(i).Cells(7).Value, frmviewallrecords.dv1.Rows(i).Cells(8).Value, frmviewallrecords.dv1.Rows(i).Cells(9).Value, frmviewallrecords.dv1.Rows(i).Cells(10).Value, _
                           frmviewallrecords.dv1.Rows(i).Cells(11).Value, frmviewallrecords.dv1.Rows(i).Cells(12).Value, frmviewallrecords.dv1.Rows(i).Cells(13).Value, frmviewallrecords.dv1.Rows(i).Cells(14).Value, _
                           frmviewallrecords.dv1.Rows(i).Cells(15).Value, frmviewallrecords.dv1.Rows(i).Cells(16).Value)
            Next
            For i As Integer = 0 To frmviewallrecords.dv2.RowCount - 1
                dt2.Rows.Add(frmviewallrecords.dv2.Rows(i).Cells(2).Value, frmviewallrecords.dv2.Rows(i).Cells(3).Value, frmviewallrecords.dv2.Rows(i).Cells(4).Value, _
                             frmviewallrecords.dv2.Rows(i).Cells(5).Value, frmviewallrecords.dv2.Rows(i).Cells(6).Value, frmviewallrecords.dv2.Rows(i).Cells(7).Value, _
                            frmviewallrecords.dv2.Rows(i).Cells(8).Value, frmviewallrecords.dv2.Rows(i).Cells(9).Value, frmviewallrecords.dv2.Rows(i).Cells(10).Value)
            Next
            For i As Integer = 0 To frmviewallrecords.dv3.RowCount - 1
                dt3.Rows.Add(frmviewallrecords.dv3.Rows(i).Cells(2).Value, frmviewallrecords.dv3.Rows(i).Cells(3).Value, frmviewallrecords.dv3.Rows(i).Cells(4).Value, frmviewallrecords.dv3.Rows(i).Cells(7).Value, frmviewallrecords.dv3.Rows(i).Cells(8).Value, _
                           frmviewallrecords.dv3.Rows(i).Cells(9).Value, frmviewallrecords.dv3.Rows(i).Cells(10).Value, frmviewallrecords.dv3.Rows(i).Cells(11).Value, frmviewallrecords.dv3.Rows(i).Cells(12).Value, frmviewallrecords.dv3.Rows(i).Cells(13).Value, _
                           frmviewallrecords.dv3.Rows(i).Cells(14).Value, frmviewallrecords.dv3.Rows(i).Cells(15).Value, frmviewallrecords.dv3.Rows(i).Cells(16).Value, frmviewallrecords.dv3.Rows(i).Cells(17).Value, frmviewallrecords.dv3.Rows(i).Cells(18).Value, _
                           frmviewallrecords.dv3.Rows(i).Cells(19).Value, frmviewallrecords.dv3.Rows(i).Cells(20).Value, frmviewallrecords.dv3.Rows(i).Cells(21).Value, frmviewallrecords.dv3.Rows(i).Cells(22).Value, frmviewallrecords.dv3.Rows(i).Cells(23).Value, _
                           frmviewallrecords.dv3.Rows(i).Cells(24).Value, frmviewallrecords.dv3.Rows(i).Cells(25).Value, frmviewallrecords.dv3.Rows(i).Cells(26).Value, frmviewallrecords.dv3.Rows(i).Cells(27).Value, frmviewallrecords.dv3.Rows(i).Cells(28).Value, _
                           frmviewallrecords.dv3.Rows(i).Cells(29).Value, frmviewallrecords.dv3.Rows(i).Cells(30).Value, frmviewallrecords.dv3.Rows(i).Cells(31).Value, frmviewallrecords.dv3.Rows(i).Cells(32).Value, frmviewallrecords.dv3.Rows(i).Cells(33).Value)
            Next
            For i As Integer = 0 To frmviewallrecords.dv4.RowCount - 1
                dt4.Rows.Add(frmviewallrecords.dv4.Rows(i).Cells(2).Value, frmviewallrecords.dv4.Rows(i).Cells(3).Value, frmviewallrecords.dv4.Rows(i).Cells(4).Value, frmviewallrecords.dv4.Rows(i).Cells(7).Value, frmviewallrecords.dv4.Rows(i).Cells(8).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(9).Value, frmviewallrecords.dv4.Rows(i).Cells(10).Value, frmviewallrecords.dv4.Rows(i).Cells(11).Value, frmviewallrecords.dv4.Rows(i).Cells(12).Value, frmviewallrecords.dv4.Rows(i).Cells(13).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(14).Value, frmviewallrecords.dv4.Rows(i).Cells(15).Value, frmviewallrecords.dv4.Rows(i).Cells(16).Value, frmviewallrecords.dv4.Rows(i).Cells(17).Value, frmviewallrecords.dv4.Rows(i).Cells(18).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(19).Value, frmviewallrecords.dv4.Rows(i).Cells(20).Value, frmviewallrecords.dv4.Rows(i).Cells(21).Value, frmviewallrecords.dv4.Rows(i).Cells(22).Value, frmviewallrecords.dv4.Rows(i).Cells(23).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(24).Value, frmviewallrecords.dv4.Rows(i).Cells(25).Value, frmviewallrecords.dv4.Rows(i).Cells(26).Value, frmviewallrecords.dv4.Rows(i).Cells(27).Value, frmviewallrecords.dv4.Rows(i).Cells(28).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(29).Value, frmviewallrecords.dv4.Rows(i).Cells(30).Value, frmviewallrecords.dv4.Rows(i).Cells(31).Value, frmviewallrecords.dv4.Rows(i).Cells(32).Value, frmviewallrecords.dv4.Rows(i).Cells(33).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(34).Value, frmviewallrecords.dv4.Rows(i).Cells(35).Value, frmviewallrecords.dv4.Rows(i).Cells(36).Value, frmviewallrecords.dv4.Rows(i).Cells(37).Value, frmviewallrecords.dv4.Rows(i).Cells(38).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(39).Value, frmviewallrecords.dv4.Rows(i).Cells(40).Value, frmviewallrecords.dv4.Rows(i).Cells(41).Value, frmviewallrecords.dv4.Rows(i).Cells(42).Value, frmviewallrecords.dv4.Rows(i).Cells(43).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(44).Value, frmviewallrecords.dv4.Rows(i).Cells(45).Value, frmviewallrecords.dv4.Rows(i).Cells(46).Value, frmviewallrecords.dv4.Rows(i).Cells(47).Value, frmviewallrecords.dv4.Rows(i).Cells(48).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(49).Value, frmviewallrecords.dv4.Rows(i).Cells(50).Value, frmviewallrecords.dv4.Rows(i).Cells(51).Value, frmviewallrecords.dv4.Rows(i).Cells(52).Value, frmviewallrecords.dv4.Rows(i).Cells(53).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(54).Value, frmviewallrecords.dv4.Rows(i).Cells(55).Value, frmviewallrecords.dv4.Rows(i).Cells(56).Value, frmviewallrecords.dv4.Rows(i).Cells(57).Value, frmviewallrecords.dv4.Rows(i).Cells(58).Value, _
                           frmviewallrecords.dv4.Rows(i).Cells(59).Value, frmviewallrecords.dv4.Rows(i).Cells(60).Value, frmviewallrecords.dv4.Rows(i).Cells(61).Value, frmviewallrecords.dv4.Rows(i).Cells(62).Value, frmviewallrecords.dv4.Rows(i).Cells(63).Value)
            Next
            Dim count As Integer = frmviewallrecords.dv5.RowCount

            If frmviewallrecords.dv6.RowCount > frmviewallrecords.dv5.RowCount Then
                count = frmviewallrecords.dv6.RowCount
                If frmviewallrecords.dv7.RowCount > frmviewallrecords.dv6.RowCount Then
                    count = frmviewallrecords.dv7.RowCount
                End If
            ElseIf frmviewallrecords.dv7.RowCount > frmviewallrecords.dv5.RowCount Then
                count = frmviewallrecords.dv7.RowCount
            End If
            For i As Integer = 0 To count - 1
                Dim immu As String
                Dim alle As String
                Dim prob As String
                immu = getImmu(frmviewallrecords.dv5, i)
                alle = getAlle(frmviewallrecords.dv6, i)
                prob = getProb(frmviewallrecords.dv7, i)

                dt5.Rows.Add(frmviewallrecords.Label8.Text, immu, alle, prob)
            Next
        
            rpt.Subreports.Item("Subreport1").SetDataSource(dt1)--------->Error: Object reference not set to an instance of an object
            rpt.Subreports.Item("Subreport2").SetDataSource(dt2)
            rpt.Subreports.Item("Subreport3").SetDataSource(dt3)
            rpt.Subreports.Item("Subreport4").SetDataSource(dt4)
            rpt.Subreports.Item("Subreport5").SetDataSource(dt5)
            With frm
                frm.CrystalReportViewer1.ReportSource = rpt
                frm.ShowDialog()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
Posted
Comments
Richard Deeming 7-Sep-18 9:45am    
That would suggest that mainreport doesn't contain a sub-report called Subreport1. But you'll need to debug your code to check that - we don't have access to your system, so we can't debug it for you.
Rodelio Gucela Jr. 8-Sep-18 8:05am    
Hi Sir Richard, thank you for replying. I am willing to give the system to you, if you are willing to debug it. I am having a hard time with that part, I couldn't fix it. Please help me sir. Thank you very much!
Rodelio Gucela Jr. 9-Sep-18 9:38am    
Nevermind sir, I was able to fixed the problem :) you're right. It should be patreport.rpt instead of subreport1 thank you very much sir!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900