Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > How to check if CType(e.Item.DataItem, DataRowView) is DBNULL

Reply
Thread Tools

How to check if CType(e.Item.DataItem, DataRowView) is DBNULL

 
 
Rob Wire
Guest
Posts: n/a
 
      07-29-2003
How do you check if CType(e.Item.DataItem, DataRowView) is
DBNULL?

I have a Datagrid with Edit,Update, and Insert buttons to
a SQL data table. During an update, a drop down list is
used for one of the fields which defaults to current value
in a row.

Private Sub DataGrid1_ItemDataBound(ByVal sender As
Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim drv As DataRowView = CType
(e.Item.DataItem, DataRowView)
Dim currentAttachType As String

currentAttachType = CType(drv("ATTACH_TYPE"),
String)

Dim ddl As DropDownList
ddl = CType(e.Item.FindControl
("DropDownList1"), DropDownList)
ddl.SelectedIndex = ddl.Items.IndexOf
(ddl.Items.FindByText(currentAttachType))
End If
End Sub

Insert fails with an error "Cast from type 'DBNull' to
type 'String' is not valid" if this function is used.
Insert button on click uses the regular update function as
well.

I think an if statement on the drv("ATTACH_TYPE") to check
if it is a DBNULL or not might fix it. How might you do
this?

Any suggestions?

Thank you,

Rob Wire
 
Reply With Quote
 
 
 
 
andrei
Guest
Posts: n/a
 
      07-30-2003
Rob,

currentAttachType = iif(IsDBNull(drv("ATTACH_TYPE")), _

"", _

CType(drv("ATTACH_TYPE"), String))

HTH,

Andrei.



"Rob Wire" <(E-Mail Removed)> wrote in message
news:03a701c35604$59912c10$(E-Mail Removed)...
> How do you check if CType(e.Item.DataItem, DataRowView) is
> DBNULL?
>
> I have a Datagrid with Edit,Update, and Insert buttons to
> a SQL data table. During an update, a drop down list is
> used for one of the fields which defaults to current value
> in a row.
>
> Private Sub DataGrid1_ItemDataBound(ByVal sender As
> Object, ByVal e As
> System.Web.UI.WebControls.DataGridItemEventArgs) Handles
> DataGrid1.ItemDataBound
> If e.Item.ItemType = ListItemType.EditItem Then
> Dim drv As DataRowView = CType
> (e.Item.DataItem, DataRowView)
> Dim currentAttachType As String
>
> currentAttachType = CType(drv("ATTACH_TYPE"),
> String)
>
> Dim ddl As DropDownList
> ddl = CType(e.Item.FindControl
> ("DropDownList1"), DropDownList)
> ddl.SelectedIndex = ddl.Items.IndexOf
> (ddl.Items.FindByText(currentAttachType))
> End If
> End Sub
>
> Insert fails with an error "Cast from type 'DBNull' to
> type 'String' is not valid" if this function is used.
> Insert button on click uses the regular update function as
> well.
>
> I think an if statement on the drv("ATTACH_TYPE") to check
> if it is a DBNULL or not might fix it. How might you do
> this?
>
> Any suggestions?
>
> Thank you,
>
> Rob Wire



 
Reply With Quote
 
 
 
 
Rick Rainey[MSFT]
Guest
Posts: n/a
 
      07-30-2003
Hi Rob,

You can use the IsDbNull function to determine if the value is null.

For Example,

If Not IsDbNull(drv) Then
'Code to handle this situation.
End If

Hope this helps,

Rick[MSFT]
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
check for dbNull is throwing an error =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= ASP .Net 1 01-16-2006 08:30 PM
Public member 'length' on type 'DBNull' not found. dave ASP .Net 1 11-26-2003 06:59 PM
Adding a DBNull value to the database Boris Zakharin ASP .Net 0 08-27-2003 12:53 AM
Question: Best way to handle DBNULL in datareaders Ravikanth[MVP] ASP .Net 6 07-18-2003 10:51 AM



Advertisments