Click here to Skip to main content
16,012,044 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
mine problem is: i have customer table tblPurchaseDetails while inserting data in this table same time i need to update in store table i.e tblMasterDetails from from same place also its should check store if it is empty it should just insert item else if it should update value to store by summing store value to current customer value...
for example
customer item=100kg rice 
 store item =50kg 
finally store=150kg

if store is empty
then store =100kg


Code
C#
try
               {
                       connection.Open();
                       OleDbCommand cmd = new OleDbCommand("Insert into tblPurchaseDetails (fldId,fldCustomerName,fldCustomerAddress,fldCustomerPhone,fldDate,fldVehicleNo,fldItemName,fldPerBag,fldNetBag,fldNetQuantity,fldUnitRate,fldNetRate,fldStockId) values ('" + txtid.Text + "','" + txtname.Text + "','" + txtaddress.Text + "','" + txtphone.Text + "','" + txtdate.Text + "','" + txtVehicleNo.Text + "','" + txtItemName.Text + "','" + txtPerbag.Text + "','" + txtNetbag.Text + "','" + txtNetQuantity.Text + "','" + txtUnitRate.Text + "','" + txtNetRate.Text + "','" + cmbId.Text + "')", connection);
                       cmd.ExecuteReader();
                       connection.Close();
                       CustomerCode();
               }
               catch (OleDbException ex)
               {
                   if (ex.ErrorCode == -2147467259)
                   {
                       OleDbCommand cmd = new OleDbCommand("Update tblPurchaseDetails SET fldCustomerName='"+txtname.Text+"',fldCustomerAddress='"+txtaddress.Text+"',fldCustomerPhone='"+txtphone.Text+"',fldDate='"+txtdate.Text+"',fldVehicleNo='"+txtVehicleNo.Text+"',fldItemName='"+txtItemName.Text+"',fldPerBag='"+txtPerbag.Text+"',fldNetBag='"+txtNetbag.Text+"',fldNetQuantity='"+txtNetQuantity.Text+"',fldUnitRate='"+txtUnitRate.Text+"',fldNetRate='"+txtNetRate.Text+"',fldStockId='"+cmbId.Text+"' Where fldId='"+txtid.Text+"' ");
                       cmd.ExecuteNonQuery();
                   }

                   else

                        MessageBox.Show(ex.Message);
               }
               finally
               {
                   connection.Close();
               }
               try
               {
                   connection.Open();

                   OleDbCommand cod = new OleDbCommand("Select fldItemName,fldNetBag,fldNetQuantity from tblMasterDetails Where fldItemName='"+txtItemName.Text+"'",connection);
                   OleDbDataReader res=cod.ExecuteReader();
                   while (res.Read())
                   {
                       int result = res.GetInt32(0);
                       if (result == null)
                       {
                           OleDbCommand cmd = new OleDbCommand("Insert into tblMasterDetails (fldItemName,fldNetBag,fldNetQuantity) Values('" + txtItemName.Text + "','" + txtNetbag.Text + "' ,'" + txtNetQuantity.Text + "')", connection);
                           cmd.ExecuteNonQuery();
                       }
                       else
                       {
                           OleDbCommand con = new OleDbCommand("Update tblMasterDetails SET fldNetBag='" + txtNetbag.Text + "' , fldNetQuantity='" + txtNetQuantity.Text + "' Where fldItemName='" + txtItemName.Text + "'", connection);
                           con.ExecuteNonQuery();
                       }
Posted
Updated 11-Jun-15 3:25am
v5
Comments
Torakami 11-Jun-15 8:59am    
I have tried to update his question , but something went wrong. Pre tags not working properly .

Please any one uopdate his question
CHill60 11-Jun-15 9:36am    
Done :)
Torakami 11-Jun-15 10:58am    
thanks man
[no name] 11-Jun-15 9:00am    
You need to use parameterized queries to avoid sql injection attacks. And you need to update your posting to include a description of an actual problem instead of just dumping a bunch of unformatted code here without any explanation.
Member 11758806 11-Jun-15 9:06am    
ok, sir..plz help its very urgent

1 solution

Have you looked at Triggers, you can achieve this behavior with the help of triggers. On the Primary Table i.e. tblPurchaseDetails , one must create a Insert Trigger, inside the trigger logic, you can check the value in Magic Tables i.e. inserted and write the logic for the secondary table i.e. tblMasterDetails insert.


For Triggers refer to https://msdn.microsoft.com/en-us/library/ms189799.aspx[^]
 
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