How can i transfer data in usercontrol to other usercontrol in VB

I’m trying to control a second user control form with this code.

` Public Sub LoadCandidateData(candidateID As Integer)
Dim editCandidateForm As New databankEditcandidate()

    Using connection As OleDbConnection = DatabaseManager.GetConnection()
        Try
            connection.Open()
            Dim query As String = "SELECT * FROM dataBank WHERE ID = @CandidateID"
            Using command As New OleDbCommand(query, connection)
                command.Parameters.AddWithValue("@CandidateID", candidateID)

                Using reader As OleDbDataReader = command.ExecuteReader()
                    If reader.Read() Then
                        editCandidateForm.ApplicantNameText = reader("applicantName").ToString()
                        editCandidateForm.id = reader("ID")
                    End If
                End Using
            End Using
            MessageBox.Show("Loaded applicant name: " & candidateID & editCandidateForm.ApplicantNameText)
            'get message to check if candidateID is not empty
        Catch ex As Exception
            MessageBox.Show("Error loading candidate data: " & ex.Message)
        Finally
            connection.Close()
        End Try
    End Using
    ShowsDatabankAddcandidateUserControl()
End Sub`

I want to execute this code to transfer the ID and name to my second user control so that I can retrieve them.

` Private _candidateID As Integer

Public Property CandidateID As Integer
    Get
        Return _candidateID
    End Get
    Set(value As Integer)
        _candidateID = value
    End Set
End Property
Public Property ApplicantNameText As String
    Get
        Return applicantNameTxt.Text
    End Get
    Set(value As String)
        applicantNameTxt.Text = value
    End Set
End Property
Public Property id As String
    Get
        Return idTxt.Text ' Return the value of the applicantNameTxt control or the actual ID field.
    End Get
    Set(value As String)
        idTxt.Text = value
    End Set
End Property




Public Sub LoadCandidateData()
    Dim editCandidateForm As New databankEditcandidate()
    editCandidateForm.CandidateID = _candidateID
    Try
        Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\HR-Recruitment-System\bin\Debug\recruitmentDatabase.accdb"

        Using connection As New OleDbConnection(connectionString)
            connection.Open()
            Dim query As String = "SELECT * FROM dataBank WHERE ID = @ID"
            Using command As New OleDbCommand(query, connection)
                command.Parameters.AddWithValue("@ID", idTxt.Text)

                Using reader As OleDbDataReader = command.ExecuteReader()
                    If reader.Read() Then
                        ' Retrieve and populate the form fields with candidate data.
                        applicantNameTxt.Text = reader("applicantName").ToString()
                        applicantNationalityTxt.Text = reader("applicantNationality").ToString()
                        applicantGenderTxt.Text = reader("applicantGender").ToString()
                        applicantAgeTxt.Text = reader("applicantAge").ToString()
                        applicantContactTxt.Text = reader("applicantContact").ToString()
                        applicantEmailTxt.Text = reader("applicantEmail").ToString()
                        applicantEducationTxt.Text = reader("applicantEducation").ToString()
                        applicantYearsofExperienceTxt.Text = reader("applicantYearsOfExperience").ToString()
                        applicantPreviouscompanyTxt.Text = reader("applicantPreviousCompany").ToString()
                        applicantPreviouspositionTxt.Text = reader("applicantPreviousPosition").ToString()
                        applicantAppliedTxt.Text = reader("applicantApplied").ToString()
                        applicantCurrentlocationTxt.Text = reader("applicantCurrentLocation").ToString()
                        ComboBox1.Text = reader("applicantLineupStatus").ToString()
                        applicantLineupremarksTxt.Text = reader("applicantLineupRemarks").ToString()
                        ComboBox2.Text = reader("applicantSource").ToString()
                        ComboBox3.Text = reader("applicantDateSource").ToString()
                        ComboBox4.Text = reader("applicantStatus").ToString()
                    End If
                End Using
            End Using
        End Using
    Catch ex As Exception
        MessageBox.Show("Error loading candidate data: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub`

I want to display the data using MessageBox.Show when the CandidateID and ID have a value of 0.

I want to retrieve personal information from the database via a UserControl named ‘UserProfileCard’ and then display the information in a second UserControl named ‘DataBankEditCandidate’.

  • A user control is just a control, like any other. How would you usually transfer data from one control to another? You would write code in the form to get a property value from the source control and set a property value in the destination control. That’s what you do here. If the source control needs to notify the form that the data is available then the source control needs to raise an event. For example, you might handle the TextChanged event of a Textbox, get its Text property and set the Text property of a Label to the same or some related value.

    – 

  • The only difference here is that you’re designing the controls, so it’s up to you to define that appropriate event and properties. Note that, if appropriate, you might call a method and get the return value rather than get a property value and you might call a method and pass one or more arguments rather than set a property. You can also pass the data out directly through the event, if appropriate. If you need an event, you might like to read this.

    – 

  • Reading your question carefully, you seem to be asking multiple questions in one. Break the problem down into smaller parts and address one at a time. If you can’t do it yourself, as us and provide ONLY the information relevant to that part. Once that’s done, incorporate the solution and go on with the next part. If you try to solve multiple problems at the same time, you’ll probably solve none.

    – 

  • thank you for your suggestion i will try your idea on this.

    – 

Leave a Comment