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

时间:2014-04-28 06:36:20

标签: database vb.net sqlclient

我有一个问题。当我运行项目并单击“保存”按钮后,此错误消息将显示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

结束班

1 个答案:

答案 0 :(得分:1)

您的代码订购不正确。您在初始化之前使用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的初始化,并将ExecuteNonQuery作为最后一条指令移动