2015-07-19 69 views
1

我想更新供應商信息,並且想要刪除文本框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); 
       } 
      } 
+0

我不確定我的理解。如果你說這個字段可以是空的,爲什麼不添加一個或一個條件來允許「」? – dman2306

+0

什麼?我不明白你的意見? –

+1

我的評論正在說明某人剛剛發佈的答案。檢查一個空字符串,並允許它 – dman2306

回答

1

因爲我明白你的問題,你不想在txtWeb文本框中沒有文本時進行正則表達式驗證。要做到這一點,只需添加一個if條件來檢查txtWeb是否爲空。

if(txtWeb.Text != String.Empty) 
    Match matchWeb = regexWeb.Match(txtWeb.Text); 
+0

我所要做的就是用你的建議取代我的最後一個條件? –

+1

是的...只需更換 – HaveNoDisplayName

+0

它的工作!感謝您的幫助 –