我返回了一個RunWorkerCompletedEventArgs
對象,並試圖獲取我可以在對象中看到的錯誤號。當我使用if (err.Number!= 2627){}
我收到以下錯誤獲取RunWorkerCompletedEventArgs對象的內容
「對象」不包含「數」和沒有擴展 方法「數字」接受型「對象」的第一個參數可能是 發現了一個定義(是你錯過了使用指令或裝配參考?)
請有人建議如何獲取對象內容(請參閱下面的立即窗口)。
也請有人可以解釋這裏實際發生了什麼,因爲錯誤表明該對象不包含'數字'的定義,但它看起來像它確實存在?
CODE:
private void GBHworker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
//update UI once worker complete his work
if (e.Result != null)
{
var err = e.Result;
if (err.Number!= 2627) //don't want to raise error on duplicate primary key
{
MessageBoxButton btn = MessageBoxButton.OK;
var result = ModernDialog.ShowMessage(e.Result.ToString(),
"Error", btn);
}
}
}
ASSIGNING E:
private void GBHworker_DoWork(object sender, DoWorkEventArgs e)
{
//....DB parameter setup
try
{
cmd_insert.ExecuteNonQuery();
}
catch (Exception crap)
{
if (e != null)
e.Result = crap;
}
}
立即窗口:
?err
{"Violation of PRIMARY KEY constraint 'PK_XYZ'. Cannot insert duplicate key in object 'dbo.XYZ. The duplicate key value is (30527293868).\r\nThe statement has been terminated."}
base {System.Data.Common.DbException}: {"Violation of PRIMARY KEY constraint 'PK_XYZ'. Cannot insert duplicate key in object 'dbo.XYZ'. The duplicate key value is (30527293868).\r\nThe statement has been terminated."}
Class: 14
ClientConnectionId: {*******}
Errors: {System.Data.SqlClient.SqlErrorCollection}
LineNumber: 1
Number: 2627
Procedure: ""
Server: "*******"
Source: ".Net SqlClient Data Provider"
State: 1
結果被定義爲對象,所以這就是爲什麼你看不到它的數字屬性。你需要先施放它。 – Surfbutler
您需要將其轉換爲您正在分配電子郵件的類型。您需要在您分配e的地方發佈代碼。 – Paparazzi