Solved

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

Posted on 2007-12-05
13
879 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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