2013-11-27 70 views
0

我的數據庫中有一個日期字段,當用戶單擊某個按鈕將其停用時,客戶被設置爲非活動狀態時,會設置該日期字段。我在嘗試找出如何在用戶試圖響應客戶時清除非活動日期方面遇到困難。設置NULL日期

這是解除按鈕的代碼是什麼樣子:

Private Sub btnDeactivate_Click(sender As System.Object, e As EventArgs) Handles btnDeactivate.Click 
     Dim selectedCustomers = GetSelectedCustomers() 
     Dim newInActiveFlag = Not selectedCustomers.Any(Function(c) (c.Inactive_Flag = "Y")) 
     Dim activateWord = "Activate" 
     If newInActiveFlag Then 
      activateWord = "Deactivate" 
     End If 

     Dim result As DialogResult = MessageBox.Show(String.Format("Are you sure you want to {0} this Customer?", activateWord.ToLower()), activateWord + " Customer", _ 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) 
     If result = Windows.Forms.DialogResult.Yes Then 

      Dim selectedCustomerIds = selectedCustomers.Select(Function(c) (c.Cust_Num)) 

      Dim service = New DataService() 

      service.Customers.SetInactiveFlags(selectedCustomerIds, newInActiveFlag) 
      service.SaveChanges() 
      PerformSearch(_lastSearchTxt, _lastIncludeActive) 
     End If 
    End Sub 

這裏是它設置無效標誌和日期片:

Public Sub SetInactiveFlags(custIds As IEnumerable(Of Integer), inactiveFlag As Boolean) 
     Dim customers As List(Of Customer) 
     customers = _context.Customers.Where(Function(c) (custIds.Contains(c.Cust_Num))).ToList() 

     For Each customer As Customer In customers 
      customer.Inactive_Flag = CreateFromBoolean(inactiveFlag) 
      customer.Inactive_Date = DateTime.Today 
     Next 

    End Sub 

是否有可能有相同的按鈕設置並按照我設置並刪除非活動標誌的方式刪除日期?

感謝

+0

我們無法瞭解您的班級客戶中的屬性。但是,如果只有兩個屬性(Inactive_Flag和Inactive_Date),代碼似乎很好用;如果具有更多屬性(對於Inactive和Active不同),則必須相應地更改SetInactiveFlags。你能解釋一下你現在遇到的確切問題嗎?你應該如何設置/刪除不活動的標誌?爲什麼你的代碼現在不能按預期工作? – varocarbas

+0

我遇到的問題是當我去激活一個客戶時,它不會清除非活動日期,而是插入重新激活的日期。要設置/刪除非活動標誌,我使用單個按鈕,根據Inactive_Flag字段中是否存在「Y」或「N」字樣,將文本更改爲「取消激活」,然後相應地更改值。 –

+0

你只是想輸入一個不同的日期,當它停用?這很簡單,只需遵循Tom的建議(設置一個可爲空的Date類型並在停用時通過Nothing,通過Tom建議的SetInactiveFlags或參數中的條件);或者通過SetInactiveFlags中的條件/參數再次創建將用於停用情況的「極端日期」(例如1-1-1900)。 – varocarbas

回答

0

假設你正在讓你的Inactive_Datenull值,你可以簡單地將它設置爲Nothing。如果更改SetInactiveFlags方法有一個可爲空(中DATETIME)參數(?或日期時間ALA C#快捷鍵),你可以直接通過它跨越:

Public Sub SetInactiveFlags(custIds As IEnumerable(Of Integer), inactiveDate As DateTime?, inactiveFlag As Boolean) 
... 
customer.Inactive_Date = inactiveDate 
... 

那麼您可以使用SetInactiveFlags(selectedCustomerIds, Nothing, newInActiveFlag)SetInactiveFlags(selectedCustomerIds, DateTime.Now, newInActiveFlag)

實體框架/ Linq2Sql將自動將Nothing轉換爲DBNull.Value。

+0

感謝您的幫助。我找到了一個解決方案,但必須等待8個小時才能發佈,因爲我的聲望低於10.我會盡快將其發佈。 –

0

好吧,我想我找到了解決方案,我把湯姆的意見,並已將此添加到BTN單擊事件:

If activateWord = "Deactivate" Then 
       service.Customers.SetInactiveFlags(selectedCustomerIds, DateTime.Today, newInActiveFlag) 
      ElseIf activateWord = "Activate" Then 
       service.Customers.SetInactiveFlags(selectedCustomerIds, Nothing, newInActiveFlag) 
      End If 

等等全片看起來是這樣的:

Private Sub btnDeactivate_Click(sender As System.Object, e As EventArgs) Handles btnDeactivate.Click 
     Dim selectedCustomers = GetSelectedCustomers() 
     Dim newInActiveFlag = Not selectedCustomers.Any(Function(c) (c.Inactive_Flag = "Y")) 
     Dim activateWord = "Activate" 
     If newInActiveFlag Then 
      activateWord = "Deactivate" 
     End If 

     Dim result As DialogResult = MessageBox.Show(String.Format("Are you sure you want to {0} this Customer?", activateWord.ToLower()), activateWord + " Customer", _ 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) 
     If result = Windows.Forms.DialogResult.Yes Then 

      Dim selectedCustomerIds = selectedCustomers.Select(Function(c) (c.Cust_Num)) 

      Dim service = New DataService() 
      If activateWord = "Deactivate" Then 
       service.Customers.SetInactiveFlags(selectedCustomerIds, DateTime.Today, newInActiveFlag) 
      ElseIf activateWord = "Activate" Then 
       service.Customers.SetInactiveFlags(selectedCustomerIds, Nothing, newInActiveFlag) 
      End If 
      service.SaveChanges() 
      PerformSearch(_lastSearchTxt, _lastIncludeActive) 
     End If 
    End Sub 

我也改了SetInactiveFlags看起來像這樣:

Public Sub SetInactiveFlags(custIds As IEnumerable(Of Integer), Inactive_Date As DateTime?, inactiveFlag As Boolean) 
     Dim customers As List(Of Customer) 
     customers = _context.Customers.Where(Function(c) (custIds.Contains(c.Cust_Num))).ToList() 

     For Each customer As Customer In customers 
      customer.Inactive_Flag = CreateFromBoolean(inactiveFlag) 
      customer.Inactive_Date = Inactive_Date 
     Next 

    End Sub 

我花了一分鐘來了解邏輯,但感謝羚牛克時間來幫助我,我真的很感激它。