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(); } }