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

Discussion in 'ASP .Net Datagrid Control' started by Rob Wire, Jul 29, 2003.

  1. Rob Wire

    Rob Wire Guest

    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
    Rob Wire, Jul 29, 2003
    #1
    1. Advertising

  2. Rob Wire

    andrei Guest

    Rob,

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

    "", _

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

    HTH,

    Andrei.



    "Rob Wire" <> wrote in message
    news:03a701c35604$59912c10$...
    > 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, Jul 30, 2003
    #2
    1. Advertising

  3. 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.
    Rick Rainey[MSFT], Jul 30, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kel Good
    Replies:
    2
    Views:
    13,118
    Kel Good
    Apr 25, 2004
  2. Henri
    Replies:
    0
    Views:
    589
    Henri
    Aug 3, 2004
  3. MattB
    Replies:
    4
    Views:
    21,265
  4. =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    check for dbNull is throwing an error

    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=, Jan 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,807
    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=
    Jan 16, 2006
  5. Keith-Earl
    Replies:
    5
    Views:
    179
Loading...

Share This Page