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

R

Rob Wire

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
 
A

andrei

Rob,

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

"", _

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

HTH,

Andrei.
 
R

Rick Rainey[MSFT]

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.
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top