Last post Jan 15, 2015 07:58 AM by mdehghani
Jan 14, 2015 09:19 PM|mdehghani|LINK
hello everyone.. how should we check if a row has been affected after the following INSERT statement so that we can create an if statement and display a success or failure message?
//add row to table
var sql = "INSERT INTO Table (Q1,Q2,Q3, StudentId) VALUES (@0,@1,@2,@3)";
// Check if the row has been affected.
if (? ? ? ?)
Jan 14, 2015 10:42 PM|wavemaster|LINK
try catch block?
your sql select here
catch (Exception ex)
figure out what exception is thrown for the various problems you may encounter with those few lines of code above.
Jan 15, 2015 03:10 AM|mdehghani|LINK
Jan 15, 2015 03:46 AM|Mikesdotnetting|LINK
In your example, you can get the return value of the Database.Execute method which will provide the number of affected rows:
var rowsAffected = db.Execute(sql, ...);
The result should always be 1 for an insert statement, so retrieving that value seems pretty pointless to me. It would be more useful if you were conducting an UPDATE or DELETE statement.
The try-catch block should be used to provide you with control over any possible exception that could be raised during the database operation.
Jan 15, 2015 07:58 AM|mdehghani|LINK
Thanks.. so I decided to just move on , showing the success message without first checking what happened!