[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Formatting currency for a label

Posted on 2010-04-10
5
Medium Priority
?
1,272 Views
Last Modified: 2012-05-09
Hi I am trying to get the subtotal query to show as a #.## value. Now I am getting four digits to the right of the decimal. I have tried various permutations of below, but I get either an error or no change.
void CalcTotal()
    {
        //Todo
        //declare and initialize connection object to connect to the database
        SqlConnection conn = new SqlConnection(
            WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd; //declare a command object that will be used to send commands to database.
        conn.Open();
        cmd = conn.CreateCommand();

        cmd.CommandText = "Select Sum(Quantity*Listprice) " +
            "From Shopcartline as SHL inner join Song as S on SHL.SongID = S.SongID " +
            "Where ShopcartID = " + Session["shopCartID"];
        cmd.CommandType = CommandType.Text;

        //declare an SqlDataReader variable
        SqlDataReader dataReader;
        dataReader = cmd.ExecuteReader();

        if (dataReader.HasRows)
        {
            dataReader.Read();
            lblTotal.Text = dataReader.GetValue(0).ToString();
          lblTotal.Text = String.Format("{0:C2}",lblTotal.Text);<----Here
        }
        else
        {
            lblTotal.Text = "";
        }
        conn.Close();
    }

Open in new window

0
Comment
Question by:sukotto100
5 Comments
 
LVL 21

Expert Comment

by:Alfred A.
ID: 30320329
Hi,

Try this,

lblTotal.Text = lblTotal.Text.ToString("f2")
0
 
LVL 21

Expert Comment

by:Alfred A.
ID: 30320415
Hi,

Or, you can try this if you are formatting currency,

lblTotal.Text = lblTotal.Text.ToString("C2")
0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 30320508
Try:

string a = dataReader.GetValue(0).ToString();
lblTotal.Text = a.ToString("#0.00"));
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 30322031
Try this:

Double amount =(Double) dataReader.GetValue(0);
lblTotal.Text = amount.ToString("c2");

Or
string amount = dataReader.GetValue(0).ToString();
lblTotal.Text = Convert.ToDouble(amount).ToString("c2");<----Here
0
 
LVL 12

Accepted Solution

by:
lazyberezovsky earned 2000 total points
ID: 30323470
I think that will be enough:
lblTotal.Text =String.Format("{0:C2}", dataReader.GetValue(0));

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

612 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