如何防止在填写DataGridView列之前调用DateTime参数?

时间:2019-06-12 06:16:50

标签: datagridview

我有一个带有4列的DataGridView,可以使用参数将其添加或动态编辑。只要首先填写了StartDate(第4列),所有内容都将起作用。当我正常(从左到右)填充DGV时,查询失败并给出错误“ System.FormatException:'字符串未被识别为有效的DateTime。”如何防止在DataGridView行完成之前激发DateTime参数。

我尝试将DBNull插入StartDate列单元格中,直到DGV填充完毕,但出现存储过程错误“参数过多”。我在代码底部的“注释”中包含了存储过程代码,以供参考。  预先感谢,lejoc

导入System.Data.SqlClient 公共课程表格1

Private connectionstring As String = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Employment;Integrated Security=True;"

Private Sub PopulatePositionComboBox()
    Using sqlCon As SqlConnection = New SqlConnection(connectionstring)
        sqlCon.Open()
        Dim sqlDa As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Position", sqlCon)
        Dim dt As DataTable = New DataTable()
        sqlDa.Fill(dt)
        cbxPositionID.ValueMember = "PositionID"
        cbxPositionID.DisplayMember = "Position"
        Dim topItem As DataRow = dt.NewRow()
        topItem(0) = 0
        topItem(1) = "-Select-"
        dt.Rows.InsertAt(topItem, 0)
        cbxPositionID.DataSource = dt
    End Using
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    PopulatePositionComboBox()
    PopulateDataGridView()
End Sub

Private Sub PopulateDataGridView()
    Using sqlCon As SqlConnection = New SqlConnection(connectionstring)
        sqlCon.Open()
        Dim sqlDa As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Employee", sqlCon)
        Dim dt As DataTable = New DataTable()
        sqlDa.Fill(dt)
        DgvEmployment.DataSource = dt
    End Using
End Sub

Private Sub DgvEmployment_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DgvEmployment.CellValueChanged
    If DgvEmployment.CurrentRow IsNot Nothing Then
        Using sqlCon As SqlConnection = New SqlConnection(connectionstring)
            sqlCon.Open()
            Dim dgvRow As DataGridViewRow = DgvEmployment.CurrentRow
            Dim sqlCmd As SqlCommand = New SqlCommand("AddOrEdit", sqlCon)
            sqlCmd.CommandType = CommandType.StoredProcedure

            ' INSERT
            If dgvRow.Cells("txtEmployeeID").Value Is DBNull.Value Then
                sqlCmd.Parameters.AddWithValue("@EmployeeID", 0)
            Else
                ' UPDATE
                sqlCmd.Parameters.AddWithValue("@EmployeeID", Convert.ToInt32(dgvRow.Cells("txtEmployeeID").Value))
            End If

            ' PROBLEM AT HERE, FAILS Query because looking for VALID DATE after 'txtName' is entered 
            'ERROR MESSAGE = System.FormatException: 'String was not recognized as a valid DateTime.'
            sqlCmd.Parameters.AddWithValue("@Name", If(dgvRow.Cells("txtName").Value Is DBNull.Value, "", dgvRow.Cells("txtName").Value.ToString()))
            sqlCmd.Parameters.AddWithValue("@PositionID", Convert.ToInt32(If(dgvRow.Cells("cbxPositionID").Value Is DBNull.Value, "0", dgvRow.Cells("cbxPositionID").Value)))
            sqlCmd.Parameters.AddWithValue("@Office", If(dgvRow.Cells("txtOffice").Value Is DBNull.Value, "", dgvRow.Cells("txtOffice").Value.ToString()))
            sqlCmd.Parameters.AddWithValue("@StartDate", Convert.ToDateTime(If(dgvRow.Cells("txtStartDate").Value Is DBNull.Value, "0", dgvRow.Cells("txtStartDate").Value)))
            sqlCmd.ExecuteNonQuery()
            PopulateDataGridView()
        End Using
    End If
End Sub


'The code for the STORED PROCEDURE  dbo.AddOrEdit
'    CREATE PROC AddOrEdit
'@EmployeeID int,
'@Name varchar(100),
'@PositionID int,
'@Office varchar(50),
'@StartDate date
'As
'If  @EmployeeID  = 0
'INSERT INTO Employee (Name, PositionID, Office, StartDate)
'VALUES (@Name, @PositionID, @Office, @StartDate)
'Else
'UPDATE Employee
'    Set
'Name = @Name,
'PositionID = @PositionID,
'Office = @Office,
'StartDate = @StartDate
'WHERE EmployeeID = @EmployeeID

我希望DGV从左到右以“ StartDate”作为最后一栏填写。 EG名称,PositionID,Office,StartDate。

1 个答案:

答案 0 :(得分:0)

提供您的StartDate列是可以为空的,那么我认为可以通过更改行来解决

sqlCmd.Parameters.AddWithValue("@StartDate",  Convert.ToDateTime(If(dgvRow.Cells("txtStartDate").Value Is DBNull.Value, "0", dgvRow.Cells("txtStartDate").Value)))

sqlCmd.Parameters.AddWithValue("@StartDate", If(dgvRow.Cells("txtStartDate").Value Is DBNull.Value, DBNull.Value, Convert.ToDateTime(dgvRow.Cells("txtStartDate").Value)))

因此,如果未填写,请将NULL保存到数据库,否则将您的txtStartDate字段转换为DateTime并保存。

将“ 0”保存为DateTime无效。