Solved

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

Posted on 2011-09-07
3
213 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now