c# LinqToSql exception handling when database is offline - c#

Building an C# MVC webapplication with Linq to Sql for querying the SQL database.
I am trying to catch an exception if the database should go offline to avoid the yellow screen of death.
After debugging, I found two things. First, the creation of the DataContext does not check if the sql server is available. Second, there is an Error message (Or an exception!?) stored in the rows variable.
The Catch statement in the code below is never reached. So I get the Yellow screen of death when trying to read data from the Model in the View.
Why isn't the Catch statement reached?
Code:
try
{
//Creating DB Context
var con = ConfigurationManager.ConnectionStrings["teststring"].ConnectionString;
TestDataContext db = new TestDataContext(con);
//Querying database. This should cause an exception to be thrown!?
var rows = from s in db.Table
orderby s.Id descending
select s;
//Returning the View with the data
return View(rows);
}
catch (Exception ex)
{
ErrorInfo err = new ErrorInfo("Something went wrong when trying to query the database. See the log for details.");
err.WriteToErrorLog(ex);
return View("Error", err);
}

have you tried to use SQLException
try
{
//Creating DB Context
var con = ConfigurationManager.ConnectionStrings["teststring"].ConnectionString;
TestDataContext db = new TestDataContext(con);
//Querying database. This should cause an exception to be thrown!?
var rows = from s in db.Table
orderby s.Id descending
select s;
//Returning the View with the data
return View(rows);
}
catch (SqlException ex)
{
ErrorInfo err = new ErrorInfo("Something went wrong when trying to query the database. See the log for details.");
err.WriteToErrorLog(ex);
return View("Error", err);
}

Firstly: as #ofir mentioned you should call .ToList() method in order to execute LINQ statement. so you should call it on your try block to be able to catch the exception.
Secondly:
you shouldn't show Yellow error screen to end users because of security reasons, you can enable CustomError in web.config file to show a default page when an exception is thrown and redirect it to an Error page
<customErrors mode="On" defaultRedirect="~/ErrorPages/GeneralError">
</customErrors>

Related

TransactionScope.Complete() returns error

