我可以毫无问题地将我的记录添加到我的数据库中,但是我无法将其自动显示到我的 datagridview 中。
为了让我在我的 datagridview 中查看我的记录,我需要关闭并重新启动整个内容以使其显示。有没有我错过的代码?
Private Sub btnAddEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEmp.Click
Dim tranEmployee As SqlClient.SqlTransaction
sAdapter = New SqlDataAdapter(cmdEmployee)
Dim strID As String
Dim strName As String
Dim strPosition As String
Dim strContactNo As String
Dim strAddress As String
Dim strDOB As String
Dim strGender As String
Dim strSQL As String
conn.Open()
strID = mskEmployeeID.Text
strName = txtEmpName.Text
strPosition = cboEmpPosition.Text
strContactNo = mskEmpDOB.Text
strDOB = mskEmpDOB.Text
strAddress = txtEmpAddress.Text
If radEmpMale.Checked Then
strGender = "Male"
Else
strGender = "Female"
End If
strSQL = "INSERT INTO Users(userID,userName,userPosition,userGender,userDOB,userAddress)" & _
"VALUES(@ID,@NAME,@POSITION,@GENDER,@DOB,@ADDRESS)"
tranEmployee = conn.BeginTransaction()
With cmdEmployee
.Transaction = tranEmployee
.CommandText = strSQL
.Parameters.AddWithValue("@ID", strID)
.Parameters.AddWithValue("@NAME", strName)
.Parameters.AddWithValue("@POSITION", strPosition)
.Parameters.AddWithValue("@GENDER", strGender)
.Parameters.AddWithValue("@DOB", strDOB)
.Parameters.AddWithValue("@ADDRESS", strAddress)
.Connection = conn
End With
Try
cmdEmployee.ExecuteNonQuery()
tranEmployee.Commit()
Catch ex As Exception
tranEmployee.Rollback()
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
End Sub