problem with checking null or empty value in detailsview

Discussion in 'ASP .Net' started by bob, May 5, 2008.

  1. bob

    bob Guest

    Hi,

    i want to check whether the textbox of the detailsview is not left empty, in
    insert mode.
    I did this:

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    DetailsView1.ItemInserting
    Dim h As String
    h = e.Values("myfield")
    'If h = "" Then
    If h Is Nothing Then
    e.Cancel = True
    Label1.Text = "may not be empty."
    End If
    End Sub

    I tried two ways (with ="" and with is nothing), but in both cases, i always
    get the label text and the event is always cancelled, even when i full some
    value.

    So, what's wrong with this code?

    Thanks
    Bob
     
    bob, May 5, 2008
    #1
    1. Advertising

  2. "bob" <> schrieb
    > Hi,
    >
    > i want to check whether the textbox of the detailsview is not left
    > empty, in insert mode.
    > I did this:
    >
    > Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    > ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)
    > Handles
    > DetailsView1.ItemInserting
    > Dim h As String
    > h = e.Values("myfield")
    > 'If h = "" Then
    > If h Is Nothing Then
    > e.Cancel = True
    > Label1.Text = "may not be empty."
    > End If
    > End Sub
    >
    > I tried two ways (with ="" and with is nothing), but in both cases,
    > i always get the label text and the event is always cancelled, even
    > when i full some value.
    >
    > So, what's wrong with this code?


    First, you should enable Option Strict. The assignment

    h = e.Values("myfield")

    is not valid.

    Then, you can examine the return value of e.Values. Maybe it is
    DBNull.Value. If it is, you can do the comparison:

    Dim Value as object
    value = e.Values("myfield")

    if value is dbnull.value then
    '...
    end if

    If it's any other value, you will be able to see it in variable
    'value', too.


    Armin
     
    Armin Zingler, May 5, 2008
    #2
    1. Advertising

  3. bob

    bob Guest

    Thanks for replying ...
    I tried this:

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    DetailsView1.ItemInserting
    Dim h As Object
    h = e.Values("myfield")
    If h Is DBNull.Value Then
    e.Cancel = True
    Label1.Text = "may not be empty."
    End If
    End Sub

    But now, the event is never cancelled and every value is accepted. In the
    db, i see NULL when rhe input in the textbox was empty.
    I conclude that 'h' is not DBNULL , but how to cancel the event when the
    input is empty?

    Thanks



    "Armin Zingler" <> schreef in bericht
    news:%...
    > "bob" <> schrieb
    >> Hi,
    >>
    >> i want to check whether the textbox of the detailsview is not left
    >> empty, in insert mode.
    >> I did this:
    >>
    >> Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    >> ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)
    >> Handles
    >> DetailsView1.ItemInserting
    >> Dim h As String
    >> h = e.Values("myfield")
    >> 'If h = "" Then
    >> If h Is Nothing Then
    >> e.Cancel = True
    >> Label1.Text = "may not be empty."
    >> End If
    >> End Sub
    >>
    >> I tried two ways (with ="" and with is nothing), but in both cases,
    >> i always get the label text and the event is always cancelled, even
    >> when i full some value.
    >>
    >> So, what's wrong with this code?

    >
    > First, you should enable Option Strict. The assignment
    >
    > h = e.Values("myfield")
    >
    > is not valid.
    >
    > Then, you can examine the return value of e.Values. Maybe it is
    > DBNull.Value. If it is, you can do the comparison:
    >
    > Dim Value as object
    > value = e.Values("myfield")
    >
    > if value is dbnull.value then
    > '...
    > end if
    >
    > If it's any other value, you will be able to see it in variable 'value',
    > too.
    >
    >
    > Armin
     
    bob, May 5, 2008
    #3
  4. "bob" <> schrieb
    > Thanks for replying ...
    > I tried this:
    >
    > Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    > ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)
    > Handles
    > DetailsView1.ItemInserting
    > Dim h As Object
    > h = e.Values("myfield")
    > If h Is DBNull.Value Then
    > e.Cancel = True
    > Label1.Text = "may not be empty."
    > End If
    > End Sub
    >
    > But now, the event is never cancelled and every value is accepted.
    > In the db, i see NULL when rhe input in the textbox was empty.
    > I conclude that 'h' is not DBNULL , but how to cancel the event when
    > the input is empty?


    Use the built-in debugging features. Have you tried logging to debug
    output? For example

    Debug.Print(h.GetType.ToString)


    Armin
     
    Armin Zingler, May 5, 2008
    #4
    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. Replies:
    5
    Views:
    27,000
    Mike Schilling
    Mar 29, 2006
  2. Dennis Allison

    interpreting a null pointer as an empty (null string)

    Dennis Allison, Mar 5, 2004, in forum: C Programming
    Replies:
    11
    Views:
    822
    Derk Gwen
    Mar 6, 2004
  3. ButlerDJIAM
    Replies:
    0
    Views:
    574
    ButlerDJIAM
    Nov 9, 2006
  4. Col
    Replies:
    1
    Views:
    637
    peter koch
    Apr 21, 2006
  5. Savvoulidis Iordanis

    Empty gridview cells and checking for empty string

    Savvoulidis Iordanis, Sep 5, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    539
    Savvoulidis Iordanis
    Sep 5, 2008
Loading...

Share This Page