Click here to Skip to main content
15,909,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Iam doing one task where i want to executed update query i have table name is Medicine_Available_Detail here iam storing total medicine available column name is Availability,I have one datagridview where im putting Quantity of that Medicine after user enter Quantity of particular medicine then it table update as Availability-Quantity for that medicine i written a code but its not executed

What I have tried:

C#
int quantity = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value);

                            cmd = new OleDbCommand(@"update Medicine_Available_Detail set [Availability]=[Availability]-@Quantity where Medicine_Name=@Medicine_Name", con);
                            cmd.Parameters.AddWithValue("@Quantity", quantity);
                            cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);

con.Open();
                            int n = cmd.ExecuteNonQuery();
                            con.Close();
                            MessageBox.Show("Record Updated Successfully");
Posted
Updated 19-Feb-17 6:54am
Comments
Richard MacCutchan 16-Feb-17 11:42am    
Why are you displaying "Record Updated Successfully" when you have no idea whether it succeeded or not?
Atul Rokade 16-Feb-17 12:12pm    
for testing purpose i done but previously code was executed as expect but now not understand where i done mistake
Richard MacCutchan 16-Feb-17 12:20pm    
Sorry, but we cannot guess why it is not working. Do not post messages like that as they are just a waste of time. Add some proper error checking into your code and use your debugger to find out exactly what is happening.
Atul Rokade 16-Feb-17 12:28pm    
Sorry im waste your precious time i will check and post as answer as soon as sorry once again
Richard MacCutchan 16-Feb-17 13:44pm    
No, you are wasting your time.

Putting code like that in order to test your application is just a bad idea. The whole point of testing is to find errors in your code. So if you ignore errors in the testing they just sit there waiting for product release, at which time they cause absolute chaos.

First of all, to know if any row is updated or not, check value of your variable
n

if it is 0 means no record is matching the criteria you are defining for update.
So check the value of varible
medicinename
in your table exists on not.

This is all can your suggest by looking at your code.
 
Share this answer
 
Hi here is find solution i use two cmd.ExecuteNonQuery();its working fine for me ,dataGridView1_RowLeave event i written 3 insert query and one update query and only one ExecuteNonQuery instead using one Execute non query i written two seprate ExecuteNonQuery with different variable

C#
  cmd = new OleDbCommand("update Medicine_Available_Detail set [Availability]=[Availability]-@Quantity where [Medicine_Name]=@Medicine_Name", con);
                            cmd.Parameters.AddWithValue("@Quantity", quantity);
                            cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);
con.Open();

                            int n1 = cmd.ExecuteNonQuery();
con.Close();
 
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