0

我做了一个Boarding House系统,一切都很顺利,直到我发现这个错误。当我单击 btnDone 时,我想将其保存到我的 Oracle 数据库中。我不知道为什么我可以通过 dsNewRow 下的 boarderpic 将 boarder_id 保存到我的 tblBoarder2 中,但是在 dsNewRow2 下,我似乎无法将它保存在我的另一个表 tblAddItems 中。

我不明白如何编码。有人可以帮忙吗?提前致谢。我实际上是编码新手,并且从上两个月开始对 vb.net 感兴趣,所以我的经验非常低。

这是我的代码:

Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim picfaculty As String
        Dim filetocopy As String
        Dim newcopy As String

        filetocopy = picPath

        newcopy = Application.StartupPath & "\Images\" & Form3.lblPicname.Text
        If System.IO.File.Exists(filetocopy) = True Then
            System.IO.File.Copy(filetocopy, newcopy)
            picfaculty = Form3.lblPicname.Text
        Else
            picfaculty = "pic.png"

        End If

        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("tblboarder2").NewRow() '<--This is the part w/ the error
        dsNewRow.Item("boarder_id") = lblID.Text
        dsNewRow.Item("lname") = Form3.txtlname.Text
        dsNewRow.Item("fname") = Form3.txtfname.Text
        dsNewRow.Item("mi") = Form3.txtmi.Text
        dsNewRow.Item("age") = Form3.txtage.Text
        dsNewRow.Item("gender") = Form3.cboGender.Text
        dsNewRow.Item("occupation") = Form3.txtOccupation.Text
        dsNewRow.Item("roomnum") = Form3.cboRoomnum.Text
        dsNewRow.Item("boarderpic") = picfaculty

        ds.Tables("tblboarder2").Rows.Add(dsNewRow)
        da.Update(ds, "tblboarder2")


        Dim dsNewRow2 As DataRow

        dsNewRow2 = ds.Tables("tblAddItems").NewRow()
        dsNewRow2.Item("boarder_id") = lblID.Text
        dsNewRow2.Item("Item") = "Blanket"
        dsNewRow2.Item("Quantity") = Form3.cboBlanket.Text

        ds.Tables("tblAddItems").Rows.Add(dsNewRow2)
        da.Update(ds, "tblAddItems")

        MsgBox("New Record Added to the Database", MsgBoxStyle.Information, "Save")
        Me.Close()
        Form3.Close()



    End Sub
4

1 回答 1

0

How certain are you the error is on that line? I am suspicious of the preceding condition. I have used similar code before and found the object reference error occurrs in the file copy:

System.IO.File.Copy(filetocopy, newcopy)

because the file already exists in the destination and its not set to overwrite. You are using the first overload. Try using the second like this:

System.IO.File.Copy(filetocopy, newcopy, true)

And see if your error goes away.

-d

于 2013-11-14T06:48:08.083 回答