Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
        }
    }
}
Posted

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();
}
 
Share this answer
 
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.

C#
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);
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900