我正在使用vb.net从sql server数据库加载数据。一个表有datatime列。我使用bindingsource将控件绑定到列。这就是我将datetimepicker绑定到数据列的方式:
returnDateDateTimePicker.DataBindings.Add(New Binding("value", bsRegister, "returnDate"))
此日期时间选择器中显示了复选框。
当我打开表单时,如果列的日期时间值不为null或为空,则数据将正确显示并选中复选框。
现在,当我想将返回日期设置为null以保存到数据库时,它不会将其保存为null!
我尝试使用它如下:
Private Sub ReturnDateDateTimePicker_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ReturnDateDateTimePicker.MouseUp
Try
If ReturnDateDateTimePicker.Checked = True Then
ReturnDateDateTimePicker.CustomFormat = "dd-MMM-yyyy hh:mm tt"
Else
ReturnDateDateTimePicker.CustomFormat = " -- 'Select Date' -- "
End If
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
当我取消选中datetimepicker时,文本将更改为' - 选择日期 - '。
我认为这会解决问题,但returnDate总是有价值! 我的代码有什么问题,解决方案是什么?
答案 0 :(得分:0)
我刚刚解决了同样的问题,那就是如何使在职员工的离职日期具有空值(因为他们仍在公司工作)。
我用VS 2017编写了一个小程序,它是VB.Net语言,老板要求我准备雇员列表程序,并在数据库中添加其加入日期和辞职日期。
当我输入有效的员工数据时,这对我来说是个问题,因为我无法填写辞职日期(他们仍在工作)并且VB不允许datetimepicker具有空值。
请参考我的代码以查看解决方案;我只添加一个对象以捕获空值,然后将其作为指定日期发送到数据库。
Private Sub btnsave_Click(sender As Object, e As EventArgs) _
Handles btnsave.Click
Dim exresigndate As String = ""
If rbactive.Checked = True Then
status = rbactive.Text
exresigndate = "Null"
ElseIf rbresigned.Checked = True Then
status = rbresigned.Text
exresigndate = "@ResignDate"
End If
Try
Call SearchEmpID()
If Not DR.HasRows Then
Call connection()
Dim CMD As SqlCommand
Dim SQL As String
SQL = "INSERT INTO Employee VALUES (@EmpID, @EmpName, @DeptName, @EmpStatus, @JoinDate, " & exresigndate & ", @Address, @Photo)"
CMD = New SqlCommand(SQL, CONN)
CMD.Parameters.AddWithValue("@EmpID", txtid.Text)
CMD.Parameters.AddWithValue("@EmpName", txtname.Text)
CMD.Parameters.AddWithValue("@DeptName", cmbdept.Text)
CMD.Parameters.AddWithValue("@EmpStatus", status)
CMD.Parameters.AddWithValue("@JoinDate", Format(dtpjoin.Value, "MM-dd-yyyy"))
If rbresigned.Checked = True Then
CMD.Parameters.AddWithValue("@ResignDate", Format(dtpresigned.Value, "MM-dd-yyyy"))
End If
CMD.Parameters.AddWithValue("@Address", txtaddress.Text)
Dim MemoryStream As New MemoryStream
PictureBox1.BackgroundImage.Save(MemoryStream,
PictureBox1.BackgroundImage.RawFormat)
Dim Dphoto As Byte() = MemoryStream.GetBuffer
Dim Images As New SqlParameter("@Photo", SqlDbType.Image)
Images.Value = Dphoto
CMD.Parameters.Add(Images)
CMD.ExecuteNonQuery()
Else
Call koneksi()
Dim CMD As SqlCommand
Dim SQL As String
SQL = "UPDATE Employee SET EmpName='" & txtname.Text & "',DeptName='" & cmbdept.Text & "',EmpStatus='" & status & "',JoinDate='" & Format(dtpjoin.Value, "MM-dd-yyyy") & "', ResignDate='" & Format(dtpresigned.Value, "MM-dd-yyyy") & "', Address='" & txtaddress.Text & "' ,Photo='" & SqlDbType.Image & "' where EmpID='" & txtid.Text & "'"
CMD = New SqlCommand(SQL, CONN)
CMD.ExecuteNonQuery()
MsgBox("Saved")
End If
TextBox1.Clear()
PictureBox1.Image = Nothing
CMD.Dispose()
CONN.Close()
Call Awal()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
我有两个dateTimePickers,一个用于加入日期,另一个用于退出日期。 每个datetimepicker都会根据其工作状态(激活还是已辞职)被激活,并通过以下代码启用或禁用它们:
If rbactive.Checked = True Then
status = rbactive.Text
exresigndate = "Null"
ElseIf rbresigned.Checked = True Then
status = rbresigned.Text
exresigndate = "@ResignDate"
End If
* rb =单选按钮 在单选按钮代码的上方,我将exresigndate描述为String并给出了空值。
Dim exresigndate As String = ""
所以我可以用下面的代码来调用它。
然后我将exresigndate插入插入语法以填充ResignDate字段。
SQL = "INSERT INTO Employee VALUES (@EmpID, @EmpName, @DeptName, @EmpStatus, @JoinDate, " & exresigndate & ", @Address, @Photo)"
此后,我给有条件的员工打电话给exrisgndate(null value)
(对于在职员工),将ResignDate
的值称为dtreigned.value
(对于辞职的员工)。
有关进一步的解释,请查看此input form。