473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Correct use of casting and DataBinder.Eval

It’s Friday afternoon and I’m being driven mad by a simple .NET problem. Can you help?


I’m binding in a repeater where I know there’s a possibility that the value for a field in a dataset could be null. The field is an integer.

So in ItemDataBound I’m doing this...

Int? myInt = (int?)DataBinder.Eval( Container.DataItem, “myIntField”);

This comes up with an invalid cast exception, as does this...

Int myInt = (int)DataBinder.Eval( Container.DataItem, “myIntField”);


My cop out is to do this....

String strTemp = DataBinder.Eval( Container.DataItem, “myIntField”, “{0}” ); // this returns a string

myInt = (strTemp != string.empty) ? Int.Parse( strTemp ) : 0; // or variations on this using Convert or Int.TryParse


There has to be a simpler way?

Sphengle
Oct 3 '08 #1
3 3585
Frinavale
9,735 Expert Mod 8TB
Since the DataBinder.Eval Method evaluates data-binding expressions at run time you will get an ArgumentNullException if the value is null (or nothing).

You're going to have to do what you have outlined above: determine if the value is null, then set it to a default value if it is null before you use the DataBinder.Eval method.

-Frinny
Oct 3 '08 #2
Hi Frinny

Do you know if there is a way of testing for null without having to format to a string first?

Sphengle
Oct 8 '08 #3
Frinavale
9,735 Expert Mod 8TB
Hi Frinny

Do you know if there is a way of testing for null without having to format to a string first?

Sphengle
You can use the String class's static method String.IsNullOrEmpty to check if a string is null or empty. Note that if the string contains a space, it is not null or empty...so check if it's null or empty first, then trim the string and check it again if you consider space empty as well.

-Frinny
Oct 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
2
by: dm_dal | last post by:
I have a control on my webform that I am binding to a dataset. The issue is, the field value in the dataset is encrypted and I am trying to decrypt it during the binding process: Example: ...
4
by: Søren Lund | last post by:
Hello, I am trying to bind a DataGrid with some data from a DataSet which contains fields in the form "group.fieldname". I am certain that my data source contains the field but I cannot get my...
1
by: Mike Lerch | last post by:
Pretty much, TSIA, but I'll expand a bit: I'm still trying to get my head around the n-tier approach to web design. It seems to me that when you use DataBinder.Eval in the ASPX that your tiers...
3
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
1
by: fabrice | last post by:
Hello I have a little problem with casting in a Datagrid Control. I'm using Option Strict in my web application. In a Template Column, I put in an ImageButton Control with a Command...
4
by: CharlesA | last post by:
a general question... I'm a bit mystified by Databinder.eval(object, Colname, ) if controls can be bound to their individual data sources within the load event of the page... why would we ever...
7
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
0
by: bnlockwood | last post by:
G'day all, Thank you in advance for anyone that can help. I have a shopping cart datagrid on my c# asp.net page. This is the code: ------------------------------------- <asp:datagrid...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.