Select to view content in your preferred language

Export To Excel - Null Reference Error

2171
10
Jump to solution
08-03-2012 01:30 PM
GreggBreton
Occasional Contributor
Hello,

I downloaded the following solution file
http://forums.arcgis.com/threads/45538-How-to-make-Export()-is-work-with-ArcGIS-API-for-Silverlight!


Export.zip
QueryWithoutMap.zip

Executed and  queried the default "New" clicked the export button and works fine
Then tried "S", returned records and tried to export and received "Null Reference Exception"

Use the new keyword to create an object instance
Check to determine if the object is null before calling the method


  private static string GetValue(FrameworkElement frameworkElement)
        {
            return frameworkElement is TextBlock ? (frameworkElement as TextBlock).Text : frameworkElement.ToString();
        }

If someone could illustrate how the above would be written to avoid the null exception error

Thank you
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
Try:

private static string GetValue(FrameworkElement frameworkElement) {     return frameworkElement == null ? null          :( frameworkElement is TextBlock ? (frameworkElement as TextBlock).Text : frameworkElement.ToString()); }

View solution in original post

0 Kudos
10 Replies
deleted-user-ATjHIWsdQYmT
Deactivated User
Gregg-
Try:

private static string GetValue(FrameworkElement frameworkElement){ return frameworkElement is TextBlock & (frameworkElement != null) ? (frameworkElement as TextBlock).Text : frameworkElement.ToString();}


This should trap if your frameworkElement is NOT null
0 Kudos
GreggBreton
Occasional Contributor
Andrew -

I still receive the same Null Exception?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Try:

private static string GetValue(FrameworkElement frameworkElement) {     return frameworkElement == null ? null          :( frameworkElement is TextBlock ? (frameworkElement as TextBlock).Text : frameworkElement.ToString()); }
0 Kudos
JoeHershman
MVP Alum
Nothing incorrect about the above code but I think this is a bit easier to read and I think should work.


        private static string GetValue(FrameworkElement frameworkElement)
        {
            var textBlock = frameworkElement as TextBlock;
            if ( textBlock == null ) return null;


            return textBlock.Text;
        }

Thanks,
-Joe
0 Kudos
GreggBreton
Occasional Contributor
Yes, that did the trick

It allowed me to download the file, but when I open the csv file it displays the following:

File is in a different file format than specified by the extension

[ATTACH=CONFIG]16753[/ATTACH]

Excel has detected that the file is a SYLK file, but cannot load it, either the file has errors or is not in SYLK format

[ATTACH=CONFIG]16755[/ATTACH]

After the above messages, the file opens and looks fine.

Would the errors be caused by the REST service data not being in correct format? The script trying to conform to the csv format? or something else that you may have run into..

Thank you Dominique
0 Kudos
GreggBreton
Occasional Contributor
Yes, that did the trick

It allowed me to download the file, but when I open the csv file it displays the following:

File is in a different file format than specified by the extension

[ATTACH=CONFIG]16756[/ATTACH]

Excel has detected that the file is a SYLK file, but cannot load it, either the file has errors or is not in SYLK format

[ATTACH=CONFIG]16757[/ATTACH]

After the above messages, the file opens and looks fine.

Would the errors be caused by the REST service data not being in correct format? The script trying to conform to the csv format? or something else that you may have run into..

Thank you Dominique
0 Kudos
deleted-user-ATjHIWsdQYmT
Deactivated User
Yes, that did the trick

It allowed me to download the file, but when I open the csv file it displays the following:

File is in a different file format than specified by the extension

[ATTACH=CONFIG]16756[/ATTACH]

Excel has detected that the file is a SYLK file, but cannot load it, either the file has errors or is not in SYLK format

[ATTACH=CONFIG]16757[/ATTACH]

After the above messages, the file opens and looks fine.

Would the errors be caused by the REST service data not being in correct format? The script trying to conform to the csv format? or something else that you may have run into..

Thank you Dominique


Gregg,
Check out this link:
http://support.microsoft.com/kb/323626

If your first  field name is "ID", try trapping it in your export code by changing it to "id" or adding an apostraphe like the article says.  Another workaround would be to hide the "ID" field from your map service unless it's necessary.
0 Kudos
GreggBreton
Occasional Contributor
I will follow up on thread and give it a try


Thank you
0 Kudos
deleted-user-ATjHIWsdQYmT
Deactivated User
I will follow up on thread and give it a try


Thank you


Gregg, Quick solution - change the Header text of the first column from "ID" to something else.  It'll solve the problem.
0 Kudos