System.ApplicationError in Data access class

  • Thread starter Thread starter Alex. O. Koranteng
  • Start date Start date
A

Alex. O. Koranteng

I have my data access class in the App_Code folder. I have added the code
for this class below. I am using objectdatasource to bind to the gridview.
The objectdatasource makes a call to the GetEmployees method when the page
loads and uses a storedproc to populate the gridview. I am getting a Use
implicitly typed local variable declaration type error; Find error message
below:

Data error.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.ApplicationException: Data error.

Source Error:


Line 228: catch (SqlException err)
Line 229: {
Line 230: throw new ApplicationException("Data error.");
Line 231: }
Line 232: finally


Source File:
c:\Infragistics\Incidents\2009\Responses\Modified_webgridusingobjectdatasourcecs05162009\App_Code\EmployeeDB.cs Line: 230







using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
using System.Collections;


public class EmployeeDetails
{
private int employeeID;
//private string HireDate;
//private DateTime HireDate;

public int EmployeeID
{
get { return employeeID; }
set { employeeID = value; }
}

public string FirstName { get; set; }

public string LastName { get; set; }

public DateTime HireDate
{
get { return HireDate; }
set { HireDate = value; }
}

public EmployeeDetails(int employeeID, string firstName, string lastName,
DateTime HireDate)
{
//this.employeeID = employeeID;
this.EmployeeID = employeeID;
this.FirstName = firstName;
this.LastName = lastName;
this.HireDate = HireDate;
}

public EmployeeDetails() { }
}
public class EmployeeDB
{
private string connectionString;

public EmployeeDB()
{
connectionString =
WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
}
public EmployeeDB(string connectionString)
{
this.connectionString = connectionString;
}

public int InsertEmployee(EmployeeDetails emp)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("InsertEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@FirstName",
SqlDbType.NVarChar, 10));
cmd.Parameters["@FirstName"].Value = emp.FirstName;
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar,
20));
cmd.Parameters["@LastName"].Value = emp.LastName;
cmd.Parameters.Add(new SqlParameter("@HireDate", SqlDbType.DateTime,
8));
cmd.Parameters["@HireDate"].Value = emp.HireDate;
cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Direction = ParameterDirection.Output;

try
{
con.Open();
cmd.ExecuteNonQuery();
return (int)cmd.Parameters["@EmployeeID"].Value;
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}


public void UpdateEmployee(EmployeeDetails emp)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("UpdateEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@FirstName",
SqlDbType.NVarChar, 10));
cmd.Parameters["@FirstName"].Value = emp.FirstName;
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar,
20));
cmd.Parameters["@LastName"].Value = emp.LastName;
cmd.Parameters.Add(new SqlParameter("@HireDate", SqlDbType.DateTime,
8));
cmd.Parameters["@HireDate"].Value = emp.HireDate;
cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Value = emp.EmployeeID;

try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}

public void UpdateEmployee(int EmployeeID, string firstName, string
lastName, string titleOfCourtesy)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("UpdateEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@FirstName",
SqlDbType.NVarChar, 10));
cmd.Parameters["@FirstName"].Value = firstName;
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar,
20));
cmd.Parameters["@LastName"].Value = lastName;
cmd.Parameters.Add(new SqlParameter("@TitleOfCourtesy",
SqlDbType.NVarChar, 25));
cmd.Parameters["@TitleOfCourtesy"].Value = titleOfCourtesy;
cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Value = EmployeeID;

try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}

public void DeleteEmployee(int employeeID)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("DeleteEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Value = employeeID;

try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}

public EmployeeDetails GetEmployee(int employeeID)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("GetEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Value = employeeID;

try
{
con.Open();
SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.SingleRow);

reader.Read();
EmployeeDetails emp = new EmployeeDetails(
(int)reader["EmployeeID"], (string)reader["FirstName"],
(string)reader["LastName"], (DateTime)reader["Hiredate"]);
reader.Close();
return emp;
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}

public EmployeeDetails[] GetEmployees()
{
SqlConnection con = new SqlConnection(connectionString);
//SqlCommand cmd = new SqlCommand("GetAllEmployees", con);
SqlCommand cmd = new SqlCommand("[dbo].[GetEmployees]",con);
cmd.CommandType = CommandType.StoredProcedure;

ArrayList employees = new ArrayList();

try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
EmployeeDetails emp = new EmployeeDetails(
(int)reader["EmployeeID"], (string)reader["FirstName"],
(string)reader["LastName"], (DateTime)reader["HireDate"]);
employees.Add(emp);
}
reader.Close();

return
(EmployeeDetails[])employees.ToArray(typeof(EmployeeDetails));
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}

public int CountEmployees()
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("CountEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;

try
{
con.Open();
return (int)cmd.ExecuteScalar();
}
catch (SqlException err)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}
}
 
Hi Alex,
Exception Details: System.ApplicationException: Data error.
Source Error:

Line 228: catch (SqlException err)
Line 229: {
Line 230: throw new ApplicationException("Data error.");
Line 231: }
Line 232: finally

From your description there's an SqlException thrown and caught by your
code. To see the detailed information of this exception please set a
breakpoint at Line 229 and start debugging. When the exception is thrown
check all the properties of err in the locals window. It can tell you
what's wrong.

How to set breakpoint:
http://msdn.microsoft.com/en-us/library/k80ex6de.aspx

How to use locals window:
http://msdn.microsoft.com/en-us/library/a6td98xe(VS.71).aspx

Please have a try and provide the detailed information for further
troubleshooting.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Alex,
Exception Details: System.ApplicationException: Data error.
Source Error:

Line 228: catch (SqlException err)
Line 229: {
Line 230: throw new ApplicationException("Data error.");
Line 231: }
Line 232: finally

From your description there's an SqlException thrown and caught by your
code. To see the detailed information of this exception please set a
breakpoint at Line 229 and start debugging. When the exception is thrown
check all the properties of err in the locals window. It can tell you
what's wrong.

How to set breakpoint:
http://msdn.microsoft.com/en-us/library/k80ex6de.aspx

How to use locals window:
http://msdn.microsoft.com/en-us/library/a6td98xe(VS.71).aspx

Please have a try and provide the detailed information for further
troubleshooting.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Allen,

Thanks for the suggestions and links. I will follow through and update you
by Wednesday
 
Allen,

Thanks for the suggestions and links. I will follow through and update you
by Wednesday
 
Hi Alex,
Thanks for the suggestions and links. I will follow through and update you
by Wednesday

Do you have any progress on this issue?

Regards,
Allen Chen
Microsoft Online Support
 
Hi Alex,
Thanks for the suggestions and links. I will follow through and update you
by Wednesday

Do you have any progress on this issue?

Regards,
Allen Chen
Microsoft Online Support
 
Back
Top