473,698 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datatable retrieving Null values - I'm stuck !

Hi there

I have a stored procedure on my SQL database that retrieves a wide range of
values from about 5 different tables. My end point is to calculate the cost
against each line of retrieved data. Depending upon the contents of a
particular field that cost calculation changes....

I retrieve the data in to a dataset and subsequently in to a datatable -
fine so far...

I then programatically add the "Cost" column and am trying to put data in to
it. The field that decides how I calculate the cost can have a value of NULL,
1, 2, 3 or 4 - this field is called "TRANSACTION_TY PE". I need to loop
through all the rows that are retrievd calculate the Cost column depending
upon what's in "TRANSACTION_TY PE".

I am trying to use an IF statement in the code as follows:

Dim i As Integer
For i = 0 To dt3.Rows.Count - 1
If dt3.Rows(i).Ite m("TRANSACTION_ TYPE").IsNull Then
dt3.Rows(i).Ite m("Cost") = 0
ElseIf dt3.Rows(i).Ite m("TRANSACTION_ TYPE") = 1 Then
dt3.Rows(i).Ite m("Cost") = 1
Else
dt3.Rows(i).Ite m("Cost") = 2
End If

Next

Obviously in the above the Cost calculation is omitted and replaced with a
simple integer for the purposes of testing...

My problem is the first IF statement - how do I identify the contents of the
"TRANSACTION_TY PE" as NULL - the above is one effort which fails with
various different messages such as:

Exception Details: System.MissingM emberException: Public member 'IsNull' on
type 'DBNull' not found.

OR

Exception Details: System.MissingM emberException: Public member 'IsNull' on
type 'Short' not found.

I have tried using:
If dt3.Rows(i).Ite m("TRANSACTION_ TYPE") Is Nothing Then

but the two lots of data I have retrieved to gain the first two errors then
show the same error:

Exception Details: System.InvalidC astException: Operator is not valid for
type 'DBNull' and string "1".

It obviously skips the first "if... is nothing" statement and fails on the
if = 1 statement... I get the impression that my "Is nothing" statement is
not matching the NULL fields in the first place

Hope this makes sense - basically - how do I reference a NULL value in the
above scenario ???

Thanks for your help

Stuart

Nov 19 '05 #1
2 3993
Stuart,
Your biggest problem is probably the fact that a null in a
programming language is not necessarily the same type of null used in a
database. Instead of testing for null as you are, see if the item in the row
is equal to System.DbNull.V alue instead. This will then compare it to the
null value returned by the database.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Stuart" <St****@discuss ions.microsoft. com> wrote in message
news:7C******** *************** ***********@mic rosoft.com...
Hi there

I have a stored procedure on my SQL database that retrieves a wide range
of
values from about 5 different tables. My end point is to calculate the
cost
against each line of retrieved data. Depending upon the contents of a
particular field that cost calculation changes....

I retrieve the data in to a dataset and subsequently in to a datatable -
fine so far...

I then programatically add the "Cost" column and am trying to put data in
to
it. The field that decides how I calculate the cost can have a value of
NULL,
1, 2, 3 or 4 - this field is called "TRANSACTION_TY PE". I need to loop
through all the rows that are retrievd calculate the Cost column depending
upon what's in "TRANSACTION_TY PE".

I am trying to use an IF statement in the code as follows:

Dim i As Integer
For i = 0 To dt3.Rows.Count - 1
If dt3.Rows(i).Ite m("TRANSACTION_ TYPE").IsNull Then
dt3.Rows(i).Ite m("Cost") = 0
ElseIf dt3.Rows(i).Ite m("TRANSACTION_ TYPE") = 1 Then
dt3.Rows(i).Ite m("Cost") = 1
Else
dt3.Rows(i).Ite m("Cost") = 2
End If

Next

Obviously in the above the Cost calculation is omitted and replaced with a
simple integer for the purposes of testing...

My problem is the first IF statement - how do I identify the contents of
the
"TRANSACTION_TY PE" as NULL - the above is one effort which fails with
various different messages such as:

Exception Details: System.MissingM emberException: Public member 'IsNull'
on
type 'DBNull' not found.

OR

Exception Details: System.MissingM emberException: Public member 'IsNull'
on
type 'Short' not found.

I have tried using:
If dt3.Rows(i).Ite m("TRANSACTION_ TYPE") Is Nothing Then

but the two lots of data I have retrieved to gain the first two errors
then
show the same error:

Exception Details: System.InvalidC astException: Operator is not valid for
type 'DBNull' and string "1".

It obviously skips the first "if... is nothing" statement and fails on the
if = 1 statement... I get the impression that my "Is nothing" statement is
not matching the NULL fields in the first place

Hope this makes sense - basically - how do I reference a NULL value in the
above scenario ???

Thanks for your help

Stuart

Nov 19 '05 #2
Spot on !

Thanks very much for your help Mark

"Mark Fitzpatrick" wrote:
Stuart,
Your biggest problem is probably the fact that a null in a
programming language is not necessarily the same type of null used in a
database. Instead of testing for null as you are, see if the item in the row
is equal to System.DbNull.V alue instead. This will then compare it to the
null value returned by the database.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Stuart" <St****@discuss ions.microsoft. com> wrote in message
news:7C******** *************** ***********@mic rosoft.com...
Hi there

