Etto Sama
Etto Sama

Reputation: 23

Display record in datagridview after adding to database

I am able to add my records into my database without any problem, but I have trouble displaying it automatically into my datagridview.

In order for me to view my records in my datagridview, I need to close and restart the whole thing for it to appear. Is there any code that I've missed?

 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

Upvotes: 0

Views: 394

Answers (2)

Cody O. Putzier
Cody O. Putzier

Reputation: 1

You need to call your select statement subroutine again. The statement where you pulled your information from the database.

Try
    cmdEmployee.ExecuteNonQuery()
    tranEmployee.Commit()
Catch ex As Exception
    tranEmployee.Rollback()
    MessageBox.Show(ex.Message)
Finally
    conn.Close()
    selectUsers()
End Try

Upvotes: 0

David
David

Reputation: 219037

The code you've shown will successfully add a record to the database, but it doesn't make any attempt to refresh anything in the UI. Where is the code which binds the grid to records in the database? That code needs to be run again after this code runs.

I'm assuming that code exists in some sort of initializer for the form. Perhaps some sort of load event? You'll want to move that grid-binding code into its own function and call it from both the load event and at the end of this click event, probably right after the line where you commit the transaction.

Upvotes: 1

Related Questions