Solved

decimal datatype not behaving correctly

Posted on 2007-11-14
11
2,110 Views
Last Modified: 2013-11-26
I have a field in a table of type decimal(10,3)
so I want it sohuld be able to save the number upto 3 decimal places.

This is in SQL Server 2005

Now while saving this data the stored procedure has declared this field as decimal.


What is passes from the application-->
shippingquantity = CType(sShippingQty, Decimal)

sShippingQty is takrn from a text box

If I enter 20.345 the value in shippingquantity  is 20.345

but when it saves this to the database it saves it as 20.000

Why so? Is decimal datatype not the correct datatype for this requirement?
What shall i use..what precision?
0
Comment
Question by:sonalj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 20282398
>>Now while saving this data the stored procedure has declared this field as decimal.<<
You need to define the parameter in your stored procedure also as decimal(10,3)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282407
Also, make sure you are declaring the precision and scale in VB.NET
0
 

Author Comment

by:sonalj
ID: 20282446
I defined the datatype in stored procedure as decimal(10,3)
and it is working correctly..but please let me know how to declare  the precision and scale in VB.NET?
Thanks.
0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282469
Post your VB.NET code as it stands now.
0
 

Author Comment

by:sonalj
ID: 20282554
Dim sShippingQty As String = CType(oRowItem.FindControl("txtQty"), TextBox).Text
                            If Not String.IsNullOrEmpty(sQty) Then
                                Try
                                    Item.Fields("dQty") = CType(sShippingQty, Decimal)
                                Catch
                                End Try
                            End If


Actually its textbox in a gridview. Item is a class . Fields is a property of type hashtable in item class.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282595
I was talking about the code you use to call the stored procedure, that is where you are supposed to set the Precision and Scale.
0
 

Author Comment

by:sonalj
ID: 20282642
Actually it is being saved in a different component and   Item.Fields("dQty")  actually is set in db.setparameter. Not sure how can we set the precesion here.  Otherwise modifying the stored proc works fine

Dim db As Database = DatabaseFactory.CreateDatabase(connection)
            Dim dbCommand As DbCommand = db.GetStoredProcCommand("sprocSaveItems")
            Try
                db.DiscoverParameters(dbCommand)            
                db.SetParameterValue(dbCommand, "_gId", Id)
 For Each sKey As String In Fields.Keys                  
                        db.SetParameterValue(dbCommand, sKey, Fields(sKey))
                    End If
                Next
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20282699
                           If Not String.IsNullOrEmpty(sQty) Then
                                Try
                                    Item.Fields("dQty") = CType(sShippingQty, Decimal)

try Double instead of Decimal on the last line above. also, do you mean
String.IsNullOrEmpty(sShippingQty)  instead of just sQty?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282705
You are not making this easy, so assuming you are using the SQLParameter object than you should set the Precision and Scale properties.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282740
>>try Double instead of Decimal on the last line above.<<
Why in world would you want to lose the precision of a decimal data type by using a double data type instead?  In T-SQL terms, that is like saying replace decimal with the far less precise float or real.
0
 

Author Comment

by:sonalj
ID: 20283227
try Double instead of Decimal on the last line above. also, do you mean
String.IsNullOrEmpty(sShippingQty)  instead of just sQty?

Imitchei--> Oh yes i mean String.IsNullOrEmpty(sShippingQty)

acperkins-->
 Dim db As Database = DatabaseFactory.CreateDatabase(connection)
            Dim dbCommand As DbCommand = db.GetStoredProcCommand("sprocSaveItems")
            Try
                db.DiscoverParameters(dbCommand)            
                db.SetParameterValue(dbCommand, "_gId", Id)
 For Each sKey As String In Fields.Keys                  
                        db.SetParameterValue(dbCommand, sKey, Fields(sKey))
                    End If
                Next
               


I am taking the parameter and its value in a loop to set the precision and scale for 1 of the parameter..I'll have to check if parameter namel ike qty ( or whatever) then set the scale & precision.


But I think setting the scale and precision in the stored procedure should be enough..right?

There is no declaration as sql parameter in the above code..so i m not very sure where to give the scale and precision for 1 of the fields in the loop
 For Each sKey As String In Fields.Keys                  
                        db.SetParameterValue(dbCommand, sKey, Fields(sKey))
                    End If
                Next
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

696 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