problem with checking null or empty value in detailsview

  • Thread starter Thread starter bob
  • Start date Start date
B

bob

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 said:
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
 
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
 
bob said:
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
 
Back
Top