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

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

paulCardiffAsked:
Who is Participating?
 
DreamMasterConnect With a Mentor Commented:
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
 
Munawar HussainPrincipal Software EngineerCommented:
seems the column "Address3" is not present in the target datasource. hence you are getting exception for that.
0
 
paulCardiffAuthor Commented:
agreed - the thing i need to know is how do i checked for this in the code?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Munawar HussainPrincipal Software EngineerCommented:
If Not (DataBinder.Eval(e.Item.DataItem, "Address3")) Is Nothing Then
        Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")
End If
0
 
AutoeformsCommented:
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
 
paulCardiffAuthor Commented:
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
 
paulCardiffAuthor Commented:
* If erroring on line 1 = Its erroring on line 1
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Have you test it

If Not IsDBNull(DataBinder.Eval(e.Item.DataItem, "Address3")) Then
  Address2 = DataBinder.Eval(e.Item.DataItem, "Address3")
End If
0
 
paulCardiffAuthor Commented:
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
 
paulCardiffAuthor Commented:
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
 
DreamMasterCommented:
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
 
paulCardiffAuthor Commented:
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
 
DreamMasterCommented:
Glad to have been helpful ;)

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

Regards,
Max.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.