Solved

export to excel from datagrid - object reference not set to an instance of an object - datagrid is null

Posted on 2010-09-15
6
470 Views
Last Modified: 2013-11-26
i have this code
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        '// Disable paging
        GridView1.AllowPaging = False

        '// exluded columns arraylist
        Dim defaultExcludedColumns As New ArrayList()

        '// Always exclude these columns
        defaultExcludedColumns.Add("")

        '// Send to base Excel export method
        ExportGridView(GridView1, "Excel-Name", defaultExcludedColumns)

        '// Rebind with paging enabled
        GridView1.AllowPaging = True
    End Sub

before calling this method ExportGridView need to validate that the GridView1 has at least one row

exportarERRORgridNULL.jpg
0
Comment
Question by:enrique_aeo
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
Comment Utility
Can you tell me if gridView  or gridView.HeaderRow is not null? (Put a breakpoint in that line and inspect the values)
If so, your problem is that you are overriding the response, thus the gridview lost its contents.
0
 

Author Comment

by:enrique_aeo
Comment Utility
THE GRIDVIEW IS NOTHING, VIEW FILE
but I need to validate if the datagrid has data before calling
 ExportGridView (GridView1, "Excel" Name ", defaultExcludedColumns)
gridNULL.jpg
0
 
LVL 7

Expert Comment

by:klakkas
Comment Utility
The problem is in your If statement.

When you say (reading from your screenshot):
If grdView.HeaderRow isNot Nothing And GrdView.HeaderRow.Cells IsNot Nothing Then

the program actually checks BOTH statements in the IF clause. But, if HeaderRow is nothing, then HeaderRow.Cells causes the "Object not referenced to an instance of an object).

What you need to do is to use AndAlso, specifically:
If grdView.HeaderRow isNot Nothing AndAlso GrdView.HeaderRow.Cells IsNot Nothing Then

With this syntax, the code will check the first and only if it is true, it will move to the second. So, If HeaderRow is Nothing, it will never check for HeaderRow.Cells and no error will be raised.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:enrique_aeo
Comment Utility
I was thinking of something like this
If (gvwQryInscripciones.Rows.Count <> 0) Then
            ExportGridView(gvwQryInscripciones, "Excel-Name", defaultExcludedColumns)
End If
you think?
0
 
LVL 7

Accepted Solution

by:
klakkas earned 250 total points
Comment Utility
That would work too, since the Rows collection exists always (would not produce an error).

0
 

Author Closing Comment

by:enrique_aeo
Comment Utility
it is OK
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

10 Experts available now in Live!

Get 1:1 Help Now