[英]Windows Forms program suddenly closes when running a query. How do I catch the error before it stops/crashes?
I have a 3 PC setup that runs the POS-like program I created.我有 3 台 PC 设置,可以运行我创建的类似 POS 的程序。 1 PC that acts as the server and the 2 as the client. 1 台 PC 作为服务器,2 台作为客户端。 I have a heavy query in the client-side that when executed, sometimes closes the program.我在客户端有一个繁重的查询,执行时有时会关闭程序。 How do I catch the problem before it force closes?如何在强制关闭之前发现问题?
The query checks the remaining stocks by getting the sum of all the inputted stocks and subtracting it from the products sold, transferred to another branch and those that got damaged.该查询通过获取所有输入库存的总和并从已售出、转移到另一个分公司和损坏的产品中减去该总和来检查剩余库存。 So every time they search a product, it goes through 4 tables (Inventory, Sales, Transferred, Damaged) to get the stocks and 4 more tables to get the product's description (Product, Category, Subcategory, Supplier).因此,每次他们搜索产品时,都会通过 4 个表(库存、销售、转移、损坏)来获取库存和另外 4 个表来获取产品的描述(产品、类别、子类别、供应商)。
So going back, how do I log the error before the application closes?那么回过头来,如何在应用程序关闭之前记录错误? I'm thinking of getting the form closed event but how do I log it only if it crashes?我正在考虑获取表单关闭事件,但如何仅在崩溃时记录它?
EDIT 1: I always put try catch with message box to all my methods.编辑 1:我总是把 try catch 和消息框放在我所有的方法中。 The application doesn't pop up any messages before it closes.应用程序在关闭之前不会弹出任何消息。
SELECT p.Id,p.Product_Name Product,p.Description,
c.Category_Name Category,sc.Subcategory_Name Subcategory,s.Supplier_Name Supplier,p.Selling_Price `Unit Price`,
i.Stocks,s.Sales,i.Stocks - IFNULL(s.Sales, 0) - IFNULL(t.Transfer, 0) - IFNULL(d.Damage, 0) AS Remaining
FROM (SELECT Id, Product_Name, Description, Selling_Price, Category_Id, Subcategory_Id, Supplier_Id FROM product WHERE enable_flag = 1) p
LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(quantity), 0) AS Stocks FROM inventory WHERE enable_flag = 1 GROUP BY product_id) i
ON p.Id = i.product_id
LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(quantity), 0) AS Sales FROM sales_detail WHERE enable_flag = 1 GROUP BY product_id) s
USING(product_id)
LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(transfer_quantity), 0) AS Transfer FROM stock_transfer WHERE enable_flag = 1 GROUP BY product_id) t
USING(product_id)
LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(damaged_quantity), 0) AS Damage FROM damaged_product WHERE enable_flag = 1 GROUP BY product_id) d
USING(product_id)
JOIN Category c ON p.Category_Id=c.Id
JOIN Subcategory sc ON p.Subcategory_Id=sc.Id
JOIN Supplier s ON p.Supplier_Id=s.Id;
EDIT 2:编辑2:
Here's my button code ( Variables.dgvSearchItemsDataSource
is equivalent to the posted query):这是我的按钮代码( Variables.dgvSearchItemsDataSource
相当于发布的查询):
private void btnSearch_Click(object sender, EventArgs e)
{
dgvSearchItems.DataSource = dbConnect.DatabaseToDatagrid(Variables.dgvSearchItemsDataSource + " WHERE p.Product_Name LIKE '" + cmbSrchProd.Text + "%'");
if (dgvSearchItems.Rows.Count != 0)
{
this.dgvSearchItems.Columns[1].Frozen = true;
this.dgvSearchItems.Columns[2].Frozen = true;
this.dgvSearchItems.Columns[0].Visible = false;
this.dgvSearchItems.Columns[7].Visible = false;
this.dgvSearchItems.Columns[8].Visible = false;
}
txtQuantity.Focus();
}
The method I use to connect to my database:我用来连接到我的数据库的方法:
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
myNotification = new frmNotifOk();
myNotification.Show("Cannot connect to server.");
break;
case 1045:
myNotification = new frmNotifOk();
myNotification.Show("Invalid username/password, please try again.");
break;
}
return false;
}
}
It seems that although you think you put try/catch blocks around every call, somewhere an unhandled exception is thrown.似乎虽然您认为您在每次调用周围都放置了 try/catch 块,但在某处抛出了未处理的异常。
I think https: System.AppDomain.UnhandledException might help.我认为 https: System.AppDomain.UnhandledException可能会有所帮助。
This event provides notification of uncaught exceptions.此事件提供未捕获异常的通知。 It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.它允许应用程序在系统默认处理程序向用户报告异常并终止应用程序之前记录有关异常的信息。 If sufficient information about the state of the application is available, other actions may be undertaken — such as saving program data for later recovery如果有关应用程序状态的足够信息可用,则可能会采取其他操作 - 例如保存程序数据以供以后恢复
Handling this event will give you the opportunity to log the problem before the program closes.处理此事件将使您有机会在程序关闭之前记录问题。
usage:用法:
static class Program
{
private static MyLogger logger = new MyLogger();
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
...
}
static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exc = e.ExceptionObject as Exception;
if (exc != null)
{ // log the exception
logger.LogException(exc);
}
// Problem is logged, Can't continue, so quit the application:
Environment.Exit(-1);
}
Add a try catch block around the code you are using to execute the query在用于执行查询的代码周围添加 try catch 块
try
{
// Execute query
}
catch (Exception)
{
// Exception handling
throw;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.