views:

375

answers:

5

Hi i got this method, how can i make the decimal to .00 and not .0000 ? :)

    public static List<Product> GetAllProducts()
{
    List<Product> products = new List<Product>();
    string sqlQuery = "SELECT * FROM Products";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand(sqlQuery, connection))
        {
            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    Product product = new Product();
                    product.Id = Convert.ToInt32(reader["Id"]);
                    product.ManufacturerId = Convert.ToInt32(reader["ManufacturerId"]);
                    product.CategoryId = Convert.ToInt32(reader["CategoryId"]);
                    product.Name = (reader["Name"]).ToString();
                    product.Description = (reader["Description"]).ToString();
                    product.Price = Convert.ToDecimal(reader["Price"]);
                    product.ItemsInStock = Convert.ToInt32(reader["ItemsInStock"]);

                    products.Add(product);
                }
            }
        }
    }
    return products;
}

UPDATE: Sorry for asking stupid questiens. i can't se where to put the DataFormatString="{0:F2}"

This is my grid:

            <asp:TemplateField HeaderText="Price" SortExpression="Price">
                <EditItemTemplate>
                    <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="PriceLabel" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
+5  A: 

1234m.ToString("0.00")

ifwdev
+4  A: 

You haven't shown where you are displaying these. All we have is a database cell and an object in memory, both of which are merely storage. For storage purposes, .00 and .0000 are the same thing. Trying to translate from one to the other is a waste of resources. Show us where you display this to the user, and we'll help you format it however you want.

Also, as a personal preference I'd write that code like this:

private static ToProduct(this IDataRecord record)
{
    var product = new Product();
    product.Id = record.GetInt32(record.GetOrdinal("Id"));
    product.ManufacturerId = record.GetInt32(record.GetOrdinal("ManufacturerId"));
    product.CategoryId = record.GetInt32(record.GetOrdinal("CategoryId"));
    product.Name = record.GetString(record.GetOrdinal("Name"));
    product.Description = record.GetString(record.GetOrdinal("Description"));
    product.Price = record.GetDecimal(record.GetOrdinal("Price"));
    product.ItemsInStokc = record.GetInt32(record.GetOrdinal("ItemsInStock"));
    return product;
}

public static IEnumerable<Product> GetAllProducts()
{
    string sqlQuery = "SELECT * FROM Products";
    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(sqlQuery, connection))
    {
        connection.Open();

        using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
                yield return reader.ToProduct();
            }
        }
    }
}

Update:
You commented that this will be in a GridView. Okay. In that case, all you need to do is have a DataFormatString in the column definition, like this:

<asp:GridView runat="server" id="TestView" ... >
    <Columns>
        <asp:BoundField DataFormatString="{0:F2}"  />
        ...
    </Columns>
</asp:GridView>
Joel Coehoorn
As an aside, this demonstrates one of the few things I like better about visual basic. Imagine for a moment that this is last method in the last class in a namespace. The end of the file would have 6 nested closing braces (}). The VB ends up so much easier to read.
Joel Coehoorn
Sorry i am printing this to a GridView.i can't use product.Price = Convert.ToDecimal(reader["Price"].ToString("0.00")); because it expects an integer
But i will try your solution
What "solution"? I haven't told you how to fix your problem yet. Only that you're looking at the wrong thing to fix it. But we are getting somewhere. You want to show this in a gridview. Great! Now we know how to fix the display issue. I'll update my answer to explain what you need to do.
Joel Coehoorn
A: 

Well obviously you couldn't do the following

product.Price = Convert.ToDecimal(reader["Price"]).ToString("0.00");

as it would return a string. Though you may beable to do this:

product.Price = Convert.ToDecimal(reader["Price"].ToString("0.00"));

This would seem the most logical way to complete this operation.

GaryDevenay
A: 

Hi,

Are you worried about culture settings?

Check out this msdn link: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Should hopefully give you a hand.

Luke Duddridge
+1  A: 

When printing Product.Price, use .ToString("N2"). What you might want is the full currency expression, which is .ToString("C"). Here is a reference link for all the format strings:

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Matt Hamsmith