Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a Text box. I want to Update that quantity of rows from the database the amount I write in the textbox. how will I do it. Please tell the query
Posted
Comments
OriginalGriff 8-Sep-14 3:52am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Member 10578683 8-Sep-14 4:02am    
con.Open();
string query2 = "update TOP(1) Inventory set P='" + Label29.Text + "' where [Product]='" + Product.Text + "' and Status='" + Status.Text + "'";
SqlCommand cmd = new SqlCommand(query2, con);
cmd.ExecuteNonQuery();
con.Close();
Hello sorry to not give u proper information
This is my code. Instead of updating the TOP(1) row I want to UPDATE that number of row that are in textbox
Prasad Avunoori 8-Sep-14 3:55am    
????????????????????????????

Re: Solution 1.
This is very, very dangerous!
Because you cannot use ORDER BY with an UPDATE command, and SQL is at liberty to process rows in any order unless a specific order is given, you can't guarantee which rows will be updated at all. It may work now, and then fail in spectacular ways in production.

A better solution is to use a cursor, as explained here: http://stackoverflow.com/questions/3439110/sql-server-update-a-table-by-using-order-by[^]
 
Share this answer
 
following C# statement may help
C#
sting Query = "update top (" + txtbox1.Text + ") TableName set Password = '" + txtPassword.Text + "' " 

so lets say you have entered 5 in txtbox1 and "abc" in the txtPassword then
Query would be
SQL
update top (5) TableName set Password = 'abc'
 
Share this answer
 
Comments
Member 10578683 8-Sep-14 4:10am    
Thank U. this is exactly the same I want
OriginalGriff 8-Sep-14 4:23am    
Not a good idea: You can't specify the order in which the rows are selected, so you don't know which are going to be the "top 5"
Jawad Shairf 8-Sep-14 5:19am    
i agree. its not a good practice
but he may be trying to understand some concept thats why... :)
OriginalGriff 8-Sep-14 5:21am    
Doesn't matter - understanding something via a method that will give problems later is not good! :laugh:

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