如何从数据库自动计算?

如何从数据库自动计算?

问题描述:

您好。



我正在尝试从数据库中自动计算。我所拥有的是TextBox1,TextBox2和TextBox3。我正在从数据库填充TextBox1和TextBox2,我在Textbox2上有TextBox_Text更改。如何从文件夹中获取两个TextBox来计算数字?这是我的代码。我在TextBox2上也有AutoPostBack。



Hello.

I am trying to auto calculate from the database. What I have is TextBox1, TextBox2 and TextBox3. I am populating TextBox1 and TextBox2 from the database and I have the TextBox_Text change on Textbox2. How can I get the two TextBoxes to calculate the numbers from the database? This is the code I have in place. I also have AutoPostBack on TextBox2.

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Drawing.Printing;
using System.IO;
using System.Web.SessionState;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace SACSCOCLogin1._1
{
    public partial class TestCal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();

            SqlCommand scmd = new SqlCommand("Select Cars, Trucks from TestCal", con);
            SqlDataReader dr = scmd.ExecuteReader();

            if (dr.Read())
            {
                TextBox1.Text = dr["Cars"].ToString();
                TextBox2.Text = dr["Trucks"].ToString();
            }
            dr.Close();
            con.Close();
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text);
            int b = Convert.ToInt32(TextBox2.Text);
            TextBox3.Text = Convert.ToString(a + b);
        }
    }
}

将您的sqlquery更改为选择汽车,卡车,汽车+来自TestCal的卡车总计

并将您的asp.net数据区代码更改为

if(dr.Read())

{

TextBox1.Text = dr [Cars] .ToString();

TextBox2.Text = dr [Trucks] .ToString();

TextBox3.Text = dr [Total] .ToString();

}
Change your sqlquery as "Select Cars, Trucks, Cars + Trucks as 'Total' from TestCal"
And change your asp.net dataread code as
if (dr.Read())
{
TextBox1.Text = dr[Cars].ToString();
TextBox2.Text = dr[Trucks].ToString();
TextBox3.Text = dr[Total].ToString();
}


我想我懂了!!!我自己解决了这个!在移动了一些东西并尝试它们之后我得到了它。我还必须删除TextBox_Text更改。这是我做的代码。



I think I got it!!! I solved this one myself!!! After moving somethings around and trying them out I got it. I also had to remove the TextBox_Text change. This is the code I did.

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Drawing.Printing;
using System.IO;
using System.Web.SessionState;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace SACSCOCLogin1._1
{
    public partial class TestCal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();

            SqlCommand scmd = new SqlCommand("Select Cars, Trucks from TestCal", con);
            SqlDataReader dr = scmd.ExecuteReader();

            if (dr.Read())
            {
                TextBox1.Text = dr["Cars"].ToString();
                TextBox2.Text = dr["Trucks"].ToString();
            }
            dr.Close();
            con.Close();


            {
                int a = Convert.ToInt32(TextBox1.Text);
                int b = Convert.ToInt32(TextBox2.Text);
                TextBox3.Text = Convert.ToString(a + b);
            }
        }
    }
}