what do i do if it's null

G

Guest

hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21));

If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

thanks,
rodchar
 
M

Mark Rae

hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21));

If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

if (!<some data value> is DBNull)
{
// proceed
}
 
G

Guest

rodchar said:
hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21));

If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

thanks,
rodchar

Use the IsNull method:

byte[] imageData;
DataRow row = ((DataRowView)fvEmployeeMaster.DataItem).Row;
if (row.IsNull(21) {
imageData = null;
} else {
imageData = (byte[])row[21]);
}

By the way:

Don't use the ItemArray property to access a single item. It creates an
array of all items every time that you use it. Use the default indexer
instead, i.e. row[21] instead of row.ItemArray[21].

You don't have to use the GetValue method to get an item out of an
array. Just use the default indexer, i.e. array(21) instead of
array.GetValue(21).
 
G

Guest

Göran Andersson said:
Just use the default indexer, i.e. array(21) instead of
array.GetValue(21).

The indexer of course uses brackets, not parenteses. array[21].
 
G

Guest

thank you for the extra tip.

Göran Andersson said:
rodchar said:
hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21));

If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

thanks,
rodchar

Use the IsNull method:

byte[] imageData;
DataRow row = ((DataRowView)fvEmployeeMaster.DataItem).Row;
if (row.IsNull(21) {
imageData = null;
} else {
imageData = (byte[])row[21]);
}

By the way:

Don't use the ItemArray property to access a single item. It creates an
array of all items every time that you use it. Use the default indexer
instead, i.e. row[21] instead of row.ItemArray[21].

You don't have to use the GetValue method to get an item out of an
array. Just use the default indexer, i.e. array(21) instead of
array.GetValue(21).
 
S

SAL

Anybody know what to do in the case where databinding is being utilized as
in the following like of markup?
StatuteRequirementsMet:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# Bind("StatuteRequirementsMet") %>' />

I'm getting the same error only for a boolean type.

S
 
D

David Wier

How about adding a 'Helper Function' to the mix?

In a function (maybe called) CheckNull

Function CheckNull(sItem) as boolean
if not sItem is system.dbnull.value then
Return true
else
Return false
end if
end Function

Then,
Checked='<%# CheckNull(Bind("StatuteRequirementsMet")) %>'

This is all OTTOMH, so it might need some tweaking here and there....

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com



SAL said:
Anybody know what to do in the case where databinding is being utilized as
in the following like of markup?
StatuteRequirementsMet:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# Bind("StatuteRequirementsMet") %>' />

I'm getting the same error only for a boolean type.

S

rodchar said:
hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21)
);
If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

thanks,
rodchar
 
S

SAL

Hi David,
I like the idea but am not sure where to put this great function. Can I just
put it in the code behind or do I have to put it in the asp code?

S

David Wier said:
How about adding a 'Helper Function' to the mix?

In a function (maybe called) CheckNull

Function CheckNull(sItem) as boolean
if not sItem is system.dbnull.value then
Return true
else
Return false
end if
end Function

Then,
Checked='<%# CheckNull(Bind("StatuteRequirementsMet")) %>'

This is all OTTOMH, so it might need some tweaking here and there....

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com



SAL said:
Anybody know what to do in the case where databinding is being utilized
as
in the following like of markup?
StatuteRequirementsMet:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# Bind("StatuteRequirementsMet") %>' />

I'm getting the same error only for a boolean type.

S

rodchar said:
hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21)
);
If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

thanks,
rodchar
 
D

David Wier

Put it in the Code behind

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com


SAL said:
Hi David,
I like the idea but am not sure where to put this great function. Can I just
put it in the code behind or do I have to put it in the asp code?

S

David Wier said:
How about adding a 'Helper Function' to the mix?

In a function (maybe called) CheckNull

Function CheckNull(sItem) as boolean
if not sItem is system.dbnull.value then
Return true
else
Return false
end if
end Function

Then,
Checked='<%# CheckNull(Bind("StatuteRequirementsMet")) %>'

This is all OTTOMH, so it might need some tweaking here and there....

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com



SAL said:
Anybody know what to do in the case where databinding is being utilized
as
in the following like of markup?
StatuteRequirementsMet:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# Bind("StatuteRequirementsMet") %>' />

I'm getting the same error only for a boolean type.

S

hey all,

Byte[] imageData =
(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21)
);
If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get around
this?

thanks,
rodchar
 
S

SAL

When I did that, I got an error:
BC30451: Name 'Bind' is not declared

I added the CheckNullBool method to the code behind:

Public Function CheckNullBool(ByVal item As Object) As Boolean
If Not item Is System.DBNull.Value Then
Return True
Else
Return False
End If
End Function

And changed the markup to:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# CheckNullBool(Bind("StatuteRequirementsMet")) %>' /><br />

S

David Wier said:
Put it in the Code behind

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com


SAL said:
Hi David,
I like the idea but am not sure where to put this great function. Can I just
put it in the code behind or do I have to put it in the asp code?

S

David Wier said:
How about adding a 'Helper Function' to the mix?

In a function (maybe called) CheckNull

Function CheckNull(sItem) as boolean
if not sItem is system.dbnull.value then
Return true
else
Return false
end if
end Function

Then,
Checked='<%# CheckNull(Bind("StatuteRequirementsMet")) %>'

This is all OTTOMH, so it might need some tweaking here and there....

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com



Anybody know what to do in the case where databinding is being
utilized
as
in the following like of markup?
StatuteRequirementsMet:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# Bind("StatuteRequirementsMet") %>' />

I'm getting the same error only for a boolean type.

S

hey all,

Byte[] imageData =

(Byte[])(((DataRowView)fvEmployeeMaster.DataItem).Row.ItemArray.GetValue(21)
);

If the record coming in is null i get the following error message:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

is there a shorthand or what conditional statement do i need to get
around
this?

thanks,
rodchar
 
J

John Saunders

SAL said:
Anybody know what to do in the case where databinding is being utilized as
in the following like of markup?
StatuteRequirementsMet:
<asp:CheckBox ID="StatuteRequirementsMetCheckBox" runat="server"
Checked='<%# Bind("StatuteRequirementsMet") %>' />

What to do depends on what your DBA means when a NULL is in this
StatuteRequirementsMet column. If it means "we don't know whether the
requirements have been met or not", then it's not necessarily appropriate to
set the check box either to true or false - NULL doesn't mean either of
those.

Do you write the checkbox value back into the database? If so, then when the
database field was NULL and your user doesn't change the checkbox, then
you'll be writing "false" (0?) back into the database, which is not correct.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top