what do i do if it's null

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
}
 
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ö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].
 
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).
 
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
 
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
 
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
 
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
 
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
 
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
 
Back
Top