双不会显示在ASPX页面

本文关键字:ASPX 页面 显示 | 更新日期: 2023-09-27 18:13:15

我试图在ASPX页面上显示下面查询的值(Total),我一直得到0。当我放入断点时,调试总数都等于15.0。该值以十进制形式保存在MS SQL数据库中。

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 %>

双不会显示在ASPX页面

从你的代码看起来,你正在使用两个单独的变量名称'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();

更传统的方法可能是在主页上放置一个,然后从Page_Load函数

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

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

您引用了两个不同的变量,一个全局变量和一个局部变量。你将局部变量设置为0然后访问全局变量,只需要删除这一行:

double total = 0;

尝试使用Label输出您的值

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

然后将值打印到标签。

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

用这个代替你的<%= total %>