ASPAlliance Home
Advertise with us
About Prasad KVNM
Refer this Site
                                               
Home CDBL instead of CSNG KV's Kool ASP
Search
Samples/Articles
Convert URLs into Hyperlinks

Advertisement Management Systems

AdRotator Component

Extending AdRotator Component

Simple Alternative to AdRotator

A more powerful Ad Management System

Opinion Poll

Suggestions/Snippets
Passing Special Characters through URL

Date delimiter for Access and SQL Server

CDBL instead of CSNG

Handling Database Errors

Avoid Partial Updates to the Database

Book Review
C# and the .NET Platform

Favorite Links
ASPLists.com
ASPNG.com
Computer Dictionary
LearnASP.com

Microsoft .NET
Search @ Google
Wrox Press
Freecode.com

Site by Prasad KVNM,
New Jersey, USA
prasad@kunisetty.com
last updated on
22nd Oct, 2001



CDBL instead of CSNG

Use CDbl() function instead of CSng() function while dealing with real or floating point numbers in ASP. CDbl() outputs are double-precision numbers where as CSng() outputs are single-precision numbers.

So, if you use CSng(), when dealing with high precision numbers (having more decimal places), it will truncate the number and may lead to problems.

For example, run the following script to see how CSng() truncates the data.

<%
Dim MyDouble1, MyDouble2, MyDouble3
Dim MySingle1, MySingle2, MySingle3

MyDouble1 = 295.34344457
MyDouble2 = 32432.123765785
MyDouble3 = 7849444.5785
MySingle1 = CSng(MyDouble1)
MySingle2 = CSng(MyDouble2)
MySingle3 = CSng(MyDouble3)

Response.Write MySingle1 & "<br>"
Response.Write MySingle2 & "<br>"
Response.Write MySingle3 & "<br>"
%>

You will get the following output:

295.3434
32432.12
7849445

See this Script live

Recently I have got a mail from a visitor. It says:

I have some problem passing decimal value from asp page to stored
procedure  (SQL SERVER 7). Please suggest me if there is any solution.

The code is below:

IF Request.Form("action")="Add" then
    sTarget=Trim(Request.Form("text4"))
    ' get procedure
    Set oCmd=GetStoredProcedure(oConn,"sp_AddTheme")
    oCmd.Parameters.append
    oCmd.CreateParameter("TARGET", addecimal,adParamInput,,Csng(sTarget))
    oCmd.execute()
END IF


Field "Target" in DB is decimal Data type (scale is 8).

When I execute this page, I am getting the following error:

"Microsoft OLE DB Provider for SQL Server error '80040e21'
Invalid character value for cast specification."


- Sumonta Chonggullayanawat

The solution is using CDbl() instead of CSng().



Refer this site ASPAlliance.com |  Contact Us |  Join |  Advertise |  Best Viewed with IE 4.0 or above