Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 22nd, 2012, 01:29 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Update multiple rows at once with different values

Hello all,

I have a table named EmpTab. I have few records in it.

I want to update some of those records at once that is when Save buttons is clicked then the few records must be updated. The whole table (all records are splitted in many web forms) it means page 1 contains 1-6 records to update, page 2 contains 7-10 records to update.

So how multiple records can be updated with different values by a single page on button's click event.

Table structure is like this

Id SettingName Value

SettingName contains the setting name, and value contains the actual value.

Please help me, I am stuck on it for many days...
 
Old February 23rd, 2012, 05:33 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hi there

you can't update a set of records with different conditions at once

update each row:

Update tbl1 set x = 'a' where id=1
Update tbl1 set x = 'b' where id=2
Update tbl1 set x = 'c' where id=3


batch update:
update tbl1 set x = 'y' where date > '2010-10-10'
__________________
happy every time, happy every where

Reza Baiat
 
Old February 23rd, 2012, 09:52 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

isn't is possible with case?
Thanks
 
Old February 25th, 2012, 07:09 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile YES. case

Here is what you should do instead:

UPDATE categories
SET display_order = CASE id
WHEN 1 THEN 3
WHEN 2 THEN 4
WHEN 3 THEN 5
END
WHERE id IN (1,2,3)

see this link:
http://www.catswhocode.com/blog/10-s...-your-database
__________________
happy every time, happy every where

Reza Baiat
 
Old February 25th, 2012, 10:24 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi,
I found that link very early that is why I asked about case. But I am stuck on how I pass the multiple records to some function (stored procedure) that are to be updated.
Thanks...
 
Old March 1st, 2012, 02:46 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Please anybody give suggestions.

I want to update multiple rows at once not the multiple columns.
Thanks
 
Old March 1st, 2012, 02:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Why not simply execute the stored procedure multiple times? Or use a DataSet and update the values and save them. Or use EF which handles all of this pretty nicely.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 1st, 2012, 03:53 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Greetings Imar,

Quote:
Why not simply execute the stored procedure multiple times?
if I am not wrong then won't it hit database multiple times?

Quote:
use EF which handles all of this pretty nicely.....
how EF can do this, would it handle multiple rows or multiple columns?

My scenario is:
Table Structure: Id SettingName Value
Now values in table are as follows

1 CommentsAllowed No(0)
2 SentEmailAfterComment Yes(1)
3 CommentsPerPage 10
4 ReviewsPerPage 5
5 ShowNonAuthReviews No(0)

etc....

So if in a web form I want to update first 3 rows and on second web form I want to update last 2 rows, then is it possible with EF?

Please reply
Thanks
 
Old March 1st, 2012, 06:52 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hi Sophia

EF does not apply changes as done. when you use ef_instance.SubmitChanges() then updated records will be updated in Database. this is because of when you change a property value of some model instance, INotifyPropertyChanging and INotifyPropertyChanged interface implemented class (EF) marks the model to be updated when SubmitChanges is called.
so you can updated multiple rows at once. I thought that you want to do it only by StoredProcedure
__________________
happy every time, happy every where

Reza Baiat
 
Old March 1st, 2012, 12:18 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Can you please give some sample code from getting values from server controls (textbox & checkbox etc) from web form to passing these values to stores procedures and save them?

Please help.
Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
update rows with vbscript in asp sanjeev jha Classic ASP Professional 1 November 12th, 2007 03:24 AM
Highlighting multiple rows nzhang ASP.NET 2.0 Basics 1 June 21st, 2006 11:12 PM
print multiple times..multiple rows... abhit_kumar JSP Basics 3 January 18th, 2005 07:11 PM
Updating multiple Rows from multiple fields in ASP vdm_nana SQL Server ASP 0 April 1st, 2004 04:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.