473,394 Members | 1,971 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,394 software developers and data experts.

conversion from type 'DBNull' to type 'float' is not valid

Hi all,

I'm using a business logic layer as described in this source code :

http://www.asp.net/QuickStart/util/s...ng=C%23+Source

It works very well until I encountered a small problem recently. When
it is time to bind the data from the db to my class representing an
"author" (let's say, for simplicity), what about if my row["au_state"]
was instead : row["au_idstate"] and was returning a NULL value. In my
"Authors class", I'd represent the idstate with an integer instead of a
string. What must I do when I try to assign a null value to an integer
(more precisely, a float in my case) because for now, I get a nasty
error "conversion from type 'DBNull' to type 'float' is not valid.

Then in my gridview, I want to see a blank cell if my float var is
NULL; not a 0 or something else...

Thanks!

ibiza

Jan 27 '06 #1
2 2410
Hi,

you'd need to use some sort of default value suitable for the scenario
(maybe 0 with integers?) since as you noted, they cannot represent null
coming from db (DbNull instance). And when such field is put visible on the
UI, you'd need to set it to empty string in case when value represents this
default value (which points that null came from db)

You could create a small library for yourself to deal with it such that
testing for null (with Convert.IsDBNull etc) wouldn't require repetitive
coding.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"ibiza" <la******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi all,

I'm using a business logic layer as described in this source code :

http://www.asp.net/QuickStart/util/s...ng=C%23+Source

It works very well until I encountered a small problem recently. When
it is time to bind the data from the db to my class representing an
"author" (let's say, for simplicity), what about if my row["au_state"]
was instead : row["au_idstate"] and was returning a NULL value. In my
"Authors class", I'd represent the idstate with an integer instead of a
string. What must I do when I try to assign a null value to an integer
(more precisely, a float in my case) because for now, I get a nasty
error "conversion from type 'DBNull' to type 'float' is not valid.

Then in my gridview, I want to see a blank cell if my float var is
NULL; not a 0 or something else...

Thanks!

ibiza

Jan 27 '06 #2
well, I figured that if I'd put (isdbnull(row["au_idstate"]? null :
row["au_idstate"]))

(in fact : IIf(IsDbNull(row("au_idstate"), Nothing, row("au_idstate"))
in VB.NET)

instead of just row("au_idstate"), I don't get the error anymore and
everything works fine again. The only problem to me is that it binds
nasty "0" in my gridview instead of a nice blank cell....any idea for
that?

Thank a lot!

ibiza

Jan 27 '06 #3

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

Similar topics

8
by: CAFxX | last post by:
i'm writing a program that executes some calculations on a bitmap loaded in memory. these calculation ends up with pixel wth values far over 255, but i need them to be between 0 and 255 since i...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
2
by: sriamar | last post by:
Hello, How does the type conversion work if the expression involves a float and long int? By K&R 2nd Ed i assume float & long -> float & float . But does the 'long' qualifier affect this...
7
by: | last post by:
Source Error: Line 173: sData(rownumber - 1, lcnt) = WhatCol.Value Line 174: End IF Line 175: If (sData(rownumber, lcnt) = sData(rownumber - 1, lcnt)) AND...
4
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a...
2
by: Chris | last post by:
Hi again, I want to read all the records of a table with 2 fields. The problem is that some records have null value in the second field. This code below works when all records have both fields...
4
by: SAL | last post by:
I'm sorry if this has been answered before but I didn't see it in a quick scan of the list. The following code is causing an error when the field is null: Error text: Conversion from type...
3
by: Sep410 | last post by:
Hi all, Please help me in this code: txtLastNameFM.Text = IIf(dgFamilyMember.CurrentRow.Cells(3).Equals(System.DBNull.Value), "", dgFamilyMember.CurrentRow.Cells(3).Value) this is an error I...
3
by: yxxxxy | last post by:
Hi, this is a part of my program code. i want to ask two questions. int time; float rate; float salary; printf("Enter # of hours worked (-1 to end):"); scanf("%d",&time);
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
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...
0
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...
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...

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.