Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: SV: Re: Decimal numbers in SQL Databases


Message #1 by "Christian Dygaard" <cdygaard@m...> on Wed, 18 Sep 2002 08:16:57 +0200
Thanks for the response Brent, now I'm able to store a number in the
database, but there's still one problem. It doesn't store a decimal number
the way I want to, if I for example write 5,75 the number that is stored is
575, if I write 5.75 the number that is stored is 6, I can't store a true
decimal number. Anyone, how is this done? can one store a true decimal
number in a SQL Server database at all?.

Best Regards

Christian Dygaard

-----Oprindelig meddelelse-----
Fra: Brent VanderMeide [mailto:ccbbttmm@a...]
Sendt: 1. september 2002 21:12
Til: ASP Databases
Emne: [asp_databases] Re: Decimal numbers in SQL Databases


I can find the following possiblities:

1.  Your FORM from the previous page is not sending any values for one of
your decimal variables.  Thus causing the INSERT command for those values
to look like the following where field2DC is your SQL Decimal Field.

  "INSERT INTO (field1, field2DC) VALUES('Hello', '')"

Therefor causing a T-SQL error stating that it could not convert the
VarChar data to numeric becuase it does not see any numeric data it simply
sees a varchar string which is blank.



2.  You do not have any data validation one this handler page.  First
verify that the information coming to the page is not blank, and that it
is numeric.  SEE EXAMPLE CODE BELOW.


I would rewrite your code to the following: (EXAMPLE CODE)
_______________________________________________________________
If Request.Form("WhatWasChosenX") = "InsertProduct" Then
            Dim strSupermarket, lngPrice, lngKgL, bitLiquid
            Dim objCmd
            strSupermarket = Request.Form("Supermarket")
            strProduct = Request.Form("Product")
            strProductType = Request.Form("ProductType")
            lngPrice = Request.Form("Price")
            lngKgL = Request.Form("KgL")
            If Request.Form("Liquid") = "on" Then
              bitLiquid = 1
            Else
              bitLiquid = 0
            End If

            '**** VERIFY DATA INFORMATION *****
            If (isNumeric(lngPrice) AND isNumeric(lngKgl)) AND _
               (lngPrice <> "" AND lngKgl <> "") Then
                Set objConn = Server.Createobject("ADODB.Connection")
                strConnString = "Provider=SQLOLEDB;" & _
                    "Persist Security Info=FALSE;" & _
                    "User ID=sa;" & _
                    "Password=pb16950;" & _
                    "Initial Catalog=Indkøbsregnskab;" & _
                    "Initial File Name=C:\Programmer\Microsoft " & _
                    "SQL Server\MSSQL\DATA\Indkøbsregnskab_Data.mdf;"
                objConn.Open strConnString
                strSQL = "INSERT INTO Product_T " & _
                    "(Product_VC, Product_Type_VC,Cost_DC, " & _
                    "Liquid_BT, Liter_Kilo_DC, Supermarket_VC) " & _
                    "VALUES ('" & strProduct & "', '" & _
                    strProductType & "', '" & lngPrice "', '" & _
                    bitLiquid & "', '" & lngKgL & "', '" & _
                    strSupermarket & "')"
                Set objCmd = Server.Createobject("ADODB.Command")
                Set objCmd.ActiveConnection = objConn
                objCmd.CommandText = strSQL
                objCmd.CommandType = adCmdText
                objCmd.Execute
            Else
                'Redirect user to the page that was submitting
                'the invalid data with an error message.
                'You can easilly add on what type of handling you want
                'But do something

                Response.Redirect Request.ServerVariables("HTTP_REFERER")
            End If
            '  CONTINUE ONTO THE REST OF YOUR CODE HERE
________________________________________________________________
Brent Allen VanderMeide
Senior Web/Application Engineer
brent@s...

Message #2 by Vijay G <happygv@y...> on Wed, 18 Sep 2002 00:15:57 -0700 (PDT)
Hi,

Change it datatype to decimal/numeric in the design view and give its scale value to whatever decimal point you want. Else if you
need more digits after decimal, then change it to float.

Cheers!!!
Vijay G

Christian Dygaard wrote:Thanks for the response Brent, now I'm able to store a number in the
database, but there's still one problem. It doesn't store a decimal number
the way I want to, if I for example write 5,75 the number that is stored is
575, if I write 5.75 the number that is stored is 6, I can't store a true
decimal number. Anyone, how is this done? can one store a true decimal
number in a SQL Server database at all?.

Best Regards

Christian Dygaard

-----Oprindelig meddelelse-----
Fra: Brent VanderMeide [mailto:ccbbttmm@a...]
Sendt: 1. september 2002 21:12
Til: ASP Databases
Emne: [asp_databases] Re: Decimal numbers in SQL Databases


I can find the following possiblities:

1. Your FORM from the previous page is not sending any values for one of
your decimal variables. Thus causing the INSERT command for those values
to look like the following where field2DC is your SQL Decimal Field.

"INSERT INTO (field1, field2DC) VALUES('Hello', '')"

Therefor causing a T-SQL error stating that it could not convert the
VarChar data to numeric becuase it does not see any numeric data it simply
sees a varchar string which is blank.



2. You do not have any data validation one this handler page. First
verify that the information coming to the page is not blank, and that it
is numeric. SEE EXAMPLE CODE BELOW.


I would rewrite your code to the following: (EXAMPLE CODE)
_______________________________________________________________
If Request.Form("WhatWasChosenX") = "InsertProduct" Then
Dim strSupermarket, lngPrice, lngKgL, bitLiquid
Dim objCmd
strSupermarket = Request.Form("Supermarket")
strProduct = Request.Form("Product")
strProductType = Request.Form("ProductType")
lngPrice = Request.Form("Price")
lngKgL = Request.Form("KgL")
If Request.Form("Liquid") = "on" Then
bitLiquid = 1
Else
bitLiquid = 0
End If

'**** VERIFY DATA INFORMATION *****
If (isNumeric(lngPrice) AND isNumeric(lngKgl)) AND _
(lngPrice <> "" AND lngKgl <> "") Then
Set objConn = Server.Createobject("ADODB.Connection")
strConnString = "Provider=SQLOLEDB;" & _
"Persist Security Info=FALSE;" & _
"User ID=sa;" & _
"Password=pb16950;" & _
"Initial Catalog=Indkøbsregnskab;" & _
"Initial File Name=C:\Programmer\Microsoft " & _
"SQL Server\MSSQL\DATA\Indkøbsregnskab_Data.mdf;"
objConn.Open strConnString
strSQL = "INSERT INTO Product_T " & _
"(Product_VC, Product_Type_VC,Cost_DC, " & _
"Liquid_BT, Liter_Kilo_DC, Supermarket_VC) " & _
"VALUES ('" & strProduct & "', '" & _
strProductType & "', '" & lngPrice "', '" & _
bitLiquid & "', '" & lngKgL & "', '" & _
strSupermarket & "')"
Set objCmd = Server.Createobject("ADODB.Command")
Set objCmd.ActiveConnection = objConn
objCmd.CommandText = strSQL
objCmd.CommandType = adCmdText
objCmd.Execute
Else
'Redirect user to the page that was submitting
'the invalid data with an error message.
'You can easilly add on what type of handling you want
'But do something

Response.Redirect Request.ServerVariables("HTTP_REFERER")
End If
' CONTINUE ONTO THE REST OF YOUR CODE HERE
________________________________________________________________
Brent Allen VanderMeide
Senior Web/Application Engineer
brent@s...




---------------------------------
Do you Yahoo!?
Yahoo! News - Today's headlines

  Return to Index