Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I throw an exception to the GUI layer from my Data Access layer if any primary key violation error comes without saying primary key violated to the GUI.
Posted

1 solution

You need to throw an exception from your datalayer for example :
C#
public void ConnectDatabase()
{
  try
  {
    SqlConnection myConnection = new SqlConnection("connectionstring");
    myConnection.Open();
  }
  catch (SqlException sqlEx)
  {
    // Include inner exception to give more information about what went wrong
    throw new MyCustomException("Failed to open new SQL Connection", sqlEx);
  }
}


In your Gui layer, you'll connect to the database somewhere, at that point, you need to catch your custom made exception :
C#
// Application starts
try
{
  DataLayerObject myObject = new DataLayerObject();
  myObject.ConnectDatabase();
}
catch (MyCustomException customEx)
{
  MessageBox.Show(customEx.Message);
}


Have fun!

Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900