【发布时间】:2019-02-01 04:51:47
【问题描述】:
这是我创建的表:
CREATE TABLE Product
(ID INTEGER IDENTITY(1,1) NOT NULL ,
Product_No AS RIGHT ('PDT0000' + CAST(ID AS VARCHAR(10)),10) PERSISTED PRIMARY KEY CLUSTERED,
Product_Image VARBINARY (MAX) NOT NULL,
Product_Name VARCHAR(50) NOT NULL,
Product_Category_No INTEGER NOT NULL FOREIGN KEY REFERENCES Product_Category(Product_Category_No),
Product_Price MONEY NOT NULL,
Product_Quantity INTEGER NOT NULL,
Grocery_Branch_No INTEGER NOT NULL FOREIGN KEY REFERENCES Grocery_Branch(Grocery_Branch_No)
)
当我在 Product_Price 列中插入 10.50 或 100.45 或 11.05 之类的值,然后在 SQL Server 和 Visual Studio 中运行 SELECT * FROM Product 时,Product_Price 列值显示为 10.50 或 100.45 或 11.05。
但是,当我在 aspx 页面的 gridview 中看到 SELECT * FROM Product 的结果时,它会显示 10.5000 或 100.4500 或 11.0500。
换句话说,SQL Server 和 Visual Studio 中 Product_Price 列值的结果显示小数点后 2 个数字(因为在 INSERT 语句中我在小数点后添加了 2 个数字)。 但是,在运行 aspx 页面时,在网格视图的 Product_Price 列值中,它显示了小数点后 4 个数字。
我确保在 AddProducts.aspx 中添加 Product_Price 列的正则表达式验证器
p>
Price:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="TextBox2" ErrorMessage="This field is required"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" ControlToValidate="TextBox2" ErrorMessage="After decimal only 2 numbers" ValidationExpression="^\d{1,9}\.\d{1,2}$"></asp:RegularExpressionValidator>
</p>
这就是我在 AddProducts.cs 中所做的
Stream stream = postedfile.InputStream;
BinaryReader binaryreader = new BinaryReader(stream);
byte[] bytes = binaryreader.ReadBytes((int)stream.Length);
string branch = Session["BranchAdmin"].ToString();
string CS;
CS = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Grocery_Demo; integrated security=SSPI";
SqlConnection con = new SqlConnection(CS);
SqlCommand cmd = new SqlCommand("AddProducts", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@ProductImage", FileUpload1.FileBytes);
cmd.Parameters.AddWithValue("@ProductName", TextBox1.Text);
cmd.Parameters.AddWithValue("@ProductCategoryName", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@ProductPrice", TextBox2.Text);
cmd.Parameters.AddWithValue("@ProductQuantity", TextBox3.Text);
cmd.Parameters.AddWithValue("@GroceryBranchName", branch);
cmd.ExecuteNonQuery();
con.Close();
MessageBox("New Product has been added");
那么这就是我在ViewProducts.aspx中所做的
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center">
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="100px" Width="150px"
ImageUrl='<%#"data:Image/png/jpg/jpeg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Product_Image")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Product_Name" HeaderText="Product" />
<asp:BoundField DataField="Product_Category_Name" HeaderText="Category" />
<asp:BoundField DataField="Product_Price" HeaderText="Price" DataFormatString="{0} AUD" />
<asp:BoundField DataField="Product_Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Grocery_Branch_Name" HeaderText="Branch" />
</Columns>
</asp:GridView>
这就是我在 ViewProducts.cs 中所做的
private void DisplayProducts()
{
string CS;
CS = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Grocery_Demo; integrated security=SSPI";
SqlConnection con = new SqlConnection(CS);
SqlCommand cmd = new SqlCommand("ViewProducts", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
我仍然无法弄清楚为什么 ViewProducts.aspx 页面在网格视图中显示 Product_Price 列值的小数点后 4 个数字。
如果提供推荐的语法解决方案以在网格视图中显示 Product_Price 列值的小数点后 2 个数字,将会很有帮助。
【问题讨论】:
-
试试
<asp:BoundField DataField="Product_Price" HeaderText="Price" DataFormatString="{0:D2} AUD" /> -
我补充说,正如你所推荐的,但是当我尝试运行 aspx 页面时,它显示以下错误: System.Web.dll 中发生了“System.FormatException”类型的异常,但是不在用户代码中处理。但是,如果我删除 D2 并尝试运行 aspx 页面,那么它会显示 gridview 显示,但列值在小数点后显示 4 个数字
-
如果您删除
AUD并保留{0:D2}会发生什么 -
如果我删除 AUD 并保留 {0:D2} ..它仍然显示相同的错误:System.Web.dll 中发生“System.FormatException”类型的异常,但未在用户中处理代码。
-
但如果我删除 D2 并保留 {0} 和 AUD,则 aspx 页面会成功运行并显示网格视图。但正如我所说,在网格视图中,它显示 Product_Price 列值的小数点后 4 个数字。
标签: c# asp.net sql-server visual-studio