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
andrei - 30 Jul 2003 17:00 GMT
Rob,
currentAttachType = iif(IsDBNull(drv("ATTACH_TYPE")), _
"", _
CType(drv("ATTACH_TYPE"), String))
HTH,
Andrei.
> How do you check if CType(e.Item.DataItem, DataRowView) is
> DBNULL?
[quoted text clipped - 38 lines]
>
> Rob Wire
Rick Rainey[MSFT] - 30 Jul 2003 17:24 GMT
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.