Double不会显示在ASPX页面
我试图在ASPX页面上显示下面的查询(总计)的值,并且我一直得到0.当我把一个断点和调试总计所有等于15.0,因为它应该。该值在从其中取出的MS SQL数据库中保存为小数。Double不会显示在ASPX页面
的.cs页
public partial class Payment : System.Web.UI.Page
{
public double total;
protected void Page_Load(object sender, EventArgs e)
{
var id = Request.Params["ID"];
System.Data.OleDb.OleDbConnection conn;
System.Data.OleDb.OleDbCommand cmd;
conn = new System.Data.OleDb.OleDbConnection("--");
cmd = new System.Data.OleDb.OleDbCommand();
conn.Open();
cmd.Connection = conn;
var sql = String.Format(@"select sum(PayAmt) as total from CurePay where CureID = '{0}'", id);
cmd.CommandText = sql;
double total = 0;
total = Convert.ToDouble(cmd.ExecuteScalar());
conn.Close();
.aspx页面中
<%= total %>
尝试使用Label
来输出自己的价值
<asp:Label ID="lblValue" runat="server" />
然后打印出来的值的标签。
lblValue.Text = Convert.ToDouble(cmd.ExecuteScalar()).ToString();
//this cuts out your variable duplication problem
使用这个,而不是你的<%= total %>
从你的代码,它看起来像您使用两个单独的参数名称 '总',一个地方,一个全球性的。本地正在更新,但全球正在输出。
public partial class Payment : System.Web.UI.Page
{
public double total; //<< keep this one
protected void Page_Load(object sender, EventArgs e)
{
var id = Request.Params["ID"];
System.Data.OleDb.OleDbConnection conn;
System.Data.OleDb.OleDbCommand cmd;
conn = new System.Data.OleDb.OleDbConnection("--");
cmd = new System.Data.OleDb.OleDbCommand();
conn.Open();
cmd.Connection = conn;
var sql = String.Format(@"select sum(PayAmt) as total from CurePay where CureID = '{0}'", id);
cmd.CommandText = sql;
double total = 0; // << remove the double keyword from this line
total = Convert.ToDouble(cmd.ExecuteScalar());
conn.Close();
这应该理清这个问题...
工作正常。谢谢。 – techora 2013-05-10 20:04:55
一种更传统的方法很可能是把一个主页上,然后在Page_Load功能
<asp:Label ID="lblNumber" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
// database stuff
lblNumber.Text = total;
}
这工作得很好,谢谢。 – techora 2013-05-10 20:08:01
你引用它分配两个不同的变量,全球和本地。您将本地var设置为0,但随后访问全局,只需删除该行:
double total = 0;
该代码将进行编译,因为它在不同范围内具有类似名称的变量是有效的。 – gunr2171 2013-05-10 20:04:06
这工作,谢谢。我喜欢这种方法! – techora 2013-05-10 20:06:40
完美的编辑,我只是要问关于转换为双。 – techora 2013-05-10 20:09:19
@kcray,没问题。我发现使用控件是一种更简单的格式化输出的方法,并且允许您沿着样式和格式化。 – gunr2171 2013-05-10 20:22:14