简体   繁体   中英

I can't delete a row from MS access database from VB.net

I'm trying to delete a row from MS database by following a YT tutorial but I got 2 kinds of errors. When I try this:

    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection

    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        dataFile = "C:\Users\user\Desktop\PU\SEMESTER 4\Visual Programming\Hospital Databases\Patient DB.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString
    myConnection.Open()
    Dim str As String
    str = "Delete * from [Patient] Where [PatientID] = " & PatientID.ToString & ""
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

I got this error: "Malformed GUID in query expression '[PatientID]=DataGridViewTextBoxColumn { Name=PatientID, index=0 } '

So I used brackets as stated here so now it's like this:

str = "Delete * from [Patient] Where [PatientID] = [" & PatientID.ToString & "]"

But this gave an error: "No value given for one or more required parameters". When I replaced the brackets with single quotes I also get this error.

I tried this , this , and this but still got an error.

Idk what to do.

EDIT:

I did PatientID.ToString because I want to make it so that when a user clicks on an ID cell, it will get the ID without the user having to input anything. But apparently that was wrong so I made an inputbox and followed Mary's answer. But I got this error on the Execute... line: "No value given for one or more required parameters"

I searched and most of them said to put single quotes for it to work. I did (^) and still got that error:( Did I call the sub in the wrong way?

    Private Sub DischargeToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles DischargeToolStripMenuItem1.Click
        Dim ID As Integer
        ID = InputBox("Enter the ID of the patient to be discharged", "Discharge")
        DeleteRecord(ID)
    End Sub

    Sub DeleteRecord(ID As Integer)
        Dim provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        Dim dataFile = "C:\Users\user\Desktop\PU\SEMESTER 4\Visual Programming\Hospital Databases\Patient DB.accdb"
        Dim connString = provider & dataFile
        Using myConnection As New OleDbConnection(connString),
                cmd As New OleDbCommand("Delete * from Patient Where PatientID = @ID", myConnection)
            cmd.Parameters.Add("@ID", OleDbType.Integer).Value = ID
            myConnection.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Sub

Connections and commands need to be disposed. Using...End Using blocks will take care of closing connections as well as disposing connections and commands. In this code both the connection and command are included in the same block. Note the comma at the end of the first line of the Using .

Don't open the connection until the last possible moment, directly before the .Execute... line. Close the connection with the End Using right after that.

Always use parameters. With Access we use parameter names for ease of reading the code. Access pays no attention to the names. What is important is the order that the parameter appears in the sql query must match the order that the parameter is added to the parameters collection. I had to guess the datatype of the parameter. Check your database for the actual type.

Private Sub DeleteRecord(ID As Integer)
    Dim provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    Dim dataFile = "C:\Users\user\Desktop\PU\SEMESTER 4\Visual Programming\Hospital Databases\Patient DB.accdb"
    Dim connString = provider & dataFile
    Using myConnection As New OleDbConnection(connString),
            cmd As New OleDbCommand("Delete * from Patient Where PatientID = @ID", myConnection)
        cmd.Parameters.Add("@ID", OleDbType.Integer).Value = ID
        myConnection.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

EDIT

User input should always be validated. A user may or may not enter something that can be converted to a number. Serveral datatypes have a .TryParse method to test with. .TryParse returns a Boolean so it fits right into an If statement. The first parameter string you want to test. The second parameter is a variable of the type you want to convert to. .TryParse will fill in the second variable with the converted value.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'An InputBox always returns a String, not a number
    Dim ID = InputBox("Enter the ID of the patient to be discharged", "Discharge")
    Dim intID As Integer
    If Integer.TryParse(ID, intID) Then
        DeleteRecord(intID)
    Else
        MessageBox.Show("Please enter a valid number")
    End If
End Sub

I think your problem is in the definition of datafile in that it is not reading the file name properly. While I agree with others that using parameters is the best way to proceed, in the interim, try this:

Dim MyFile as string = "Patient DB.accdb"
Dim dataFile = "C:\Users\user\Desktop\PU\SEMESTER 4\Visual Programming\Hospital Databases\" & MyFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM