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
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
483 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
How our DevOps Teams Maximize Uptime

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

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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