-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

Alternate color for RS looping with For Next
Northwind / ASP - Nested select
ASP SQL Injection




 Forums /

Inconsistent execution of code
Clint | Posted 5:34pm 10. October 2002 Server Time |

Hi all,

The code below is doing my head in. It works fine for some entries in the database but not for others?

I added the first 2 lines as part of my troubleshooting.

<%
              
sfe = Len(rs.Fields("EngMonFuncDigital"))

Response.Write sfe

If Len(rs.Fields("EngMonFuncDigital")) = 0 Then

  Response.Write "No Digital Functions Specified"
  
Else

  strDigital = rs.Fields("EngMonFuncDigital")

  arrDigital = Split(strDigital, ", ", -1, 1)

  For J = LBound(arrDigital) To UBound(arrDigital)

  Response.Write arrDigital(J) & "<br>"

  Next
  
End If

%>

There is no difference between the records it works for and the records it doesn't. In all the field is null yet it still falls over for some? When the field is not null it seems to work perfectly.

I used to have 'If IsNull(field)' and 'If field = ""' but the 'If Len(field) = 0' seems to work best, but still not perfect.

The frustrating thing is I have similar code above for another field yet it never fails there? At worst all I have seen it do is not write the "No Upgrades / Modifications Required". Having said that it still runs through the rest correctly, that is not executing after the Else.

<%
    
If Len(rs.Fields("UpgradesRequired")) = 0 Then

  Response.Write "No Upgrades / Modifications Required"
  
Else  

  strAllUpgrades = rs.Fields("UpgradesRequired")
  
  arrAllUpgrades = Split(strAllUpgrades, ", ", -1, 1)
    
  For I = LBound(arrAllUpgrades) To UBound(arrAllUpgrades)
    
  Response.Write arrAllUpgrades(I) & "<br>"
        
  Next
  
End If
  
%>

Can anyone please assist here?

Thanks,
Clint
Clint | Posted 6:28pm 10. October 2002 Server Time |

OK I think I know why. It's to do with the way the data is going into the database.

This might have not been the best way of doing it but I needed to write a screen between the form and actually posting the data, a confirm page. This screen would check entered values between existing values in a different table and allow the user to view the differences. I passed the data along to the final page by a heap of hidden form inputs like so :
<input type="hidden" name="EngMonFuncDigital" value="<%=TRIM(Request.Form("FuncDig"))%>">
I notice that if a value goes from say 1 to null, it is not overwritten and the value of 1 is still the value.

I would say it is working for Upgrades Required because it is an existing value in the database and is not touched by the new confirm page.

So how do I force it to write a null value?
Clint | Posted 6:31pm 10. October 2002 Server Time |

Forgot to mention in my first post that my troubleshooting lines fail as well. Len does not return a value. I'm no guru but to me that makes me think I'm on the right track with the above (2nd) post?

See how crazy this is driving me?! I'm talking to myself!!
Clint | Posted 8:09pm 10. October 2002 Server Time |

OK Nevermind. I fixed it or rather I have a workaround.

For those interested I made the page that inserts the entry (the page before all those mentioned above) insert a '' for the fields affected. This fixed the problem for all new entries created.

For existing records that were affected, I simply inserted "No Digital Functions Selected" into the actual database. These entries are unable to be edited by design so it will never come up as an option.

I guess if I knew what I know now when I started this project I would have done it differently. But to be positive it works (just not the cleanest code) and I've learnt heaps from this error!

Thanks for listening!

Clint :)
Holly | Posted 6:17am 11. October 2002 Server Time |

Sounds like issues with allowing nulls in your fields. You were absolutely right when you said the problem was with how data was going into the database.

If you define the field on the database as allowing nulls, be consistent in what you put in the field. If you want it to be null, set it to NULL in your SQL statement. Don't set it to "", which is not the same as NULL.

UPDATE theTable SET theField = NULL WHERE theKey = 123

A field which is truly null has no length. You won't get any value at all with Len(rs("thefield")), not even zero. You have to test it for IsNull if you want to know if there is a value in it.

1
Show all replies to 'Inconsistent execution of code'
This post have been closed for new replies



| Info |
© Copyright 1997-2013 Alexander Haneng