删除选定的行dgv

时间:2018-01-30 17:16:24

标签: sql-server vb.net

我在从datagridview删除数据时遇到问题。我不知道为什么要如何解决它。所以,在我按下删除按钮并在dialogresult上选择是按钮之后。我收到了这个错误; “错误:列名EMP无效”。帮帮我们谢谢你提前!!

代码:

Public Sub deleteEmployee()
        employeesView.btnDelete.Text = MessageBox.Show("Are you sure you want to delete the selected employee?", "WARNING: DELETION OF EMPLOYEE!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
        If employeesView.btnDelete.Text = DialogResult.Yes Then
            employeesView.btnDelete.Text = "Delete"
            Try
                dbConnection()
                Dim employee_number As String = employeesView.EmployeeDGV.SelectedRows(0).Cells("employee_number").Value
                delete_query = "DELETE FROM tblemployee_information WHERE employee_number = " & employee_number
                command = New SqlCommand
                With command
                    .Connection = connection
                    .CommandText = delete_query
                    result = .ExecuteNonQuery()
                    If result = 0 Then
                        MsgBox("Error in deleting employee!")
                    Else
                        MsgBox("Successfully deleted the selected employee!")
                    End If
                End With
            Catch ex As SqlException
                MsgBox("Error : " + ex.Message)
            Finally
                connection.Close()
                command.Dispose()
                retrieveEmployees()
            End Try
        Else
            employeesView.btnDelete.Text = "Delete"
        End If
    End Sub

按钮:

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        deleteEmployee()
    End Sub

表格

--Table Structure for Employees Information
CREATE TABLE tblemployee_information (
    employee_number varchar(50) NOT NULL PRIMARY KEY,
    employee_first_name varchar(100) NOT NULL,
    employee_middle_name varchar(100) NOT NULL,
    employee_last_name varchar(100) NOT NULL,
    employee_contact varchar(100) NOT NULL,
    employee_address varchar(200) NOT NULL  
);

--Table Structure for Employees Account
CREATE TABLE tblemployee_account (
    employee_account_id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    employee_email varchar(100) NOT NULL,
    employee_username varchar(50) NOT NULL,
    employee_password varchar(50) NOT NULL,
    employee_role varchar(50) NOT NULL,
    employee_number varchar(50) FOREIGN KEY REFERENCES tblemployee_information(employee_number) ON DELETE CASCADE
);

1 个答案:

答案 0 :(得分:0)

如果您使用参数,您将避免许多小问题和大问题。在这里你有一个PK的字符串,但你把它当作一个数字。如果正确使用参数,则无需担心。

Dim cn As SqlConnection = New SqlConnection("Connection String")
Dim delete_query As String = "DELETE FROM tblemployee_information WHERE employee_number = @empNumber;"
Dim Command = New SqlCommand With {
      .Connection = cn,
      .CommandType = CommandType.Text,
      .CommandText = delete_query}
Command.Parameters.Add("@empNumber", SqlDbType.VarChar, 50).Value = employee_number
Dim Result As Integer = Command.ExecuteNonQuery()