problem with conditional response.write

R

raj chahal

Hi there

I need to be able to print on screen when I check if a value within a db

<td>
<% if ((Recordset1.Fields.Item("profile1").Value) <>
"") then response.Write"Pro1<br>" &
Recordset1.Fields.Item("profile1").Value%>

</td>

I am checking if profile1 contains any text if not don't print anything. If
yes then print the heading and the contents .
Now the problem is that I cannot seem to check the value and print the same
value.
If I check profile2 instead of profile1 (which contains a value) then
profile1 is printed.
At the moment only Pro1 is written to the screen but the contents of
profile1 is not, eventhough profile1 is not empty.

What could be wrong ??

Thanks
 
P

Patrice

For now I would start by writing :
Response.Write Recordset1.Fields.Item("Profile1").value=""
to make sure this is not an empty string.

If not it could be null or a string that contains white spaces (especially
if the DB field uses CHAR instead of VARCHAR)...
 
R

raj chahal

Hi there are no emty strings..
Also the text 'Pro1' is written to screen meaning that part of it executes
but not the Recordset1.Fields.Item("profile1").Value bit in its current
surroundings..
 
P

Patrice

Not sure if this is a response to my suggestion.

What I meant is that if profile1 holds " " it will print Pro1 (as this
is not "") but you won't see anything visible. Similarly if the value is
null and if I remember well my ASP days, it will print Pro1 (as this is not
"") but you won't see anything either.

I would really double check. If it still fails print those values and report
them to us :
Value<>"" ' It would definitely allows to find out if the value
is an empty string or not
IsNull(Value) ' It would allow to find is this is NULL
Len(Value) ' It would allow to find if it contains non visible
characters
 
R

Ray Costanzo [MVP]

Perhaps you have a NULL value. Try this.

<%
'''append an empty string to deal with possibility of null value
Dim sPro1
sPro1 = Recordset1.Fields.Item("profile1").Value & ""
If sPro1 <> "" Then Response.Write "Pro1<br>" & sPro1
%>

Ray at work
 
P

pulaki

Just in case you are unframiliar with NULL....

a Null value in a data field can appear as an empty string ("")....but it is
NOT an empty string. It is a value and it behaves like a value.

So, if a field contains a NULL value, then it may appear as though it is
empty, but technically, it is not empty. Perhaps you just want to add to
your conditional test:

if ( (..... <> "") AND (..... <> "Null") ) then .....

Just a thought.
 
M

Mike Brind

pulaki said:
Just in case you are unframiliar with NULL....

a Null value in a data field can appear as an empty string ("")....but it is
NOT an empty string. It is a value and it behaves like a value.

So, if a field contains a NULL value, then it may appear as though it is
empty, but technically, it is not empty. Perhaps you just want to add to
your conditional test:

if ( (..... <> "") AND (..... <> "Null") ) then .....

Just a thought.

Shouldn't that be

If ( (..... <> "") And ( Not IsNull(.......)) ) Then ..... ?

<>"Null" means is not equal to a string with the value "Null" - as
opposed to a null value. doesn't it? Or am I confusing database nulls
and vbscript nulls?
 
D

Dave Anderson

Patrice said:
...Similarly if the value is null and if I remember well
my ASP days, it will print Pro1 (as this is not "") but
you won't see anything either.

Afraid you don't remember your VBScript correctly, Patrice. If either
argument is Null, the inequality returns Null:
http://msdn.microsoft.com/library/en-us/script56/html/adb6e3ac-d925-4987-9d43-f6486d5f1e30.asp


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
P

Patrice

I wonder why I didn't thought VBScript would be clever enough to be
consistent with the usual NULL behavior ;-)

So, it would finally likely left us with non visible characters in profile1
(perhaps because of the use of CHAR). Hopefully the original poster will pop
up once checked...
 
R

raj chahal

Hi there again..

Thanks for your help.. I have now discovered something weird that explains
the problem below..
When I check froa a value or print a value from recordset the value is
emptied ! This is why I have the problem.

I tested with the below code :

1 <%
2 response.Write "profile1 - " &
(Recordset1.Fields.Item("profile1").Value) & "-<br> "

3 ' if ((Recordset1.Fields.Item("profile1").Value) <> null) then
4 response.Write "Pro1 " & (Recordset1.Fields.Item("profile1").Value)
5 ' End IF
6 %>

When I execute the above the response is :

profile1 - Photoshop and guitar playing-
Pro1

Therefore the value of (Recordset1.Fields.Item("profile1").Value) is now
somehow empty in line 4 !

When I comment line 2 the response is :

Pro1 Photoshop and guitar playing

So now (Recordset1.Fields.Item("profile1").Value) in line 4 prints !

The value of (Recordset1.Fields.Item("profile1").Value) is also emptied on
any checking for null.
I know line 3 may be incorrect for the moment but this is commented for this
test.

Any ideas on this pls ?
Thanks again
 
B

Bob Barrows [MVP]

Mike said:
Is the first one valid?

Why go through all this rigmarole? A simple

If len(yourvariable)>0 then
'it's got a value
else
'it's either null or an empty string
end if

Bob Barrows
 
P

Patrice

Never saw this. You may want also to use "view source" in case you would
have some kind of html markup inside the value that causes a problem in
rendering and make the value appears to be empty when it would be just not
rendered.

The first test I can think off would be something like :
Response.Write Now() & "<br>" ' To make sure the test is current
Response.Write Recordset1.Fields.Item("profile1").Value="Photoshop and
guitar playing" & "<br>"
Response.Write Recordset1.Fields.Item("profile1").Value & '"<br>"
Response.Write Recordset1.Fields.Item("profile1").Value="Photoshop and
guitar playing" & "<br>"
Response.Write Recordset1.Fields.Item("profile1").Value & "<br>"

To make sure if printing the value is really destructive. I suppose
Recordset1 is a usual ADOB recordset ?

Good luck.
 
A

Anthony Jones

Mike Brind said:
Is the first one valid?

I don't see anything wrong with it. IsNull returns a boolean which is
flipped then And'ed with the expression prior to it which also returns a
boolean.

Still I prefer Bob's solution. :)

Anthony.
 
M

Mike Brind

Anthony said:
I don't see anything wrong with it. IsNull returns a boolean which is
flipped then And'ed with the expression prior to it which also returns a
boolean.

Still I prefer Bob's solution. :)

Anthony.

So do I. It's the one I use - I don't use IsNull very often at all in
VBScript. I generally use AND [value] IS NOT NULL in my SQL to prevent
nulls ever getting to the recordset....
 
D

Dave Anderson

Patrice said:
I wonder why I didn't thought VBScript would be clever enough
to be consistent with the usual NULL behavior ;-)

One of the many comforts of JScript is its Boolean coercion. Since null and
empty string each coerce to false, this is sufficient in JScript:

if (Recordset1.Fields("profile1").Value) ...

Of course, this leads to one potential obstacle -- numeric zero is also
"false". Never hurts to know your data...



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top