Solved

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

Posted on 2007-12-05
13
883 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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