Solved

How do I check to see if a data bound item is null without throwing exception

Posted on 2007-12-05
13
872 Views
Last Modified: 2008-02-01
I currently have the following scenario i.e. I'msetting a variable on the item data bound event of a grid - however am getting an exception because the original data source that it's bound to i.e. of type XmlDataSourceNodeDescriptor' does not exist and I get the following error returned to me.

How do I check for null / nothing without throwing an exception and doing a very dirty try catch around it.

DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'Address3'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'Address3'.


If Not IsNothing(DataBinder.Eval(e.Item.DataItem, "Address3")) Then

        Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")

End If

Open in new window

0
Comment
Question by:paulCardiff
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 12

Expert Comment

by:needo_jee
ID: 20410488
seems the column "Address3" is not present in the target datasource. hence you are getting exception for that.
0
 

Author Comment

by:paulCardiff
ID: 20410497
agreed - the thing i need to know is how do i checked for this in the code?
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 20410498
If Not (DataBinder.Eval(e.Item.DataItem, "Address3")) Is Nothing Then
        Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")
End If
0
 
LVL 8

Expert Comment

by:Autoeforms
ID: 20410520
i have created a class that has functions for doing this.  
here is the sample of the string version. Pass your e.item.dataitem for scdata.  if null you will get back an empty string or the value you supplie.

i have the same function for integers, dates etc.  i have found it makes the code way more readable.

greg


    Public Shared Function sTestDBNull(ByVal scData As Object, _

                                       Optional ByVal sDefault As String = aef_gc_ReturnValues.sEmpty) As String
 

        If IsDBNull(scData) Then Return sDefault

        If IsNothing(scData) Then Return sDefault

        Return CType(scData, String)

    End Function

Open in new window

0
 

Author Comment

by:paulCardiff
ID: 20410566
Sorry but i dont think these will work i.e. in my code sample you see that i'm doing the following

1-       If Not IsNothing(DataBinder.Eval(e.Item.DataItem, "Address3")) Then
2-                    Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")
3-       End If

If erroring on line 1 and this is my problem i..e. how do i check if a value is nothing when the only way i get to that value throws an error?
0
 

Author Comment

by:paulCardiff
ID: 20410596
* If erroring on line 1 = Its erroring on line 1
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 48

Expert Comment

by:jpaulino
ID: 20410608
Have you test it

If Not IsDBNull(DataBinder.Eval(e.Item.DataItem, "Address3")) Then
  Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")
End If
0
 

Author Comment

by:paulCardiff
ID: 20410735
Yes i have please find error below.
DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'Address3'. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 

Exception Details: System.Web.HttpException: DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'Address3'.
 

Source Error: 
 
 

Line 83: 

Line 84:             

Line 85: If Not IsDBNull(DataBinder.Eval(e.Item.DataItem, "Address3")) Then

Line 86:Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")

Line 87:End If

 

Open in new window

0
 
LVL 19

Accepted Solution

by:
DreamMaster earned 500 total points
ID: 20410765
The problem is not so much that it is null or nothing...

The problem he has is that he wants to check whether or not a field even exists in the results he is getting. When it doesn't exist, you won't be able to check that using Is Nothing or IsDBNull.

What you will need to do if you want to make sure it exists is use a query that checks the syscolumns for the existence of your field.

select *
from syscolumns
where id = object_id ('yourtablename')
and name = 'yourcolumnname'

This is however something you can prevent by knowing your database structure. ;) I don't think you are going to want to check every single field this way.. ;)

Regards,
Max.
0
 

Author Comment

by:paulCardiff
ID: 20410812
Agreed this datasource is XML and in the application its perfectly valid not to have an address 2 in it, so is there anyway to use the object XmlDataSourceNodeDescriptor to identify whats fields do exist?
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20410869
I must admit I am not familiar enough with the XmlDataSourceNodeDescriptor to tell you that. If it is valid for different fields to exist in the datasource it would be unwise in my oppinion to use it as a datasource for a gridview. This because a gridview, repeater or any other of the standard controls sort of expect a similar layout. Probably the easiest way to overcome this is to alter the way the XML gets created so that the fields you need are always there. They can be empty, but they'd need to be there.

Otherwise i'd choose a completely different approach, perhaps even one where the resultset is written out to the browser by hand. May take a lot more time, but offers for a lot more flexibility.

Regards,
Max.
0
 

Author Comment

by:paulCardiff
ID: 20411211
I'll be honest and say i came to that conclusion myself - but the lazy developer in me wanted an easier way out of this :-)

I'm sure there is a way to do this but in the interests of time i'll just rewrite the datasource

Thanks all
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20411311
Glad to have been helpful ;)

Oh and by the way, there is nothing wrong with being lazy as a developer.

Regards,
Max.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Setting runtime form location 4 19
Create XML 5 34
COnsume rest client 6 15
Birthdays 3 21
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

14 Experts available now in Live!

Get 1:1 Help Now