对于每个循环使用嵌套如果未正常运行。 VB.net

时间:2015-10-06 20:36:37

标签: vb.net foreach

我有一个sub,按钮点击后应该查询我的SQL数据库拉出与ant匹配的所有结果,然后清除那些不符合用户定义值的结果。

dt1和dt2是开始日期和结束日期的datePickers。日期作为字符串存储在数据库中,并在检索时进行解析。我知道因为索引号而对于每个循环和DELETE都有一些奇怪的东西,但我所有的工作都尝试了IF THEN语句不运行。

 Private Sub getdata_Click(sender As Object, e As RoutedEventArgs) Handles getdata.Click

    Dim dt As New DataTable
    dt.Dispose()
    SQL.AddParam("@name", emplistadm.Text.ToString)

    SQL.ExecQuery("SELECT EmployeeName, PunchIn, PunchOut, HoursWorked FROM PUNCHES WHERE EmployeeName = @name And CurrentStatus = 0 AND Processed = 0")

    Dim dt1 As DateTime = startdate.DisplayDate
    Dim dt2 As DateTime = enddate.DisplayDate

    Dim dt1int As Int16 = DatePart(DateInterval.DayOfYear, dt1)
    Dim dt2int As Int16 = DatePart(DateInterval.DayOfYear, dt2)

    For Each dr As DataRow In dt.Rows
        Dim dt3 As DateTime = DateTime.Parse(dr("PunchIn"))
        Dim dt3int As Int16 = DatePart(DateInterval.DayOfYear, dt3)
        If dt3int > dt2int AndAlso dt3int < dt1int Then
            dr.Delete()
        End If

    Next



    dt = SQL.SQLDS.Tables(0)
    dataGrid.ItemsSource = dt.DefaultView
End Sub

我知道我还需要调用accept更改但是当我用msgbox替换dr.delete(“dakjhfdsalkjf”)时它仍然没有显示消息框。

1 个答案:

答案 0 :(得分:0)

试试这个(我改名为dt1Int和dt2Int)

Private Sub getdata_Click(sender As Object, e As RoutedEventArgs) Handles getdata.Click

Dim dt As New DataTable
dt.Dispose()
SQL.AddParam("@name", emplistadm.Text.ToString)

SQL.ExecQuery("SELECT EmployeeName, PunchIn, PunchOut, HoursWorked FROM PUNCHES WHERE EmployeeName = @name And CurrentStatus = 0 AND Processed = 0")

Dim dt1 As DateTime = startdate.DisplayDate
Dim dt2 As DateTime = enddate.DisplayDate

Dim dt1 As Integer = DatePart(DateInterval.DayOfYear, dt1) 'startdateInt 
Dim dt2 As Integer = DatePart(DateInterval.DayOfYear, dt2) 'enddateInt 

dt = SQL.SQLDS.Tables(0)

For Each dr As DataRow In dt.Rows
    Dim dt3 As DateTime = DateTime.Parse(dr("PunchIn"))
    Dim dt3int As Integer = DatePart(DateInterval.DayOfYear, dt3)
    ' was:         If dt3int > dt2int AndAlso dt3int < dt1int Then
    ' should be 
    If dt3int > **dt1int** AndAlso dt3int < **dt2int** Then 
        dr.Delete()
    End If

Next

dataGrid.ItemsSource = dt.DefaultView
End Sub
而不是
如果dt3int&gt; dt2int AndAlso dt3int&lt; dt1int那么

使用

如果dt3int&gt; dt1int AndAlso dt3int&lt; dt2int 然后

您正在与错误的变量进行比较。