Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a form in visual studio 8.0. In my form I have
two text field(ID,amount),
a button(Order). I have table in access having two Field
ID and amount and having two records. First record 1 in ID and 40 in amount. second record 2 in ID and 100 in amount.
when I Enter 1 in text Field (ID) 20 in text Field (amount), and then press button order.
I want it to be added with the previous Amount that have ID 1 Saved in the table.Means If previous amount that have ID 1 is 40
then the result after saving must be 60.

when I Enter 2 in text Field (ID) 200 in text Field (amount), and then press button order.
I want it to be added with the previous Amount that have ID 2 Saved in the table.Means If previous amount that have ID 1 is 100
then the result after saving must be 300.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jul-11 13:14pm    
Do not re-post. Always use "Improve question" on original one.
--SA

If your table is stored in SQL Server then you can use

SQL
IF (EXISTS (SELECT * FROM MY_TABLE AS t1
  WHERE t1.ID = ID_VALUE))
begin
  UPDATE MY_TABLE
  SET AMOUNT = AMOUNT + AMOUNT_VALUE
  WHERE ID = ID_VALUE
end
else
begin
  INSERT INTO MY_TABLE (ID, AMOUNT)
  VALUES(ID_VALUE, AMOUNT_VALUE)
end
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OleDbCommand ocmnd;
        OleDbConnection oconn = null;
        
        string ConectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\TestDB.mdb";
        private void btnOrder_Click(object sender, EventArgs e)
        {
            int intID = Int32.Parse(this.txtID.Text);
            decimal decAmount = Decimal.Parse(this.txtAmount.Text);
            oconn = new OleDbConnection(ConectionString);

            string query1 = "Select * from table1 where ID = NULL OR ID =" + intID;
            ocmnd = new OleDbCommand(query1, oconn);
            
            try
            {
                oconn.Open();
                DataSet DS = new DataSet();
                OleDbDataAdapter myDA = new OleDbDataAdapter(ocmnd);
                myDA.Fill(DS);
                DataTable DT = DS.Tables[0];
                if (DT.Rows.Count > 0)
                {
                    foreach (DataRow DR in DT.Rows)
                    {
                        int IDValue = Int32.Parse(DR.ItemArray[0].ToString());
                        decimal AmountValue = Decimal.Parse(DR.ItemArray[1].ToString());
                        if (IDValue == intID)
                        {
                            AmountValue += decAmount;
                            ocmnd.CommandText = "Update table1 set Amount =" + AmountValue + " Where ID = " + IDValue;
                            ocmnd.ExecuteNonQuery();    
                        }
                    }
                    
                }
                else
                {
                    ocmnd.CommandText = "Insert Into table1(ID,Amount) values (" + intID + "," + decAmount + ")";
                    ocmnd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                
                oconn.Close();
            }

        }
    }
}
 
Share this answer
 
v3
Comments
sokkong 19-Jul-11 10:34am    
it's Java ?

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