Classic ASP: RecordSet Field giving no value - asp-classic

I'm not really an ASP developer, so a little bit lost.
I have the following data access code:
sSQL = "SELECT answer_id, company_name, old_access_company_name, answer, flag_asker_notified FROM Q01_ask_sheiiba_answer_company2 WHERE question_id="& sQuestion_id &" ORDER BY answer_id"
rs.open sSQL, conn
DO WHILE NOT rs.EOF
Response.Write(rs.Fields("answer"))
rs.MoveNext
LOOP
I have tested that the sql query is built properly by outputting it to the response before it is called. It produces the following query:
SELECT answer_id, company_name, old_access_company_name, answer, flag_asker_notified
FROM Q01_ask_sheiiba_answer_company2
WHERE question_id=988
ORDER BY answer_id
When I copy that exact query to sql management studio and run it I get the expected results of 5 rows and each row containing data in every cell, BUT, when I run it through the above code, I get the same 5 rows with the same cell data, EXCEPT for the answer column, which is empty!
What am I missing?
Thanks in advance

There are two things you might want to try:
Put your text field at the end of the query. For example:
SELECT answer_id, company_name, old_access_company_name, flag_asker_notified, answer
If this doesn't give you the results, you might want to try:
WHILE NOT rs.EOF
theanswer=rs("answer")
Response.Write(theanswer)
rs.movenext
wend
Text and Memo fields can play a little havoc with ASP.
EDIT: Another thing that you can try is:
rs.CursorLocation = adUseClient
or
rs.CursorLocation = 3

The problem is that the ODBC driver can't access large text blobs as strings; you need to access them as chunked BLOB data.
I advise instead to dump the ODBC connection and connect using the OLE-DB driver directly. This will let you access that column as if it was just another varchar column.

I just had a similar problem (I think). I converted a varchar field to text. When I did so, I found that the text field seemed to "disappear" from my selected record set. I my case, I discovered that you can reference the text field ONLY ONCE. After that, it seems to disappear. Accordingly, for the text field, I now simply move it into a string variable and then operate on the string. That solved my problem.
John

Had the same problem and found the solution here https://web.archive.org/web/20170224013842/http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=80
Basically when you open the recordset (not connection.execute) use the options adOpenKeyset (val 1) and adUseClient (val 3), and the text filed should be the last in your field list in strSql
example: rs.Open strSql, dbConn, adOpenKeyset, adUseClient

Related

like condtion in sqldatasource for oracle database

i have an sqldatasource for a grid to query data from oracle database. everything works fine and dandy till i run into a problem with the where clause with "like" condition. below is the command i would like to pass into the datasource.
ssql = "select * from table1 where lastname like upper('%' || :last_name || '%')"
sqldatasource.selectparameters.add("last_name","test")
but no matter what i do it keeps saying invalid variable name/number. can someone help me please? thanks
There's probably something that you can do in the code to escape the %, but you could always avoid the issue by changing the sql statement to use chr(37) instead of '%'. E.g.:
select * from dual
where 'ABXD' like chr(37)||'X'||chr(37);
(I'd comment the code heavily to make it clear why you've done that, if you do end up choosing this route, though!)

classic asp query a recordset rather than a table

So, you have a recordset created like this:
Set rs = Server.CreateObject("ADODB.Recordset")
And you fill it like this:
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly
My question is, I want a second recordset that is a subset of the first (rs) recordset, can you do this in classic asp
Set rs2 = Server.CreateObject("ADODB.Recordset")
My immediate guess is that it would be something like this
rs2.Open queryString, rs, adOpenKeyset, adLockReadOnly
Why you ask? Well we have an older site that we are updating and adding new features too and rather than change a LOT of code I was wondering if I could be sneaky and use a setset of an already created (large) recordset, to save on another query to the db etc. Just wondering if it can be done.
Thanks,
You can use the Clone method to create a duplicate recordset, then use Filter to reduce the dataset to what you're interested in. For example:
Dim rs, rs2
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly
Set rs2 = rs.Clone()
rs2.Filter = "Field1 = 'foo'"
The string form of Filter is basic; it's pretty much <Field> <op> <value>. You can combine multiple expressions using AND and OR, but even that has some limitations (see the documentation link for the full details).
For more complex filtering, you can pass the Filter property an array of Bookmark objects. In this case, you loop through the recordset (or a clone of the recordset), testing each record by whatever complex criteria you have. If the record passes the test, you save its Bookmark to an array or other collection. Then, you can set the Filter property to your array of Bookmarks and you have a custom-filtered recordset.
'Note that I haven't tested this code
Dim rs, rs2, bookmarks
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly
Set rs2 = rs.Clone()
bookmarks = Array()
Do Until rs2.EOF
If rs2("Field1") = 2 * rs2("Field2") Then
ReDim Preserve bookmarks(UBound(bookmarks) + 1)
bookmarks(UBound(bookmarks)) = rs2.Bookmark
End If
rs2.MoveNext
Loop
rs2.Filter = bookmarks
' Now rs2 contains only records where Field1 = 2*Field2
You can use this same technique to get unique values (aka DISTINCT) by using a Dictionary object to store the unique key values. Doing a DISTINCT on multiple fields is a bit trickier. What I've done is the past is to combine the multiple fields using a separator that won't be in the data (such as a pipe |). That's not always possible, though.
The recordset object can be opened only by a "connection" object, you cannot replace the connection object with another recordset object as shown above, however if you modify the querystring object and open the desired subset through your own query you can achieve this.
Please post querystring if you have difficulty making the subset.
Obviously you can create a second query and recordset and run it over and over again inside "do while not rs.eof............loop" - I'm guessing that's what you want to avoid.
You might want to take a look at Datashaping. There's an article about it here. It's old, but then this is Classic ASP
https://web.archive.org/web/20210513001641/https://www.4guysfromrolla.com/webtech/092599-1.shtml
There also used to be a page on MSDN called "Using Data Shape to create hierarchical recordsets". You'll find loads of links to it on Google but they all take you to a 404 page on a resource for .net developers now :(

