Solved

How can I see all recs that were affected in SQL Update Statement

Posted on 2011-09-07
3
219 Views
Last Modified: 2012-06-27
In my ASP.NET app, I have a function that allows users to update the status of certain records.  After the Update, I want to populate a gridview that shows the user all of the records that were updated.  What's a good way to do that based off my current Update code (below).  Thanks -
string strSql = "Update metadata set Status = 'Active' where Status = 'XL_Import'";
        try
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSql.ToString();
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            string errorMsg = "Error in Updation";
            errorMsg += ex.Message;
            throw new Exception(errorMsg);
        }
        finally
        {
            con.Close();
        }

Open in new window

0
Comment
Question by:JT_SIRO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
AJRDev earned 125 total points
ID: 36499503
ExecuteNonQuery(), or any of the other ADO.NET SQL methods, can only ever return to you the number of records affected - never which records!

The only way to do this is with logic at the SQL level. You need to write a stored procedure that creates a temporary table, populates this table with the affected record id's, and returns that to your application.
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36501686
@@RowCount will give you the number of records affected by a SQL Statement.

The @@RowCount works only if you issue it immediately afterwards. So if you are trapping errors, you have to do it on the same line. If you split it up, you will miss out on whichever one you put second.

SELECT @NumRowsChanged = @@ROWCOUNT

Write Stored procedure for this. hope this will work.

Thanks
0
 
LVL 7

Assisted Solution

by:gopaltayde
gopaltayde earned 125 total points
ID: 36516107
Create a copy of metadata table, with some more column like Operation, OprationDate etc, Write an after trigger on metadata table which will populate this newly created table.
A sample example -
http://weblogs.asp.net/jgalloway/archive/2008/01/27/adding-simple-trigger-based-auditing-to-your-sql-server-database.aspx
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question