Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
im new to this

i wanted to create only a plus calculation with button&txtbox and display it in other textbox using the data int in my sqlexprs db.

i've created a db and dtable in sqlserver management tool there is only product name and price.


how do i do this.. pls give an example

What I have tried:

This is my Data Access Layer.cs

public static class DAL
    {
        public static DataTable ExecSP(string spName, List<SqlParameter> sqlParams = null)
        {
            string strConnect = "Server=PC\\SQLEXPRESS;Database=MyLoginApp;Trusted_Connection=True;";     
            SqlConnection conn = new SqlConnection();
            DataTable dt = new DataTable();

            try
            {
                //Connect to the database
                conn = new SqlConnection(strConnect);
                conn.Open();

                //Build an sql command / query
                SqlCommand cmd = new SqlCommand(spName, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(sqlParams.ToArray());

                //Execute command
                SqlCommand command = conn.CreateCommand();
                SqlDataReader dr = cmd.ExecuteReader();

                //fill datatable with the results
                dt.Load(dr);


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //No matter what happends this will run
                conn.Close();
            }
            return dt;
        }
    }


This's the button function i want to create..
"BasoSUCost" must be the value in dtable

 private void btnHitung_Click(object sender, EventArgs e)
        {
          if (int.TryParse(txtBasoSU.Text, out NumberofOrder))
            {
                ItemBasoSU = Double.Parse(txtBasoSU.Text);
                TotalPriceOfBasoSU = BasoSUCost * ItemBasoSU;
}
Posted
Updated 4-Sep-18 21:03pm

Um.
I see nothing that involves your DataTable outside the method in which you load it (and you'd be better off doing that with a DataAdapter rather than a DataReader - it involves a lot fewer round trips to the DB), so quite what you want to do is not obvious.

But ... two things do spring to mind: Why catch all exceptions if all you are going to do is re-throw them? That doesn't really help at all, it just complicates things. You can just have the try...finally block and skip the catch if that's what you are trying to do, or even better use a using block to Close and Dispose the connection (and another for the Command) instead of the try block.
C#
using (SqlConnection conn = new SqlConnection(strConnect))
    {
    using (SqlCommand cmd = new SqlCommand(spName, conn))
        {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddRange(sqlParams.ToArray());
        using (DataAdapter da = new SqlDataAdapter(cmd))
            {
            da.Fill(dt);
            }
        }
    }

Secondly, if it's a double then it'll fail the int.TryParse test - the presence of a '.' in the text string will mean it isn't an integer, so the outer TryParse will fail and it'll never get to the Double.Parse. Use Double.TryParse as the one and only test, and use the value it converted:
C#
private void btnHitung_Click(object sender, EventArgs e)
    {
    double value;
    if (double.TryParse(txtBasoSU.Text, out value))
        {
        TotalPriceOfBasoSU = BasoSUCost * value;
        ...
        }
    }
 
Share this answer
 
Comments
Muhammad nur Ihsan 3-Sep-18 5:38am    
what is double value; for? how do i get the value from the sql server datatable exactly..
OriginalGriff 3-Sep-18 6:20am    
When you use TryParse, it store the result in a variable you pass to it via the out parameter, and returns a true/false for "good/bad conversion"
double value;
is just declaring the variable to stick the converted value into, if the conversion works.
Muhammad nur Ihsan 3-Sep-18 5:51am    
its really hard to understand i just wanted to get the value from the datatable to my textbox..
OriginalGriff 3-Sep-18 6:21am    
That' more complex than you think, because a DataTable doesn't contain just one value - it's a Table - which means it contains rows of columns of data.
The first thing you need to work out is which row do you need, and which column of that row?
Muhammad nur Ihsan 3-Sep-18 6:38am    
alright so i have 2 column there is "Products" and "Price" i only have 5 rows one of em is "BasoSU" it has "2000" for price, like i want to multiply his price with the whatever number inputed from the textbox.... pls give an example again.. what do evrything i need to add to the program.

and btw the class DAL is for the login & register form do i need to create a class again for this problm?
Hell yes i got my solution!!

Form1 load

public Meja1()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection(@"Data Source = PC\SQLEXPRESS; Initial Catalog = MyLoginApp; Integrated Security = True");
   
    private void Form1_Load(object sender, EventArgs e)
        {
            con.Open();
            string query = "SELECT * FROM products";
            
            SqlDataAdapter SDA = new SqlDataAdapter(query, con);
            DataTable dt = new DataTable();
            SDA.Fill(dt);
            dataGridView1.DataSource = dt;

            string query2 = "SELECT harga FROM products WHERE Product = 'BasoSU'"; 
            DataSet ds = new DataSet();
            lblBSU.Text = dataGridView1.Rows[0].Cells["Harga"].Value.ToString();
          }


Form1 Button

 private void btnHitung_Click(object sender, EventArgs e)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();
            DataTable dt = DataAccess.ExecSP("Products", sqlParams);
            SqlConnection conn = new SqlConnection();

            SqlDataAdapter adp = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand("select * from products",conn);
            adp.SelectCommand = cmd;
            DataSet ds = new DataSet();
            
//declare
Double BasoSUCost; Double ItemBasoSU; Double HargaBasoSU;

if (int.TryParse(txtBasoSU.Text, out NumberofOrder))
            {
                ItemBasoSU = Double.Parse(txtBasoSU.Text);
                BasoSUCost = Double.Parse(lblBSU.Text);
                HargaBasoSU = BasoSUCost * ItemBasoSU; 

             txtTotal.Text = String.Format("Rp. {0:N} ", HargaBasoSU);


this is what i want it looks weird but it works! haha
im using gridview and label to use it in arithmetic lol
 
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