Solved

ASP - Type Mismatch

Posted on 2014-01-22
7
449 Views
Last Modified: 2014-01-23
To start, I only have 1 record in my DB (brand new) and none of the fields are blank.

If i change productname (which is a text field in the DB) to price (which is currency in the DB) then this code works.  If I leave productname then I get:

Microsoft VBScript runtime error '800a000d'
Type mismatch: '[string: "TestProduct"]'
/triplels/index.asp, line 12

Ideas why I am getting this?

<%
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
conn.Open "DSN=triplels"

strSQL = "SELECT (productname) "
strSQL = strSQL & "FROM inventory"
rs.Open strSQL, conn

WHILE NOT rs.EOF

IF rs("productname") = 2 THEN
    productname = productname & "<option selected value=" & rs("productname") & ">" & rs("productname") & "</option>"
ELSE
    productname = productname & "<option value=" & rs("productname") & ">" & rs("productname") & "</option>"
END IF

rs.MoveNext

WEND

rs.Close
conn.Close

%>

Open in new window

0
Comment
Question by:bschwarting
7 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 300 total points
ID: 39801456
If productname is a string then you cannot compare it against a number

IF rs("productname") = 2 THEN

and productname sounds counter intuitive if you are storing a number in the field
0
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 150 total points
ID: 39801626
that is because a currency data type in a database is numerical, thats why it'll work if the database type is currency. if the database field does indeed contain the string "2" then you could do:

IF CInt( rs("productname") ) = 2 THEN

that'll convert the value to an integer data type. If the value is not an integer, it'll cause an error.

I do agree though, productname indicates a string to me, and not an integer value.

I also noticed in your loop you're missing some single quotes around your value attribute. it should be (i added spaces for readability, you should take them out):

WHILE NOT rs.EOF

IF rs("productname") = 2 THEN
    productname = productname & "<option selected value=' " & rs("productname") & ">" ' & rs("productname") & "</option>"
ELSE
    productname = productname & "<option value='  " & rs("productname") & " '>" & rs("productname") & "</option>"
END IF

rs.MoveNext

WEND

Open in new window

0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 50 total points
ID: 39801885
Instead of converting to an integer, I think it is a little better to covert everything to a string and that way you will avoid any error when there are things like nulls or blanks.

cStr(rs("productname")) = "2" THEN

On another note, is there a reason you are using a DSN connection?  DSN-Less connections are faster and safer.  Here is something more current http://blogs.office.com/2011/04/08/power-tip-improve-the-security-of-database-connections/ but if you read up at 4guys there are plenty of articles dating back to 1999 on the benefits.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:bschwarting
ID: 39803380
Guys, thanks for all this, it worked great.  I totally missed the =2 part.  When you pointed that out, it fixed my 1st issue.

I love the ASP portion of this site, best part of experts-exchange IMO.  You guys are awesome.

The only other issue I have is when it populates my drop down list, I lose the "value" to post the one I end up selecting.  So when I select the first in the list, then submit the form, the value is blank.

Ideas why this is happening?
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 39803400
glad you got it working :)

hard to say why, as we don't have the full code for the select box html. make sure you have a  NAME attribute on it, and also make sure you implemented my code for adding single quotes around the VALUE attribute in the options (taking out the single space that I added for readability). If all tha checks out, post your code so we can have a look.
0
 
LVL 1

Author Comment

by:bschwarting
ID: 39803555
Found my issue!  I had the wrong name in my area where the <select> tags were:

<!-- DROP DOWN FROM DATABASE -->
Product Name<br>
<select size="1" name="productname">
<%=productname%>
</select>
<!-- DROP DOWN FROM DATABASE -->

Open in new window

0
 
LVL 1

Author Closing Comment

by:bschwarting
ID: 39803559
Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now