Link to home
Start Free TrialLog in
Avatar of JT_SIRO
JT_SIRO

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of AJRDev
AJRDev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@@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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial