Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 36

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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 

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 1000 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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