Updating the first row only?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The flow of my app is as follows
1) Pull up an order and change the quantity of a textbox item. (Ex: change the quantity from 1 to 2
2) Click on the Update button
3) When the page posts back you should see the updated quantity of the item. (by the way, in some cases there will be multiple lines for an order...this is where the problem comes in.

What happens is that the process works...for the first row of data only. I need the app to update all lines of an order, not just the first line. Below is the problematic code

//Code Star
for(int counter = 0; counter < tbl.Rows.Count; counter++) //I have verified that the count of table rows is correct, no matter the amount...so the issue is not with the row coun

objCommand.CommandText = "update_qp"; //this is the stored procedure which is a simple update statement. there is nothing to it
objCommand.CommandType = CommandType.StoredProcedure
objCommand.Parameters.Clear(); //I clear the parameters just in case
objCommand.Parameters.Add("@Quantity", tbl.Rows[counter]["Quantity"]); //if you set the counter variable to 1 it will update the second order line correctly, if you set the counter variable to 2 it will update the third order line correctly, etc. this is good because it shows that the data is there and it is correct. However, by default (i.e. leave it set to tbl.Rows[counter]) it will only display the first row (i.e. tbl.Rows[0]) , even though this code is contained within a looping structure
objAdapter.UpdateCommand = objCommand
objAdapter.Update(tbl)

//Code En

Does anyone know what is going on? The data is all there, it recognizes how many rows the table has, and it updates perfectly if you hardcode the row numbers. So why won't this work by looping through? Does it have something to do with the objAdapter.Update?
 
I'm confused - you are setting a stored procedure on a SqlCommand object -
but then using a data adapter to do the updating?

When you call Update on the data adapter, it does the update on ALL the rows
in the entire table. After that, it calls AcceptChanges, which resets the
state of all the rows to unchanged. Any other Updates you call after this
in your loop, won't do anything - as all the rows are marked as unchanged,
hence, there is nothing to update. So if you command is data adapter's
update command is objCommand, then, the update will happen once - with the
value at row 0 being used for all the rows in the table.

George said:
The flow of my app is as follows:
1) Pull up an order and change the quantity of a textbox item. (Ex: change the quantity from 1 to 2)
2) Click on the Update button.
3) When the page posts back you should see the updated quantity of the
item. (by the way, in some cases there will be multiple lines for an
order...this is where the problem comes in.)
What happens is that the process works...for the first row of data only. I
need the app to update all lines of an order, not just the first line. Below
is the problematic code.
//Code Start
for(int counter = 0; counter < tbl.Rows.Count; counter++) //I have
verified that the count of table rows is correct, no matter the amount...so
the issue is not with the row count
{
objCommand.CommandText = "update_qp"; //this is the stored procedure which
is a simple update statement. there is nothing to it.
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.Clear(); //I clear the parameters just in case.
objCommand.Parameters.Add("@Quantity", tbl.Rows[counter]["Quantity"]);
//if you set the counter variable to 1 it will update the second order line
correctly, if you set the counter variable to 2 it will update the third
order line correctly, etc. this is good because it shows that the data is
there and it is correct. However, by default (i.e. leave it set to
tbl.Rows[counter]) it will only display the first row (i.e. tbl.Rows[0]) ,
even though this code is contained within a looping structure.
objAdapter.UpdateCommand = objCommand;
objAdapter.Update(tbl);
}
//Code End

Does anyone know what is going on? The data is all there, it recognizes
how many rows the table has, and it updates perfectly if you hardcode the
row numbers. So why won't this work by looping through? Does it have
something to do with the objAdapter.Update?
 
Marina

It turned out that objAdapter.Update was indeed the problem. It should have been objAdapter.Fill. Now it works perfectly

----- Marina wrote: ----

I'm confused - you are setting a stored procedure on a SqlCommand object
but then using a data adapter to do the updating

When you call Update on the data adapter, it does the update on ALL the row
in the entire table. After that, it calls AcceptChanges, which resets th
state of all the rows to unchanged. Any other Updates you call after thi
in your loop, won't do anything - as all the rows are marked as unchanged
hence, there is nothing to update. So if you command is data adapter'
update command is objCommand, then, the update will happen once - with th
value at row 0 being used for all the rows in the table

George said:
The flow of my app is as follows
1) Pull up an order and change the quantity of a textbox item. (Ex: chang the quantity from 1 to 2
2) Click on the Update button
3) When the page posts back you should see the updated quantity of th
item. (by the way, in some cases there will be multiple lines for a
order...this is where the problem comes in.need the app to update all lines of an order, not just the first line. Belo
is the problematic code
for(int counter = 0; counter < tbl.Rows.Count; counter++) //I hav
verified that the count of table rows is correct, no matter the amount...s
the issue is not with the row coun
objCommand.CommandText = "update_qp"; //this is the stored procedure whic
is a simple update statement. there is nothing to it
objCommand.CommandType = CommandType.StoredProcedure
objCommand.Parameters.Clear(); //I clear the parameters just in case
objCommand.Parameters.Add("@Quantity", tbl.Rows[counter]["Quantity"])
//if you set the counter variable to 1 it will update the second order lin
correctly, if you set the counter variable to 2 it will update the thir
order line correctly, etc. this is good because it shows that the data i
there and it is correct. However, by default (i.e. leave it set t
tbl.Rows[counter]) it will only display the first row (i.e. tbl.Rows[0])
even though this code is contained within a looping structure
objAdapter.UpdateCommand = objCommand
objAdapter.Update(tbl)

//Code En
how many rows the table has, and it updates perfectly if you hardcode th
row numbers. So why won't this work by looping through? Does it hav
something to do with the objAdapter.Update
 
Back
Top