My application is near completion and currently being beta tested at my work place by others. While on my development system everything seems stable, in the wild it's shown to be less stable than I thought.
Hence I'm wanting to write some robust error handling into the application. Right now the largest instances of errors happen when doing an 'insert' or 'update'.
So far I've managed to loose server connections on a few occasions through unmanaged insert / update errors, resulting in server restarst to fix.
I've added code below and I an wondering whether I'm on the right track, or if there is a better way to handle insert, update errors.
Any advice appreciated..
@{
var userErrMsg = "";
var errMsg = "";
var actiontype = Request["action-type"];
var projectno = Request["projectno"];
var description = Request["Description"];
var db = Database.Open(App.conDatabase);
try {
var row = processSQL.getUsername(App.conDatabase, WebSecurity.CurrentUserName);
if(actiontype == "new")
{
var author = row.Username;
var createddate = DateTime.Now;
var sql = "INSERT INTO tblProjectBenefits (Description, ProjectRef, Author , CreatedDate) VALUES (@0, @1, @2, @3)";
db.Execute(sql, description, projectno ,author, createddate);
}
//edit user post
else if(actiontype == "edit")
{
var id = Request["ID"];
var updateby = row.Username;
var updatedate = DateTime.Now;
var sql = "UPDATE tblProjectBenefits SET Description = @0, ProjectRef = @1, UpdateBy = @2, UpdateDate = @3 WHERE ID = @4";
db.Execute(sql, description, projectno, updateby, updatedate, id);
}
else if(actiontype == "delete")
{
var id = Request["ID"];
var sql = "DELETE FROM tblProjectBenefits WHERE ID=@0";
db.Execute(sql, id);
}
}
catch (Exception ex) {
// You can use the exception object for debugging, logging, etc.
errMsg = ex.Message;
// Create a friendly error message for users.
userErrMsg = "An exception has occured, please contact "
+ "your system administrator.";
}
finally
{
//Any cleanup code
db.Close();
}
}
2bitcoder
Member
99 Points
69 Posts
error handling advice
Apr 10, 2012 11:11 AM|LINK
Hello,
My application is near completion and currently being beta tested at my work place by others. While on my development system everything seems stable, in the wild it's shown to be less stable than I thought.
Hence I'm wanting to write some robust error handling into the application. Right now the largest instances of errors happen when doing an 'insert' or 'update'.
So far I've managed to loose server connections on a few occasions through unmanaged insert / update errors, resulting in server restarst to fix.
I've added code below and I an wondering whether I'm on the right track, or if there is a better way to handle insert, update errors.
Any advice appreciated..
@{ var userErrMsg = ""; var errMsg = ""; var actiontype = Request["action-type"]; var projectno = Request["projectno"]; var description = Request["Description"]; var db = Database.Open(App.conDatabase); try { var row = processSQL.getUsername(App.conDatabase, WebSecurity.CurrentUserName); if(actiontype == "new") { var author = row.Username; var createddate = DateTime.Now; var sql = "INSERT INTO tblProjectBenefits (Description, ProjectRef, Author , CreatedDate) VALUES (@0, @1, @2, @3)"; db.Execute(sql, description, projectno ,author, createddate); } //edit user post else if(actiontype == "edit") { var id = Request["ID"]; var updateby = row.Username; var updatedate = DateTime.Now; var sql = "UPDATE tblProjectBenefits SET Description = @0, ProjectRef = @1, UpdateBy = @2, UpdateDate = @3 WHERE ID = @4"; db.Execute(sql, description, projectno, updateby, updatedate, id); } else if(actiontype == "delete") { var id = Request["ID"]; var sql = "DELETE FROM tblProjectBenefits WHERE ID=@0"; db.Execute(sql, id); } } catch (Exception ex) { // You can use the exception object for debugging, logging, etc. errMsg = ex.Message; // Create a friendly error message for users. userErrMsg = "An exception has occured, please contact " + "your system administrator."; } finally { //Any cleanup code db.Close(); } }ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: error handling advice
Apr 10, 2012 12:37 PM|LINK
If I do not know what is the error, I would not catch.
2bitcoder
Member
99 Points
69 Posts
Re: error handling advice
Apr 11, 2012 10:27 AM|LINK
Okay, be specific about what errors I want to catch..
Should I also be closing the db connection everytime I make a call?
does the finally { } run on every connection even if there is no failure?
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: error handling advice
Apr 17, 2012 02:13 AM|LINK
Hi
For the exception you should know what exception may cause, then right like this.
catch (InvalidCastException e) //http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx { }And for finally{ } it will run in any situation, so put db.close in finally is right.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework