problem with checking null or empty value in detailsview

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
 
A

Armin Zingler

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
 
B

bob

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
 
A

Armin Zingler

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top