2013-05-10 142 views
0

我試圖在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 %> 

回答

0

嘗試使用Label來輸出自己的價值

<asp:Label ID="lblValue" runat="server" /> 

然後打印出來的值的標籤。

lblValue.Text = Convert.ToDouble(cmd.ExecuteScalar()).ToString(); 
//this cuts out your variable duplication problem 

使用這個,而不是你的<%= total %>

+0

這工作,謝謝。我喜歡這種方法! – techora 2013-05-10 20:06:40

+0

完美的編輯,我只是​​要問關於轉換爲雙。 – techora 2013-05-10 20:09:19

+0

@kcray,沒問題。我發現使用控件是一種更簡單的格式化輸出的方法,並且允許您沿着樣式和格式化。 – gunr2171 2013-05-10 20:22:14

3

從你的代碼,它看起來像您使用兩個單獨的參數名稱 '總',一個地方,一個全球性的。本地正在更新,但全球正在輸出。

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(); 

這應該理清這個問題...

+0

工作正常。謝謝。 – techora 2013-05-10 20:04:55

1

一種更傳統的方法很可能是把一個主頁上,然後在Page_Load功能

<asp:Label ID="lblNumber" runat="server" /> 


protected void Page_Load(object sender, EventArgs e) 
{ 
    // database stuff 
    lblNumber.Text = total; 
} 
+0

這工作得很好,謝謝。 – techora 2013-05-10 20:08:01

1

你引用它分配兩個不同的變量,全球和本地。您將本地var設置爲0,但隨後訪問全局,只需刪除該行:

double total = 0; 
+0

該代碼將進行編譯,因爲它在不同範圍內具有類似名稱的變量是有效的。 – gunr2171 2013-05-10 20:04:06