我有一個表格可以在某些條件下在 sql 資料庫中插入一些資料,首先我需要檢查空值并提醒用戶:
void CheckNulls() {
try {
if (txtcode.Text.Length == 0 || txtItem.Text.Length == 0 || txtWh.Text.Length == 0) {
MessageBox.Show("Fill Required Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
checkExistAndDo();
}
}
catch(Exception Err) {
MessageBox.Show("This Error Occured :" Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
然后我需要檢查用戶是否檢查過checkbox
:
void checkExistAndDo() {
if (chkProduct.Checked || chkMaterial.Checked)
{
if (chkProduct.Checked == true) {
if (!chkMaterial.Checked) {
da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0) {
MessageBox.Show("Existing Code", "Error");
}
else {
TakeAction();
}
}
}
else {
da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0) {
MessageBox.Show("Existing Code", "Error");
}
else {
TakeAction();
}
}
}
else {
MessageBox.Show("Check even one", "check", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
所以,我總共有 4 種不同的組合:
chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
true : true : InsertSubProduct()
true : false : InsertProduct()
false : true : InsertMaterial()
false : false : Ask User to Check
然后我需要根據組合采取行動:
private void TakeAction()
{
try
{
if (chkProduct.Checked == true && chkMaterial.Checked == false)
{
InsertProduct();
MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (chkProduct.Checked == false && chkMaterial.Checked == true)
{
InsertMaterial();
MessageBox.Show("Material Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
InsertSubProduct();
MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show("This Error Occured :" Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.Close();
}
}
問題是這部分沒有做任何不顯示我的訊息的事情,甚至沒有錯誤訊息,就好像它在我的代碼中不存在一樣:
else
{
InsertSubProduct();
MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
很抱歉發了很長的帖子,但我盡了最大的努力并多次除錯了代碼,但我無法通過這個,在此先感謝您的幫助,我們深表感謝。
uj5u.com熱心網友回復:
如果該ThreeState
屬性設定為 true,則 Checked 屬性將為true
aChecked
或Indeterminate
CheckState 回傳。
檢查復選框的ThreeState
屬性chkMaterial
uj5u.com熱心網友回復:
根據@Joel Coehoorn 的評論,我嘗試了不同的方法,我參考了“更好地分離關注點。CheckNulls() 應該只回傳一個布林值,而不應該向用戶顯示任何訊息或呼叫任何其他方法。checkExistAndDo() 根本不應該存在(該邏輯應完全在資料庫中,作為 InsertProduct/MaterialSubproduct() 的一部分)。”
效果很好,謝謝大家的回復,謝謝你的指導。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/469415.html
上一篇:IF陳述句無效語法訊息