I want to create my own exception in windows form application.I am trying to add the add some data into the database.
code:
try
{
string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");
sqlCommand = new SqlCommand(insertData, databaseConnectivity.connection);
sqlCommand.Parameters.AddWithValue("@starttime", broadcastStartDateTime);
sqlCommand.Parameters.AddWithValue("@endtime", broadcastEndDateTime);
sqlCommand.Parameters.AddWithValue("@lastuptime", currentDateTime);
sqlCommand.Parameters.AddWithValue("@applicationname", txtApplicationName.Text);
sqlCommand.ExecuteNonQuery();
}
catch (DataBaseException ex)
{
MessageBox.Show(ex.Message);
}
here I have created a my own exception.Here I have given scalar variable @lastuptime instead of @lastupdatetime for capturing the sqlexception.
here is my databaseException class.
class DataBaseException : Exception
{
public DataBaseException(string Message)
: base(Message)
{
}
public DataBaseException(string message, Exception innerException)
: base(message, innerException)
{
}
}
Here while running the program it show the error at
sqlCommand.ExecuteQuery();
but it does not capture the error and show the message box text.I know i have done some thing wrong.I do know whether which i created custom exception handling is right or wrong.can any one help me.Thanks in advance.
rampt patter
17-Feb-2015sample custom exception..
Rampt
Vijay Shukla
13-Jun-2013Hello ezra heywood!
You need to throw your custom exception so that it can be caught by the calling method. In your code, program will throw exception from the DB and not your custom exception.