错误对象引用未设置为vb中对象的实例



我有个问题。当我运行项目并单击"保存"按钮后,将显示此错误消息https://www.dropbox.com/s/drpopoqi3etob3d/Presentation1.png

以下是项目的代码

Imports System.Data.SqlClient

公共类frmAddNewStaffAdmin

Dim con As SqlClient.SqlConnection
Dim dbSource As String
Dim cmd As SqlClient.SqlCommand
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Try
        If Len(Trim(cboRole.Text)) = 0 Then
            MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            cboRole.Focus()
            Return
        ElseIf Len(Trim(txtStaffID.Text)) = 0 Then
            MessageBox.Show("Please enter Staff ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtStaffID.Focus()
            Return
        ElseIf Len(Trim(txtStaffName.Text)) = 0 Then
            MessageBox.Show("Please enter Staff Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtStaffName.Focus()
            Return
        ElseIf Len(Trim(txtUsername.Text)) = 0 Then
            MessageBox.Show("Please enter Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtUsername.Focus()
            Return
        ElseIf Len(Trim(txtPassword.Text)) = 0 Then
            MessageBox.Show("Please enter Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtPassword.Focus()
            Return
        End If
        dbSource = "Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|HMS.mdf;Integrated Security=True;User Instance=True"
        con = New SqlConnection(dbSource)
        con.Open()
        Dim sql As String = "INSERT INTO [User] ([Staff_ID], [Staff_Role], [Staff_Name], [Username], [Password]) VALUES (@StaffID, @Role, @StaffName, @Username, @Password) "
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        cmd.Parameters.AddWithValue("@Staff_ID", txtStaffID.Text)
        cmd.Parameters.AddWithValue("@Role", cboRole.Text)
        cmd.Parameters.AddWithValue("@StaffName", txtStaffName.Text)
        cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
        cmd = New SqlCommand(sql)

        MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
        con.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Me.Hide()
    frmStaffAdmin.Show()
End Sub

最终类

您的代码排序不正确。在初始化之前使用SqlCommand,当然这会导致上面提到的错误

    dbSource = "Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|HMS.mdf;Integrated Security=True;User Instance=True"
    con = New SqlConnection(dbSource)
    con.Open()
    Dim sql As String = "INSERT INTO [User] ([Staff_ID], [Staff_Role], [Staff_Name], [Username], [Password]) VALUES (@StaffID, @Role, @StaffName, @Username, @Password) "
    cmd = New SqlCommand(sql)
    cmd.Connection = con
    cmd.Parameters.AddWithValue("@StaffID", txtStaffID.Text)
    cmd.Parameters.AddWithValue("@Role", cboRole.Text)
    cmd.Parameters.AddWithValue("@StaffName", txtStaffName.Text)
    cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
    cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
    cmd.ExecuteNonQuery()
    MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
    con.Close()

在第一次使用SqlCommand之前移动了SqlCommand的初始化,并将ExecuteNonQuery作为最后一条指令移动

相关内容

  • 没有找到相关文章

最新更新