473,395 Members | 1,568 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.

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_TYPE". I need to loop
through all the rows that are retrievd calculate the Cost column depending
upon what's in "TRANSACTION_TYPE".

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).Item("TRANSACTION_TYPE").IsNull Then
dt3.Rows(i).Item("Cost") = 0
ElseIf dt3.Rows(i).Item("TRANSACTION_TYPE") = 1 Then
dt3.Rows(i).Item("Cost") = 1
Else
dt3.Rows(i).Item("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_TYPE" as NULL - the above is one effort which fails with
various different messages such as:

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

OR

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

I have tried using:
If dt3.Rows(i).Item("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.InvalidCastException: 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 3982
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.Value instead. This will then compare it to the
null value returned by the database.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Stuart" <St****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.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_TYPE". I need to loop
through all the rows that are retrievd calculate the Cost column depending
upon what's in "TRANSACTION_TYPE".

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).Item("TRANSACTION_TYPE").IsNull Then
dt3.Rows(i).Item("Cost") = 0
ElseIf dt3.Rows(i).Item("TRANSACTION_TYPE") = 1 Then
dt3.Rows(i).Item("Cost") = 1
Else
dt3.Rows(i).Item("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_TYPE" as NULL - the above is one effort which fails with
various different messages such as:

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

OR

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

I have tried using:
If dt3.Rows(i).Item("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.InvalidCastException: 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.Value instead. This will then compare it to the
null value returned by the database.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Stuart" <St****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.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_TYPE". I need to loop
through all the rows that are retrievd calculate the Cost column depending
upon what's in "TRANSACTION_TYPE".

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).Item("TRANSACTION_TYPE").IsNull Then
dt3.Rows(i).Item("Cost") = 0
ElseIf dt3.Rows(i).Item("TRANSACTION_TYPE") = 1 Then
dt3.Rows(i).Item("Cost") = 1
Else
dt3.Rows(i).Item("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_TYPE" as NULL - the above is one effort which fails with
various different messages such as:

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

OR

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

I have tried using:
If dt3.Rows(i).Item("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.InvalidCastException: 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
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...
13
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...
3
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...
6
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
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...
5
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...
15
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...
9
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...
3
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...
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: 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:
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
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,...
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...
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.