[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

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

0
paulCardiff
Asked:
paulCardiff
  • 6
  • 3
  • 2
  • +2
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
DreamMasterCommented:
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now