I’m working on an application that opens a database connection and retrieving data from it to be showed to the user. Everything looks fine in the code of what I can see, but something is wrong because an exception gets thrown in the method below. I have placed a breakpoint at “conn.Open();” and it’s there the exception is thrown.
I have no idea how to find out what the actual error is and how to solve it, and hope to get some help here. The stack trace for the exception can be found here if it helps.
The exception is: System.ApplicationException: Error in the application
Thanks in advance!
the method that throws the exception:
public List<Movie> GetMovies() {
var movieTitles = new List<Movie>(100);
using (var conn = CreateConnection()) {
try {
var cmd = new SqlCommand("dbo.usp_GetMovies", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (var reader = cmd.ExecuteReader()) {
var movieIDIndex = reader.GetOrdinal("MovieID");
var nameIndex = reader.GetOrdinal("Name");
var yearIndex = reader.GetOrdinal("Year");
var lengthIndex = reader.GetOrdinal("Length");
var summaryIndex = reader.GetOrdinal("Summary");
while (reader.Read()) {
movieTitles.Add(new Movie {
MovieID = reader.GetInt32(movieIDIndex),
Name = reader.GetString(nameIndex),
Year = reader.GetInt32(yearIndex),
Length = reader.GetInt32(lengthIndex),
Summary = reader.GetString(summaryIndex),
});
}
}
}
catch {
throw new ApplicationException("An error occured!");
}
}
movieTitles.TrimExcess();
return movieTitles;
}
Heres the base class for the class containing the method above:
public abstract class DALBase {
private static string _connectionString;
static DALBase() {
_connectionString = WebConfigurationManager.ConnectionStrings["NameOfTheDatabase_ConnectionString"].ConnectionString;
}
protected SqlConnection CreateConnection() {
return new SqlConnection(_connectionString);
}
}
from web.config:
<connectionStrings>
<add name="NameOfTheDatabase_ConnectionString" connectionString="Data Source=xxx.xx.xxx.x;Initial Catalog=The_Catalog;User ID=xxxxxx;Password=xxxxxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
Change
to
and you will get the actual exception and message.