I am facing some strange error while executing following code.
I debugged all methods and found no error but scope.Complete(); line returns an error "Transaction has aborted" and InnerException is "The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION."
InsertEntries(myObject) calls few more methods within for making entries to near about 12-15 different tables in SQL Server. It used StoredProcedues to do that.
I am using EntityFramework and SQLServer.
NOTE:
1 - Everything works fine without TransactionScope.
2 - Error returns on scope.Complete(); line. No other errors.
using (TransactionScope scope = new System.Transactions.TransactionScope()
{
try
{
myObject.IsMain = false;
InsertEntries(myObject);
myObject.IsDuplicateNumber = IsDuplicateNumber;
myObject.RecNum = RecNum;
myObject.NextNumber = nextNumber;
scope.Complete();
}
catch (Exception ex)
{
throw ex;
}
}

How to get user defined SQLException number with Dapper

My stored procedure is throwing custom errors to handle validation within a multi user web app. This is working as expected in SQL Server with error number 50001 being returned however when my C# code catches the error it always has the error number 50000 instead of 50001 therefore I cannot treat the errors differently.
How can I catch error number 50001? Note that I am using Dapper to execute all stored procedures.
SQL
THROW 50001, 'Client already has an Active Visit! THIS IS DEV!!!!',1;
Msg 50001, Level 16, State 1, Line 1
Client already has an Active Visit! THIS IS DEV!!!!
C#
catch (SqlException ex)
{
var errorHandler = new ErrorHandler();
var msg = errorHandler.ErrorMessage(ex);
if (ex.Number == 50001)
{
return BadRequest(msg);
}
else
{
return StatusCode(500, msg);
}
}
catch (Exception ex)
{
var errorHandler = new ErrorHandler();
return StatusCode(500, errorHandler.ErrorMessage(ex));
}
EXAMPLE
SQL server error 50000 is reserved for general user defined message, so it sounds like it's not finding error 50001 in sys.messages. You can try to add the error using the stored procedure sp_addmessage.
Once that is done, you can call it like this:
RAISERROR(50001, 1, 1)
Another reason it could be error 50000 is if you are raising the SQL error in a SQL TRY/CATCH as that will always return a 50000 error code.
Just tried this code myself:
using (var cmd = new SqlCommand("THROW 50001,'error',1;", conn))
{
cmd.ExecuteNonQuery();
}
And that does return ex.Number 50001 so it must be a problem in your SQL. Like I said above, it could be caused by a TRY/CATCH as this won't save the original error code number.

EF EntityState.Modified Try Catch Issue

I have a code like this:
try
{
Member member = database.Members.Where(m=>m.ID=1).FirstOrDefault();
member.Name = "NewMemberName";
database.Entry(member).State = EntityState.Modified;
database.SaveChanges();
}
catch (Exception ex)
{
database.Logs.Add(new Log() { Value=ex.ToString() });
database.SaveChanges();
}
And Entity:
[StringLength(5)]
public string Name { get; set; }
If the Name String more than 5 it would be error and catch the exception ,but when I add a log then save ,the exception from SaveChange(); still remains,how should I do?(Can't change the schema)
the exception from SaveChange(); still remains
Well, if this throws an exception:
database.SaveChanges();
Then there's a pretty good chance that this will also throw an exception:
database.SaveChanges();
Basically, in your catch block you shouldn't be immediately re-trying the operation that just failed a millisecond ago. Instead, log the failure and handle the exception:
catch (Exception ex)
{
// DO NOT call SaveChanges() here.
}
Of course, if writing to the database is failing, then logging to the database is also likely to fail. Suppose for example that the connection string is wrong or the database is down or timing out. You can't log that.
I recommend using a logging framework (log4net, NLog, etc.) as a separate dependency from your Entity Framework data access layer. It's a small learning curve, but you end up with a pretty robust logging system that can much more effectively handle problems. And can be easily configured to log to multiple places, so if writing to one error log (the database) fails then you still have another one (a file, for example).
At the very least, if persisting your data context fails, you'll need to log to a new data context. Otherwise the part that failed is still there.
Something structurally more like this:
try
{
using (var database = new DbContext())
{
Member member = database.Members.Where(m=>m.ID=1).FirstOrDefault();
member.Name = "NewMemberName";
database.Entry(member).State = EntityState.Modified;
database.SaveChanges();
}
}
catch (Exception ex)
{
using (var database = new DbContext())
{
database.Logs.Add(new Log() { Value=ex.ToString() });
database.SaveChanges();
}
}

Inserting Data Into Db By Entity Framework Throwing Errors

I Trying to insert the data into Database By using entity framework but it throwing error following
An error occurred while updating the entries. See the inner exception for details.
Code is Here
public int InsertUserData(UserDetail userDetail, BusinessObjects objects)
{
try
{
UserCredential objCredentials = newPersonEntity.UserCredentials
.First(cd => cd.UserName == objects.UserName);
objCredentials.Status = objects.Status;
newPersonEntity.UserDetails.Add(userDetail);
int result=newPersonEntity.SaveChanges();
return result;
}
catch (Exception ex)
{
CatchError(ex);
return 3;
}
}
Can Any One Tell what mistake i did ?
As i undestand your code snipped correctly, you don't need to perform operations with objCredentials. Or if you want to change its status, you must call
newPersonEntity.UpdateObject(objCredentials);
And the only reason, why you cannot save changes is incorrect userDetail object fields values. Check that all required fields filled correclty and don't conflict with existing key values, stored in dataBase

Annoying SQL exception, probably due to some code done wrong

I started working on this "already started" project, and I'm having a really annoying error when trying to execute some interactions with SQL Server 2008:
The server failed to resume the
transaction. Desc.:
One of these errors I get in this specific method call:
The aspx.cs Call:
busProcesso openProcess = new busProcesso(pProcessoId);
try
{
if (openProcess.GetDocument() == null)
{
//Irrelevant code.
}
}
catch{ //... }
The Business class (relevant part):
public class busProcesso : IbusProcesso
{
public Processo vProcesso { get; set; }
RENDBDataContext db;
public busProcesso()
{
vProcesso = new Processo();
}
public busProcesso(decimal pProcessoId)
{
db = new RENDBDataContext();
try
{
vProcesso = db.Processos.SingleOrDefault(x => x.Id == pProcessoId);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
public string GetDocument()
{
try
{
string document = null;
foreach (Processo_has_Servico ps in ListaServicosProcesso())
{
if (ps.Servico.Document != null) //Get the error right at this line.
{
document = ps.Servico.Document;
}
}
return document ;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
public IQueryable<Processo_has_Servico> ListaServicosProcesso()
{
db = new RENDBDataContext();
try
{
return from ps in db.Processo_has_Servicos
join s in db.Servicos on ps.Servico_Id equals s.Id
where ps.Processo_Id == vProcesso.Id
select ps;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
As I said, the error occurs right at the line:
if (ps.Servico.Document != null) from the GetDocument() method.
Opening SQL Server Activity Monitor, I see there is a process for my database (.Net SqlClient Data Provider)
After some time/use (when I start to get the "server failed to resume the transaction" error), I go to the SQL Server Activity Monitor and there's around 5 or 6 more identical processes that weren't killed and (probably) should've been. When I manually kill them, the error stops for a while, until it starts again.
I'm not really good at working in OO and all, so I'm probably missing something, maybe some way to close one of these connections. Also, any help/tip about this structure will be welcome.
PS. The error doesn't happen everytime. Sometimes it runs just perfectly. Then it starts to give the error. Then it stops. Sometimes it happens just once.. pretty weird.
The code in ListaServicosProcesso is creating the context db. Then it is returning an IQueryable.
At this point no request has been sent to the database.
Then there is a for each in the code. At this point EF says "I need to get the data from the database". So it tries to get the data.
But the context db is now out of scope, so it crashes, on the first line that tries to use the data.
There are 2 ways to get around this:
return a list from ListaServicosProcesso, this will force the database call to execute
move the for each into ListaServicosProcesso
Edit
Pharabus is correct db is not out of scope. The problem is here:
db = new RENDBDataContext();
A new instance of the context is being created without the old one being disposed. Try Dispose of db at the end of ListaServicosProcesso. Even better place db in a using statement. But then the foreach must be moved inside the using statement.
Here's a couple of ideas to try.
1/ You can attach SQL server profiler to see the query that is being executed, which will allow you to copy and paste that query to see the data that is in the database. This might be help.
2/ You never check whether ps.Servico is null - you jump straight to ps.Servico.Document. If ps.Servico is null then you will get a null reference exception if you try to access any properties on that object.
I'm not sure of the exact cause of the error you're seeing (if you Google it, the references are all over the place...), but there are a few things you could improve in your code and I've found that just cleaning things up a bit often makes problems go away. Not always, but often.
I agree with the other answerers that it would help to keep better track of your DataContext(s). For example in you're creating it once in the constructor, then again in ListaServicosProcesso(). At that point vProcesso is on one DataContext and other entities will be on another, which gets messy.
I think you could simplify the whole thing a bit, for example you could combine GetDocument() and ListaServicosProcesso() like this:
public string GetDocument()
{
try
{
// Are you sure vProcesso is not null?
if (vProcesso == null)
return null;
// Only create the context if it wasn't already created,
if (db == null)
db = new RENDBDataContext();
return db.Processo_has_Servicos
.Where(ps => ps.Processo_Id == vProcesso.Id && ps.Servico.Document != null)
.Select(ps => ps.Servico.Document) // use an implicit join
.SingleOrDefault();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}

Categories

Resources