Wrox Programmer Forums
|
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 7th, 2005, 09:11 AM
Authorized User
 
Join Date: Mar 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Returning Decimal values from database

One of my table column type is decimal(18,3). I use this column to store Weight values in kilograms, so it stores values in 0.500 (500 grams) or 5 (5 kilograms)

Now I am trying to get the value from this column.

I have a stored procedure which works correctly when I run it in SQL Query Analyser.

Then in my code behind file, I have:
decimal weight = MyCompany.WebModules.Products.Business.Products.Ge tBasketProductWeight(cartId, comID, true);

In Products.Data, I have:
    public decimal GetBasketProductWeight(string cartID, int companyid, bool actualWeight)

        {
            int numAffected;

            // create the parameters
.....
                                            new SqlParameter("@TotalWeight", SqlDbType.Decimal, 9),
            };

            // set the values
......
            parameters[3].Direction = ParameterDirection.Output;

            RunDecimalProcedure("spv_Products_GetBasketWeight" , parameters, out numAffected);

        return (decimal)parameters[3].Value;
        }


Then in DbObject.cs, i changed the RunProcedure as follows:

        protected decimal RunDecimalProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
        {
            decimal result;

            Connection.Open();
            SqlCommand command = BuildIntCommand( storedProcName, parameters );
            rowsAffected = command.ExecuteNonQuery();
            result = Convert.ToDecimal((int)command.Parameters["ReturnValue"].Value);
            Connection.Close();
            return result;
        }


However, I am still not getting the value in decimal. If the value returned from my database is 0.500, my code will return 0.

If the database returned value is 1, then my code returns 1.

So any idea how I can get return values like 0.500 (if the database has 0.500) and 5 (if the database has 5)

kind regards


 
Old October 7th, 2005, 09:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're casting it to an integer (dropping the decimal portion), then you convert it to decimal.
 
Old October 8th, 2005, 07:53 AM
Authorized User
 
Join Date: Mar 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by englere
 You're casting it to an integer (dropping the decimal portion), then you convert it to decimal.
I guess you are referring to this line:
result = Convert.ToDecimal((int)command.Parameters["ReturnValue"].Value);

If I take the (int) out, then I get a cast exception

 
Old October 10th, 2005, 08:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your types don't match: decimal(18,3) and new SqlParameter("@TotalWeight", SqlDbType.Decimal, 9). You told .net that it's an integer.

http://msdn.microsoft.com/library/de...ScaleTopic.asp





Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting decimal values in xsl SOMANATHAN10 XSLT 1 May 10th, 2007 07:47 AM
Allowing Decimal values bright_mulenga Access 1 February 12th, 2007 12:38 PM
How to add two variable with Decimal values sa_moizatyahoo ASP.NET 1.0 and 1.1 Basics 1 April 3rd, 2004 06:17 AM
decimal values is rounded hosefo81 PHP Databases 2 December 9th, 2003 01:50 AM
returning values with SQL stu9820 SQL Language 6 December 8th, 2003 02:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.