Winform reopening if an Exception is thrown
I have a WinForm which is calling a method, using .Net Remoting, on the server.
The method is querying the database. Pretty straight forward so far. The problem is that if I close the form while this method is being executed an exception is thrown Complaining that it is expecting 开发者_如何学运维an SqlParameter
, which reopens the form. I don't want the form to be reopened.
I don't want to simply suppress any SqlException
which is thrown, because I want to be notified if an exception is thrown.
Is there a simple way around this ?
StringBuilder sb = new StringBuilder();
sb.Append("SELECT COUNT(*) FROM ")
.Append(table)
.Append(" WHERE ")
.Append(fieldName)
.Append(" = @Value AND SoftDelete = 0");
SqlCommand cmd = this.m_SqlDBConnPool.GetSqlCommand(sb.ToString());
int count = -1;
lock (cmd)
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("Value", value));
this.m_SqlDBConnPool.ExecQuery(cmd, new SqlDBConnPool.DataReader<object>(
delegate(SqlDataReader reader, object o)
{
count = reader.GetInt32(0);
return true;
}), null);
}
Please note that SqlDBConnPool
is a class we wrote.
Try to put the following around your code...
try
{
//Your code
}
catch(Exception ex)
{
//Close the form by your self
//Send back the error
}
精彩评论