473,657 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2420
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.IsDBNul l 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.goo glegroups.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(ro w("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
3794
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 got to write them in a bmp file (in which RGB values are limited to 8 bits per channel). so i need to have them scaled down. first of all i find the highest and lowest of those values:(keep in mind that max, min, bmp, ptr are ulong and w, h are...
6
2090
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
2480
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 in any way?. Because I dont remember seeing something like 'long float ' anywhere and only 'long
7
6261
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 (WhatCol.Type <> 5 AND WhatCol.Type <> 6) Then Line 176: If rownumber = 3 Then Line 177: thisfield = WhatCol.Value
4
3368
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 separate answer for each version of the language. Let's consider the conversion from a given floating real type to a specific integer type.
2
7028
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 fiilled, but gives the error: "Conversion from type 'DBNull' to type 'String' is not valid" when not. ..... comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
4
6225
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 'DBNull' to type 'Date' is not valid. <asp:Calendar ID="Calendar2" runat="server" SelectedDate='<%# Bind("DateReceived") %>' VisibleDate='<%# Eval("DateReceived") %>'></asp:Calendar>
3
3241
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 get: Conversion from type 'DBNull' to type 'String' is not valid. I know the value of the dgFamilyMember.CurrentRow.Cells(3).Value= null but the first part of the iif returns false! What is wrong here?
3
2291
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
8845
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
8743
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
8522
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,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4173
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...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
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.