[英]Search stuck on datetime parameters
我正在Visual Studio 2012中構建一個搜索頁面,而不是為每個參數使用單獨的工具條,而是使用與用戶輸入一樣多的參數。 使用兩個datetime參數並將其設置為null時會出現問題。 我從如何將Null日期變量傳遞到SQL Server數據庫以及處理DateTime和Null值中獲得了很好的指導,所以我認為以某種方式使用Dbnull.value是可行的方法,但是,我不知道代碼在哪里在我的頁面上。 這是按鈕子和相應的查詢代碼:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Me.Claims_IncidentsTableAdapter.FillByAllOptions(Me.IncidentSearchDataSet.Claims_Incidents, LastNameTextBox.Text, ClaimNumberTextBox.Text, InsuredNameTextBox.Text, FirstNameTextBox.Text,
New System.Nullable(Of Date)(CType(DOLTextBox.Text, Date)), New System.Nullable(Of Date)(CType(DRTextBox.Text, Date)), AssertedCheckBox.CheckState, CWSCheckBox.CheckState)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
詢問
SELECT Claim_Number, Insured_Name, Policy_Number, lastname, firstname, dateofloss, datereceived, asserted, movedtocws
FROM dbo.Claims_Incidents
WHERE (@lastname IS NULL OR
lastname LIKE '%' + @lastname + '%') AND (@Claim_Number IS NULL OR
Claim_Number LIKE '%' + @Claim_Number + '%') AND (@Insured_Name IS NULL OR
Insured_Name LIKE @Insured_Name + '%') AND (@firstname IS NULL OR
firstname LIKE '%' + @firstname + '%') AND (@dateofloss IS NULL OR
dateofloss = @dateofloss) AND (@datereceived IS NULL OR
datereceived = @datereceived) AND (@asserted IS NULL OR
asserted = 1) AND (@movedtocws IS NULL OR
movedtocws = 1)
當前,我收到錯誤消息“從字符串“”轉換為“日期”類型無效”。 如果我將日期字段留空。 我知道了,但是我將代碼放在哪里? 如果要輸入一個函數,該在哪里放置函數代碼? 我不知道合適的位置。
原因
這是您的問題:
New System.Nullable(Of Date)(CType(TEXTBOX.Text, Date))
如果TEXTBOX
為空( ""
),則不能將其轉換為日期。 實際上, System.Nullable(Of Date)
永遠不會為null,因為空的TEXTBOX
的值為String.Empty
,而不是Nothing
。
解
用。。。來代替:
If(String.IsNullOrEmpty(TEXTBOX.Text), New Nullable(Of Date), New Nullable(Of Date)(Date.Parse(TEXTBOX.Text)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.