Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to update one cell in the row the user selects. So when the start button is clicked the cell in that row is updated to say 'production started'

However, when I check the database, every cell under the specific column has updated and has 'production started') instead of only the cell in the column of the row click
So the update has been applied to all cells under that column ... can anyone help ?


private void Start_Click(object sender, EventArgs e)
{
    MAcon.Open();
    OleDbCommand cm = new OleDbCommand("UPDATE [Production] SET [Product Status]=@ProductStatus WHERE [OrderID]=@OrderID", MAcon);
    cm.Parameters.AddWithValue("@ProductStatus", Access.Text);
    cm.Parameters.AddWithValue("@OrderID", OrderIDTxt.Text);
    cm.ExecuteNonQuery();
    MAcon.Close();
}





So i.e. row one is selected - when user clicks start - row 1, product status should say 'Production started'

What I have tried:

{
    MAcon.Open();
    OleDbCommand cm = new OleDbCommand("UPDATE [Production] SET [Product Status]=@ProductStatus WHERE [OrderID]=@OrderID", MAcon);
    cm.Parameters.AddWithValue("@ProductStatus", Access.Text);
    cm.Parameters.AddWithValue("@OrderID", OrderIDTxt.Text);
    cm.ExecuteNonQuery();
    MAcon.Close();
}
Posted
Updated 6-May-18 8:06am
v5
Comments
Wendelius 6-May-18 9:48am    
Cn you post example data and also an example what should be updated
Member 13765884 6-May-18 9:55am    
Done, I'm not sure if its the type of example you were looking for though
Wendelius 6-May-18 11:24am    
I'm trying to get better understanding of the problem. Can you explain in more detail what you're trying to do and what doesn't work?
Member 13765884 6-May-18 11:43am    
I have improved the question now

Check what value is in the OrderID column of your data - your command will update every row where the OrderID matches the value you gave it in the OrderIDText TextBox.
 
Share this answer
 
Comments
Maciej Los 7-May-18 13:46pm    
5ed!
You need to comfirm that "Access.Text" actually contains what you think it does.

cm.Parameters.AddWithValue("@ProductStatus", Access.Text);
 
Share this answer
 
Comments
Maciej Los 7-May-18 13:46pm    
5ed!
Assuming an order have more that 1 product, you need to tell which product or row in the order you want to update, otherwise all products of order are updated.
This SQL code updates only 1 row.
You need to check if this code is executed more than once.
One way to do it is to add a textbox and append some text each time the code is executed. include the OrderID.
Another way is to use the debugger to see what is doing your code.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
Comments
Member 13765884 6-May-18 9:39am    
Thank you, I'm not sure I understand what you mean.
Each order number cannot be duplicated
How would I do that please?
Patrice T 6-May-18 9:58am    
Define what is in an order.
Are you displaying a datagrid ?
do you display 1 row per order ?
Three things that come in mind..

As suggested the command could update more rows than you expect. The ExecuteNonQuery returns an integer, how many rows were affected by the command. You should investigate that it returns 1. For example:
C#
...
int affected = cm.ExecuteNonQuery();
if (affected != 1) {
   MessageBox.Show(affected.ToString());
}
...


Also as suggested, there is a possibility that the code is executed for all the rows. The easiest way to see this is by setting a breakpoint to the beginning of the method and see when the breakpoint is hit.

The third thing is the query when you fetch the data. You haven't posted the actual SELECT which fetches the data so it could be that you actually fetch the status from a wrong row.
 
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