473,605 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert.ToDecim al(dr[2]).ToString("#,# #0.00") and NULL

Bob

Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob

Aug 1 '07 #1
6 21792
Bob wrote:
Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob
Use dr.IsDBNull(0) to determine if the value is null before you try to
use the value.

--
Göran Andersson
_____
http://www.guffa.com
Aug 1 '07 #2
Bob <Go****@Yahoo.C omwrote on 8/1/2007 3:05:59 PM
>Hello folks.
I use this formating but if the value in dr[0] is a null it generates
an error
"Object cannot be cast from DBNull to other types."
Anybody know how I can deal with nulls?
Thanks,
Bob
<hr>
Bob,

create your own conversion for the decimal conversion. Convert.ToDecim al throws exceptions for DBNull, null, not-a-number, etc, etc.
It's better to catch those before-hand and in case of failure return 0 (most likely anyway).
You can use regular expressions for checking if it is really a decimal.

Besides that, one other point (assuming dr[2] = DataRow[third column]) you really should consider using data-column names and not indexes.

Leon.
Aug 1 '07 #3

Check for null first..

dr.IsDBNull(2) ? "" : Convert.ToDecim al(dr[2]).ToString("#,# #0.00")

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 01 Aug 2007 06:03:05 -0700, Bob <Go****@Yahoo.C omwrote:
>
Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob
Aug 1 '07 #4
On 01 Aug 2007 13:18:15 GMT, le***********@f rost-nospam-bits.nl wrote:
>Besides that, one other point (assuming dr[2] = DataRow[third column]) you really should consider using data-column names and not indexes.

Leon.
There's a performance difference between using indexes and column
names. It's best not to use the column name based retrieval methods
on a datarow whenever you're retreiving many records.

You can use GetOrdinal(name ) to get the index from a name, store that
in a local variable (outside the read loop) and then use that (inside
the read loop) to retrieve actual data from the reader.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

Aug 1 '07 #5
On top of all the other suggestions here, I have to question you
converting to a decimal, and then back to a string. The only way I can see
this being of use is if the value in dr[2] is a number in string format,
which you want to express in a different format.

Otherwise, the value in dr[2] is a numeric type, and you should be able
to call ToString directly on it, you just have to perform a cast on the
value returned so that you can make the appropriate call:

((decimal) dr[2]).ToString("#,# #0.00");

If the type of dr[2] is something other than decimal, I recommend
casting to that type, since you aren't going to gain anything by casting any
numeric type to decimal, and then formatting the string. If you had to do
further work with it, then I would understand the cast to decimal (for the
need for precision), but that isn't the case here.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob" <Go****@Yahoo.C omwrote in message
news:11******** **************@ l70g2000hse.goo glegroups.com.. .
>
Hello folks.

I use this formating but if the value in dr[0] is a null it generates
an error

"Object cannot be cast from DBNull to other types."

Anybody know how I can deal with nulls?

Thanks,

Bob

Aug 1 '07 #6

Casting data from a data reader can be a problem with numbers
depending on the sql provider. Some providers will return different
data types depending on the particular data being returned (not always
even depending on the declared field type).

It's safer to always use Convert than cast. In case where you're
converting to the class it already is, the performance impact is
negligible (a call to the appropriate interface method which in the
end just does "return this").

Also, Convert.ToDecim al(dr[2]) can be give different results from
dr.GetDecimal(2 ) in some providers.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 1 Aug 2007 11:40:32 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard .caspershouse.c omwrote:
On top of all the other suggestions here, I have to question you
converting to a decimal, and then back to a string. The only way I can see
this being of use is if the value in dr[2] is a number in string format,
which you want to express in a different format.

Otherwise, the value in dr[2] is a numeric type, and you should be able
to call ToString directly on it, you just have to perform a cast on the
value returned so that you can make the appropriate call:

((decimal) dr[2]).ToString("#,# #0.00");

If the type of dr[2] is something other than decimal, I recommend
casting to that type, since you aren't going to gain anything by casting any
numeric type to decimal, and then formatting the string. If you had to do
further work with it, then I would understand the cast to decimal (for the
need for precision), but that isn't the case here.
Aug 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1850
by: Jianwei Sun | last post by:
Hi, guys, If I simply do a double d = 2.0 ,and then s=d.ToString() , it will trim the .0 off and return me 2, is there a good way to keep .0 and return me 2.0. Thanks,
1
6094
by: Steven Spits | last post by:
Hi, Suppose decimal Price contains 12,3. Price.ToString("C2") gives 12,30 ? (yes, I'm located in Europe). What would be the best way to convert this string to a decimal again? Steven - - -
4
4874
by: Larry Smith | last post by:
Once you call "ToString()" for an object, how can you then convert the string back to the original object (assuming you know its type of course)? Also, why does "ToString()" produce one string and "TypeConverter.ConvertToString()" produce yet another (e.g., for a "System.Drawing.Size" object, the former method yields, say, "Width=5, Height=40" and the latter yields "5, 40"). Thanks.
1
8822
by: Marc Gravell | last post by:
Just been debugging a daylight savings glitch... something that interested me: (in a BST locale at 5:14 local time, 4:14 GMT) DateTime.Now.ToString("R") >"Mon, 26 Mar 2007 05:14:34 GMT" However... DateTime.Now.IsDaylightSavingTime() >true
5
70383
by: Bob | last post by:
Hello Folks, I am bring backa data reader dr from the database/ So I have a number of fields such as dr To put this in a table I use "<td>" + dr.ToString() + "</td>" But I want to format the number as mentioned in the subject line.
5
2315
by: piyumi80 | last post by:
hi, i write the following code to get a specific data row from the data set.but it generates the "Object reference not set to an instance of an object.".....error private void showClientListForUser() { string fkeys = new string; fkeys = txtUserName.Text; fkeys = txtUserCompanyNumber.Text;
3
12307
by: Ronald S. Cook | last post by:
Stupid q for the day, but if the user enters 2 in a textbox, I want to display as 2.00. If they enter 2.129 I want it as 2.13. I'm guessing will code in the validated method of the textbox, but not sure how to convert the number. Thanks for the help.
3
2278
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I use Session to save and pass values between pages, (please see the following). It works fine on my computer, but on the server, I launch the main page, wait for 20 minutes, and click a button to launch User.aspx, it seems that it cannot see the current value of Session. There's no problem to launch User.aspx from the main page within 20 minutes. Please help. Thanks. //Users.aspx
0
8007
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7937
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3913
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1546
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1274
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.