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
479 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
ID: 33688297
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
ID: 33688514
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
ID: 33689766
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:enrique_aeo
ID: 33692131
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
ID: 33692357
That would work too, since the Rows collection exists always (would not produce an error).

0
 

Author Closing Comment

by:enrique_aeo
ID: 33694213
it is OK
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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