Solved

ASP - Type Mismatch

Posted on 2014-01-22
7
452 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 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

13 Experts available now in Live!

Get 1:1 Help Now