我想更新供應商信息,並且想要刪除文本框txtWeb的內容。如何跳過一個條件?
但是,當我調試它,它總是進入我的正則表達式的條件來驗證txtWeb文本框的內容,即使文本框的內容是空的。當我在文本框中輸入正確的URL時,它工作正常,但不起作用,因爲沒有內容。
但是數據庫表允許在txtWeb列中爲null。
問題在於最後的「如果」條件。
public void Modifier_Supplier()
{
SamsonEntities db = new SamsonEntities();
try
{
action = "MODIF";
if (gridSuppliers.CurrentRow != null && gridSuppliers.CurrentRow.DataBoundItem != null)
{
SupplierDisplay supAct = (SupplierDisplay)gridSuppliers.CurrentRow.DataBoundItem;
Supplier supUpd = db.Suppliers.Single(sup => sup.SupplierID == supAct.SupplierID);
supUpd.SupplierName = txtNom.Text;
supUpd.Address = txtAdresse.Text;
supUpd.City = txtVille.Text;
supUpd.PostalCode = mskPostal.Text;
supUpd.Contact = txtContact.Text;
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(txtCourriel.Text);
if (!match.Success)
{
MetroMessageBox.Show(this, "Ce courriel est invalide", "Message d'erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
supUpd.Email = null;
return;
}
else
supUpd.Email = txtCourriel.Text;
supUpd.Phone = mskPhone.Text;
Regex regexWeb = new Regex(@"^(https?://)?([\da-z.-]+)\.([a-z\.]{2,6})([/\w .-]*)/?$");
Match matchWeb = regexWeb.Match(txtWeb.Text);
if (!matchWeb.Success)
{
MetroMessageBox.Show(this, "Ce site web est invalide", "Message d'erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
supUpd.Website = null;
return;
}
else
supUpd.Website = txtWeb.Text;
db.SaveChanges();
FillSuppliers();
MetroMessageBox.Show(this, "Fournisseur mis à jour", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
我不確定我的理解。如果你說這個字段可以是空的,爲什麼不添加一個或一個條件來允許「」? – dman2306
什麼?我不明白你的意見? –
我的評論正在說明某人剛剛發佈的答案。檢查一個空字符串,並允許它 – dman2306