2013-07-30 64 views
1

更新如何解决无法正常工作的SQL语句?

在WinForm具有一个DateTimePicker 4个定期texboxes和2个掩蔽texboxes和绑定datagridview的。

我正在使用datagridview的cellformatting事件从另一个数据表中查找员工的姓名。为了实现这一点,我使用了大部分工作的sql语句。

因为它现在正在从第二个数据表中的不同列中返回雇员姓名,并将其设置在数据网格视图的第二列中,并且在雇员ID之后立即设置。

不工作的部分正在查看终止日期(“FSalida”列)并仅在没有终止日期或选定日期早于终止日期时返回雇员姓名。换句话说,如果雇员已经被终止,它不应该在终止日期之后出现。

是我的代码是这样的:

Private Sub PartePersonalDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting 
    'This looks up the employee's full name using the employee number from the ID column (first column) 
    Try 
     Dim dgvr As DataGridViewRow = DataGridView1.Rows(e.RowIndex) 
     If dgvr.Cells(0).Value IsNot Nothing AndAlso dgvr.Cells(0).Value IsNot DBNull.Value Then 
      Dim empID As Integer = CInt(dgvr.Cells(0).Value) 
      Dim qry = From dr As PersonalObraDataSet.PersonalObRow In PersonalObraDataSet.PersonalOb 
      Where (dr.cdTrabajador = empID) And (dr.FSalida = Nothing) Or (dr.FSalida <= txtDate.Text) 
      'This returns each part of the name and joins it in the the 2nd column (name column) 
      DataGridView1.Rows(e.RowIndex).Cells(1).Value = (qry.First.Nombre1 & " " & qry.First.Nombre2 & " " & qry.First.Apellido1 & " " & qry.First.Apellido2) 
      DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = DefaultBackColor 
     End If 
     'If there is an exemption like the employee doesn't exists this turns the background color red and instead 
     'of the name on the second column it shows "employee doesn't exist." 
    Catch ex As Exception 
     DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red 
     DataGridView1.Rows(e.RowIndex).Cells(1).Value = "ESTE EMPLEADO NO EXISTE" 
     Exit Sub 

不工作的部分是后“和”:

Where (dr.cdTrabajador = empID) And (dr.FSalida = Nothing) Or (dr.FSalida <= txtDate.Text) 

这是数据的上查找样本表

ProjectID EmployeeID DepartmentID HIreDate TerminationDate  Name Middle Last 
3116  83    1  09/03/2012  27/04/2012   John  Doe  Doe2 
3116  373    1  16/11/2012       Pedro John  Ortiz 
3116  1    1  01/01/2013       Jose  Maria Applessed 

你的帮助解决这个将不胜感激。

回答

0

你已经得到了上面几行的解决方案,使用DBNull.Value而不是Nothing

Where (dr.cdTrabajador = empID) And (dr.FSalida = DBNull.Value) Or (dr.FSalida <= txtDate.Text) 

或者,你可以在FSalida字段设置为类似DateTime.MinValue(我相信这是对的DateTime默认值反正)在构造函数中的PersonalObRow和核对,例如:

Class PersonalObRow 

Public FSalida As DateTime 'the VB Date keyword is synonymous to System.DateTime 

    Public Sub New() 

     FSalida = DateTime.MinValue 

    End Sub 

End Class 

或者你可以把它Nullable(Of DateTime),这样可以让你检查,如果它是Nothing,例如:

Class PersonalObRow 

    Public FSalida As Nullable(Of DateTime) 'or Nullable(Of Date) 

End Class 
+0

我得到的错误DBNull无法转换为'日期',这就是为什么我首先将它改为Nothing的原因。任何其他想法? – David

+0

是啊,检查我的编辑=] – Sean

+0

对不起,但我不知道如何做任何一个。当你说的字段DateTime.MinValue你的意思就像Dim Fsalida DateTime.MinValue?因为你可以说我对编码很陌生。 – David