asp classic retrieve and display single recordset

i hv a code that need me to find only a single line of recordset from the database into the variable.
dim Connect,conn_,data,sql
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "toServer"
sql = "SELECT * from sppro where proj_name='pname'"
set Data = Connect.Execute(sql)
response.write data("proj_id")
i just cant find to correct way to retrieve and view single record set... i found something about cursor, but i dont understand it at all...
can anyone pls explain to me?
edit:
error tht i got with this code is as below.
ADODB.Field error '80020009'
Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
/bkpi-msn/Include/ServerSideAjax.asp, line 0
If I understand correctly you already know how to display one record from a database, but now you want to display more than one record. Is that right?
With some changes to your code, you can loop though all records:
dim Connect,conn_,data,sql
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "toServer"
sql = "SELECT * from sppro where proj_name='pname'"
set Data = Connect.Execute(sql)
Do Until Data.Eof
response.write data("proj_id")
Data.MoveNext
Loop
Edit: to retrieve just one row, you can use the following. It's basically the same, except there is a check to ensure there is at least one record, and no need to loop through the recordset.
dim Connect,conn_,data,sql
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "toServer"
sql = "SELECT * from sppro where proj_name='pname'"
set Data = Connect.Execute(sql)
If Not Data.Eof Then
response.write data("proj_id")
End If

how to make sql server 2005 search in arabic language?

I have a SQL Server 2005 database in which I have some tables contain Arabic text. The datatype for those fields is NVARCHAR(n).
The Arabic text inside the table is appearing properly, and when selecting using select clause, they appear properly.
The problem is that searching for Arabic text with where clause results in 0 rows.
select * from table_name
where name=#name
This retrieves no rows, where there is a name with this value.
When we use it like:
select * from table_name
where name=N’Arabic_Text’
Then it works, but how we can pass searching text from front end to back end.
Can you please guide me on how to write the query?
PS
In code behind i wrote:
Dim UserName As String = "N'" & txtLogin.Text & "'"
Dim _dtLogin As DataTable = oUser.UserLogin(UserName)
it returns 0 rows even if that user exist in database.
You need to concatenate your query and you need to use N before value to check because of unicode value
Considering this part: select * from table_name where name=#name
If this is a stored procedure, you need to modify it to be like this:
select * from table_name where name=N#name
give it a try and let us know if it worked.

Constructing a good search query using system.data.oracleclient

I am constructing a search function in a class to be used by several of our asp pages. The idea is simple, take a search term from the user and query the database for the item. Currently I am doing this the wrong way, which is vulnerable to SQL injection attacks (and ELMAH is in there to save the day if something goes wrong):
Public Shared Function SearchByName(ByVal searchterm As String) As DataTable
SearchByName = New DataTable
Dim con As New OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings("OracleDB").ConnectionString)
Try
con.Open()
Dim SqlStr As String = "select ID_ELEMENT, ELEMENT_NAME from table_of_elements where upper(ELEMENT_NAME) like upper('%" & searchterm & "%')"
Dim cmd As New OracleCommand(SqlStr, con)
SearchByName.Load(cmd.ExecuteReader)
Catch ex As Exception
Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
End Try
con.Close()
con.Dispose()
Return SearchByName
End Function
String concatenation is BAD. Next thing you know, Bobby Tables wrecks my system.
Now, the correct way to do this is to to make a proper oracle variable, by putting :searchterm in the string and adding the following line:
cmd.Parameters.Add(New OracleParameter("SEARCHTERM", searchterm))
The problem is since I am using a like statement, I need to be able to have % on either side of the search word, and I can't seem to do that with '%:searchterm%', it just gives an error of ORA-01036: illegal variable name/number.
Can I parameterize but still have my flexible like statement be a part of it?
Instead of doing the concatenation in your VB code, do the concatenation in the SQL statement. Then what you're trying to do should work. Here's some SQL illustrating what I'm talking about:
select ID_ELEMENT, ELEMENT_NAME
from table_of_elements
where upper(ELEMENT_NAME) like ('%' || upper(:searchterm) || '%')
BTW, you might end up with more efficient queries if you switch the collation on ELEMENT_NAME to case-insensitive and then remove the calls to upper().
Since you're using oracle, another option would be to use Oracle Text to perform the search.
It can take a bit to set up properly, but if you have a large amount of text to search, or have some sort of structured data, it can offer you many more options than a simple wild-card comparison.
It also has some nice features for dealing with multiple languages, if you happen to have that problem as well.

Resources