使用二维数组中的项目将记录添加到表中

时间:2018-01-03 21:31:50

标签: vb.net

该表已有记录。我要做的就是将记录追加到表中。我在一张名为FLOORS的桌子上有许多楼层。我想在桌子上插入额外的地板。在表单的字段中输入表的数量。如果在表单上输入的数字是>在Table.Count中,生成附加记录并将其添加到表中。异常消息 - “更新失败”

 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
      Try
           Dim FirstCharacter As String
           Dim LastCharacter As String
           Dim ExtraFloors As Integer

           Floor = Me.FloorDbDataSet.Tables("Floor").Rows.Count + 1
           If Floor <= CInt(TxtNoOfFloors.Text) Then
                ExtraFloors = (CInt(TxtNoOfFloors.Text) - Floor)
                MsgBox("Extra Floors - " & ExtraFloors)
           End If
           Floor = Floor + 1
           MsgBox("First floor to be added - " & Floor)
           'setup array
           Dim FloorDetail(ExtraFloors - 1, 1) As String '(row,col)
           For intRow As Integer = 0 To ExtraFloors - 1
                If Floor < 10 Then
                     strFloorNo = "0" & Floor
                Else
                     strFloorNo = Floor
                End If

                FirstCharacter = strFloorNo(0)
                LastCharacter = strFloorNo(strFloorNo.Length - 1)
                If LastCharacter = "1" Then
                     strDescription = FirstCharacter & "1st Floor"
                ElseIf LastCharacter = "2" Then
                     strDescription = FirstCharacter & "2nd Floor"
                ElseIf LastCharacter = "3" Then
                     strDescription = FirstCharacter & "3rd Floor"
                Else
                     strDescription = strFloorNo & "th Floor"
                End If
                FloorDetail(intRow, 0) = strFloorNo
                FloorDetail(intRow, 1) = strDescription
                MsgBox(strDescription & " ==> " & strFloorNo)
                Floor = Floor + 1
           Next intRow

           Dim NewFloorRow As DataRow = FloorDbDataSet.Floor.NewFloorRow
           For outerIndex As Integer = 0 To ExtraFloors - 1
                NewFloorRow("FloorNo") = FloorDetail(outerIndex, 0)
                NewFloorRow("Description") = FloorDetail(outerIndex, 1)
                FloorDbDataSet.Floor.Rows.Add(NewFloorRow)
           Next
           Me.FloorTableAdapter.Update(FloorDbDataSet.Floor)
           FloorTableAdapter.Fill(FloorDbDataSet.Floor)
           FloorBindingSource.MoveLast()
      Catch ex As Exception
           MsgBox(strDescription & " ==> Update failed")
      End Try
 End Sub

1 个答案:

答案 0 :(得分:0)

Plutonix对您的错误处理绝对正确。

但是,最初的问题是:

Dim NewFloorRow As DataRow = FloorDbDataSet.Floor.NewFloorRow

这应该是INSIDE循环。否则,您的代码会尝试重复添加相同的行,这将导致错误。

       For outerIndex As Integer = 0 To ExtraFloors - 1
            Dim NewFloorRow As DataRow = FloorDbDataSet.Floor.NewFloorRow
            NewFloorRow("FloorNo") = FloorDetail(outerIndex, 0)
            NewFloorRow("Description") = FloorDetail(outerIndex, 1)
            FloorDbDataSet.Floor.Rows.Add(NewFloorRow)
       Next

建议更改异常处理:

MsgBox(strDescription & " ==> Update failed" & Environment.NewLine & ex.Message)

ex.Message是实际的错误消息。异常还有其他属性,就像stacktrace一样,在某些模式下会给出错误的行号。