ASP question

J

Jon Spivey

Ok, what happens if you do
<%
strTemp = "2003"
response.write objBU(strTemp)
%>
there's no reason why the above wouldn't work, so by extension this
<%
strTemp = request.form("year")
' assuming request.form("year") contains 2003
response.write objBU(strTemp)
%>
would work just the same. I think what we need here is to see more of your
code - can you post up exactly what you have so far.
 
K

Kevin Spencer

In any programming language, there are 2 different kinds of strings.

A string literal is a literal string, and is denoted by enclosing a literal
string in double quotes. Example:

Response.Write "A Literal String" ' writes "A Literal String" to the
page

A string variable is a storage container for a string. Think of it as a box,
which you can reference by name. The box has a string inside it, and when
you refer to the name of the box (variable), you are actually referring to
what is inside the box. Example:

Dim s
s = "A Literal String"
Response.Write s ' writes "A Literal String" to the page

Note that the string variable is NOT enclosed in quotes. Why? Because it is
not a string literal. It is a variable HOLDING a string. The variable name
represents the string.

Now, what happens when you enclose the variabe name "s" in double quotes? It
becomes a string literal. Example:

Dim s
s = "A Literal String"
Response.Write "s" ' writes "s" to the page

Now, knowing that, let's look at your failed example:
Response.Write "<TD rowspan=2>" & objBU("Response.Write
Request.Form("Year")") & "</TD>"

Note that, according to the rules of VBScript, you enclosed "Response.Write
Request.Form("Year")" in double quotes. So, what does that indicate?:

Response.Write "<TD rowspan=2> & objBU("Response.Write
Request.Form("Year")") ...

So, what database column is being looked at? Apparently, one named
"Response.Write Request.Form("Year")"

- which wouldn't even parse correctly, because it is a string with more than
2 double-quotes in it, BTW.

So, how to correct the code? Something along the lines of:
Response.Write "<TD rowspan=2>" & objBU(Request.Form("Year")) & "</TD>"

Now, in order for the above code to work, you would have to have gone to a
page previously, which has a form in it, and the form would have to have a
field named "Year" in it. The form's ACTION property would point to the
current page, would have posted to the current page, and in the current
page, you would have to have opened a RecordSet, and there would have to be
a database COLUMN with the same NAME (not value) as the year that the user
entered in it.

This is very basic ASP stuff. If you plan to continue using ASP, you need to
have the basics down before you proceed.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
D

David Berry

Hi Kevin. Actually, what he's trying to do is take the value from
Request.Form and use that as the Field name in his recordset. For example:

If Request.Form("Year") = "2003" then he wants to Response.write
objBU("2003") - 2003 is the field name. If the value is 2004 then he wants
the field called 2004 - objRSBU("2004") and so on.
 
M

Matt Shudy

Hey,

Yeah that works. Any other ideas? I think that I would
not be able to set up a database in a way that i would be
able to display the results like i intended to...


Matt Shudy
 
M

Matt Shudy

Hey,

Yeah that works. Any other ideas? I think that I would
not be able to set up a database in a way that i would be
able to display the results like i intended to...


Matt Shudy
 
M

Matt Shudy

Hey,

Yeah that works. Any other ideas? I think that I would
not be able to set up a database in a way that i would be
able to display the results like i intended to...


Matt Shudy
 
M

Matt Shudy

Hey,

Yeah that works. Any other ideas? I think that I would
not be able to set up a database in a way that i would be
able to display the results like i intended to...


Matt Shudy
 
J

Jon Spivey

So, how to correct the code? Something along the lines of:
That will always fail - even though it looks like it should be OK.
However this
<%
' assuming request.form("year") = 2003
Response.Write objBU(cstr(request.form("year")))
%>
or this
<%
' assuming request.form("year") = 2003
strTemp = Request.Form("Year")
Response.Write objBU(strTemp)
%>
works fine - there's probably a good reason :)

Jon
 
D

David Berry

If that works then changing it to:

strFieldName = Request.Form("Year")
Response.Write objBU(strFieldName)

should also work. 2 questions.

1. Is the form field actually called "Year"
2. Are you in fact filling out the form and posting it to the next page that
has the above code?
 
M

Matt Shudy

Hey,

Thanks to all of you for helping me out! The cstr
(Request.Form("Year")) worked for me :). I will try
setting strField=Request.... again to see if that works.

Again Thanks for sticking with me!

Matt Shudy
 
M

Matt Shudy

Hey,

Thanks to all of you for helping me out! The cstr
(Request.Form("Year")) worked for me :). I will try
setting strField=Request.... again to see if that works.

Again Thanks for sticking with me!
 
K

Kevin Spencer

It may have something to do with your use of objBU(value). While the ADO COM
object model lets you play fast and loose with the RecordSet Field object,
it can cause problems. In point of fact, objBU(value) is a reference to an
ADO Field object, NOT its' value. The ADO object model allows you to use
this shortcut, and will USUALLY return the value in the field, but the
correct syntax to make sure that the value is indeed what you're referring
to is objBU(value).Value. The Value property of the ADO Field object
contains the actual value from the RecordSet. Note that the data type of the
value will be the same data type as the type stored in the column/row of the
RecordSet you're pulling it from. This means that you have to be careful
when, for example, comparing strings and numbers, or if the value in the
column is Null. The Null problem can be particularly difficult to identify
and deal with. To make sure that you aren't working with a Null, but a
string, you can use something like the following:

Response.Write objBU(Request.Form("Year")).Value & ""

Note that the Value property is referenced, and that a blank string is
concatenated to the value returned. This ensures that if the value is Null,
it is appended toa blank string, making the result a blank string, rather
than Nothing.

Also, make sure that the value returned by Request.Form("Year") is ALWAYS a
valid database column name.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jon Spivey

Interesting question - I just tested this against the pubs database in sql
server with this code
<%
if request.form <> "" then
strTemp = request.form("t")
set oRs=server.createobject("adodb.recordset")
oRs.open "select * from authors",
"provider=sqloledb;server=127.0.0.1;database=pubs;uid=sa;pwd=***"
do until oRs.eof
response.write oRs(request.form("t")) & "<br>"
oRs.movenext
loop
else
%>
<form method="post" action ="test.asp">
<input type="text" name="t"><input type="submit">
</form>
<%end if%>
and then typing "City" into the form field - it fails with
Item cannot be found in the collection corresponding to the requested name
or ordinal.

whereas changing
response.write oRs(request.form("t")) & "<br>"
to
response.write oRs(cstr(request.form("t"))) & "<br>"
or
strTemp = request.form("t")
response.write oRs(strTemp) & "<br>"
works

Live and learn :)

Jon
 
K

Kevin Spencer

How bizarre. Having to cast or assign a Request.Form value in order to get
it to become a string?! In my experience, Request.Form values have always
been strings to start out with! Must be a quirk of either ASP or VBScript. I
can't stand Late Binding! Thank God for ASP.Net!

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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

Similar Threads

SQL help 1
Updating db... still struggling 2
upadting db 1
What do we do with asp page? 1
asp help 2
FP2000 - Error with ASP Tell a Friend Script 1
Simple asp password page 2
File Upload question 3

Top