473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

problem with checking null or empty value in detailsview

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.DetailsViewInsertEventAr gs) 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
Jun 27 '08 #1
5 3612
"bob" <bo*@gh.vbwrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...
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.DetailsViewInsertEventAr gs) Handles
DetailsView1.ItemInserting
Dim h As String
h = e.Values("myfield")
'If h = "" Then <== is this supposed to be commented?
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?
Firstly, you appear to have commented the line which I've highlighted, which
means that the corresponding End If must also be commented, otherwise the
code won't compile... I'm assuming you're using Option String...?

However, more fundamentally, a string variable can never really be a null
value, so it will never equate to Nothing in VB.NET...

Also, you appear not to be using any exception handling...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #2
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:ee*************@TK2MSFTNGP02.phx.gbl...
code won't compile... I'm assuming you're using Option String...?
Apologies - I mean Option Strict...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #3
"bob" <bo*@gh.vbschrieb
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.DetailsViewInsertEventAr gs)
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

Jun 27 '08 #4
bob
Thanks for replying ...
I tried this:

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewInsertEventAr gs) 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" <az*******@freenet.deschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
"bob" <bo*@gh.vbschrieb
>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.DetailsViewInsertEventAr gs)
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

Jun 27 '08 #5
"bob" <bo*@gh.vbschrieb
Thanks for replying ...
I tried this:

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventAr gs)
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

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
15
by: Robert Mark Bram | last post by:
Hi All! I have the following code in an asp page whose language tag is: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> // Find request variables. var edition = Request.Form ("edition"); var...
2
by: Jonas Hansson | last post by:
Hi, I have a problem regarding NULL values in a database when I retrieve the data with the MySqlDataAdapter fill a DataSet with the returned value. Whenever the SQL statement returns data that has...
4
by: luna | last post by:
could someone explain the meaning behind :- System.IndexOutOfRangeException: firstname1 when the code is :- If objDR("firstname1") Is System.DBNull.Value Then firstname1.Text = "" Else :...
1
by: David Lozzi | last post by:
Howdy, I am trying to accomplish the following: dim DateSend as date if txtDateSend.text = "" then DateSend = dbnull.value else DateSend = txtDateSend.text
3
by: Trapulo | last post by:
I've some problem checking items in a checkedlistbox by code: For jj As Int32 = 0 To cbTimeOptions.Items.Count - 1 ' cut: logic to set maycheck variable cbTimeOptions.SetItemChecked(jj,...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
4
by: John Kotuby | last post by:
Hi all, I have a simple user-form which accepts contact and profile information for the user to edit and save. All the textboxes are working fine and displaying the expected data, both when the...
3
gingawarrior
by: gingawarrior | last post by:
Hi All, I've written some code to act as a validator for a set of form fields - it accepts a string of '~' delimited item names and then loops through to evaluate if they are either empty or =='999'...
19
by: Alino | last post by:
how do i clonenode for example an div tag where are one text input field and two option tags to make that new cloned input field with an empty value? Thank you.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.