I have a stored procedure on my SQL database that retrieves a wide range
of
values from about 5 different tables. My end point is to calculate the
cost
against each line of retrieved data. Depending upon the contents of a
particular field that cost calculation changes....

I retrieve the data in to a dataset and subsequently in to a datatable -
fine so far...

I then programatically add the "Cost" column and am trying to put data in
to
it. The field that decides how I calculate the cost can have a value of
NULL,
1, 2, 3 or 4 - this field is called "TRANSACTION_TY PE". I need to loop
through all the rows that are retrievd calculate the Cost column depending
upon what's in "TRANSACTION_TY PE".

I am trying to use an IF statement in the code as follows:

Dim i As Integer
For i = 0 To dt3.Rows.Count - 1
If dt3.Rows(i).Ite m("TRANSACTION_ TYPE").IsNull Then
dt3.Rows(i).Ite m("Cost") = 0
ElseIf dt3.Rows(i).Ite m("TRANSACTION_ TYPE") = 1 Then
dt3.Rows(i).Ite m("Cost") = 1
Else
dt3.Rows(i).Ite m("Cost") = 2
End If

Next

Obviously in the above the Cost calculation is omitted and replaced with a
simple integer for the purposes of testing...

My problem is the first IF statement - how do I identify the contents of
the
"TRANSACTION_TY PE" as NULL - the above is one effort which fails with
various different messages such as:

Exception Details: System.MissingM emberException: Public member 'IsNull'
on
type 'DBNull' not found.

OR

Exception Details: System.MissingM emberException: Public member 'IsNull'
on
type 'Short' not found.

I have tried using:
If dt3.Rows(i).Ite m("TRANSACTION_ TYPE") Is Nothing Then

but the two lots of data I have retrieved to gain the first two errors
then
show the same error:

Exception Details: System.InvalidC astException: Operator is not valid for
type 'DBNull' and string "1".

It obviously skips the first "if... is nothing" statement and fails on the
if = 1 statement... I get the impression that my "Is nothing" statement is
not matching the NULL fields in the first place

Hope this makes sense - basically - how do I reference a NULL value in the
above scenario ???

Thanks for your help

Stuart


Nov 19 '05 #3

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

Similar topics

2
8478
by: Ricardo Luceac | last post by:
HI all.. I have a huge table that I want to display in a datagrid, the problem is that if I make a dataset, the entire table must go to the dataset to the data begin to display, and it takes much time... I need to start show data after a row pushed to the dataset, so I think of using the datareader to populate a datatable and bind the datatable to the datagrid.
13
3955
by: RHPT | last post by:
I am wanting to capture the XML posted by an InfoPath form with .NET, but I cannot figure out how to capture the XML stream sent back by the InfoPath form. With Classic ASP, I could just create an MSXML object then load the XML with a simple objXML.Load(Request). This would give me the XML stream, which I could then manipulate. However, using System.Xml, this is not possible - not that I have found, anyway. The InfoPath documentation...
3
5404
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some example first:
6
19969
by: AlveenX | last post by:
Hi, I am trying to pick a Guid from a data row using the following code: foreach(DataRow row in MyDataTable.Rows) { (Guid)row }
9
12018
by: jsoques | last post by:
Hello, I created a Web Service using .Net 2.0 that has a function that returns a DataTable. I can test the function from the web page when I access the .asmx from a browser on localhost and it works. I can also test the function using VB6 and the xmlhttp activex object. The problem I have now is when using VS 2005 or VB.Net 2005 Express and creating a web references is that the proxy created doesn't map the function as returning a DataTable...
5
5053
by: gbattine | last post by:
Hi guys, i've a very important question for you,i'm stopped my work from 10 days to solve it,but nothing.... i hope your can help me. I'm developing a jsf application and i've created a datatable with empty fields, so each row of my datatable is an input row...i've associated a button to each row and cliccking them i retrieve values inserted by user(for each row) and i put them into a database. What i want now is avoiding to use a button for...
15
3528
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS AS IDENTITY ( START WITH 1
9
9916
by: ajos | last post by:
Hello friends, After thinking about this for sometime, i decided to post this in the java forum. Well my problem here in detail is, i have 3 jsp pages where in a.jsp(for example) i have a combo box, based on the selection of the value in that combo box, b.jsp page gets populated with the value associated to a.jsp, and again based on the selection of value of the 2nd combobox my 3rd combo box gets populated, which is in c.jsp. I'm able to...
3
1164
by: preethadotnet | last post by:
Hi, Can anyone tell me how to retrieve record values from a datatable?.My requirement is like: Within a for loop I am executing different stored procedures and storing records in a datatable so that i cannot specify paticular column names.i want to write the first row values of each result into a log file.(application is windows service) Code is : DataTable dtTransID = new DataTable(); DBConnect(); string sp=" exec...
0
8598
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
9014
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
8885
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
8855
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...
1
6515
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
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
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
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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.