user12951147
user12951147

Reputation:

Search in form1 and display it to the form2 using vb.net windows form

In my form1 I have textbox where the user input their employee_number, and i have a second form where the data of that client will displayed.

This is my first form

Dim dt As New DataTable
Dim EmployeeNumber = EmployeeNumber_TextBox1.Text.Trim()
Try
    Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=contacttracing;server=localhost;port=5432;uid=ctadmin;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
            cmd As New Odbc.OdbcCommand("SELECT firstname   FROM ""TracingApp_fmcustomeremployeesupplier"" where employee_number='" & EmployeeNumber & "' ", MyCon)
        MyCon.Open()
        dt.Load(cmd.ExecuteReader)

        EmployeeInformation.Show()

    End Using
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

how do I do that when the employee enters their number in form1 the data will be displayed in the form2 textbox?

form1

enter image description here

form2

enter image description here

i dont have code yet in my form2 cause i dont know how to get the data from form1 and displayed it to form2

Upvotes: 0

Views: 183

Answers (2)

Kate
Kate

Reputation: 1836

You have at least two more options:

  1. Send an event from Form1 to Form2, which is a safer method. If the user has closed Form2, then Form1 will trigger an exception, unless you check that the form Form2 is indeed loaded and accessible. I think it is more elegant to broadcast an event and let the target form react to it.

  2. Overload the Show (or ShowDialog) method of the target form

You could overload the Show method in Form2 like this:

Public Class Form2
    Inherits System.Windows.Forms.Form

    Public Overloads Sub Show(ByVal ContactD As Integer)
        ' load the contact from DB
        MyBase.Show()
    End Sub

End Class

Basically, you add an alternative declaration for the method. Then in Form1 you instantiate Form2 like this:

Dim frm2 as new Form2
frm2.Show(123456) ' ContactID value

And you let Form2 fetch the data from the DB. So I think sending a contact ID or some primary key is sufficient, but you can send more variables if you want. In this case you could send a DataRow.

Upvotes: 0

Mary
Mary

Reputation: 15091

Always use Parameters. User input can be malicious. Parameters indicate to the database that this is only a value not executable code. Parameters help to prevent sql injection.

I changed the names of your controls to match my test program. Of course in your code you would use descriptive names.

In you CommandText, select all the fields you need to display. I had to guess at the names of the fields. Check your database for the correct names. Use the name of the parameter in the Where clause.

When you .Add the parameter check your database for the correct datatype. Since your code had the value of the parameter in single quotes I guessed VarChar. If it is an Int or some other number type be sure to CInt(TextBox1.Text) or whatever datatype you need to change to. You have probably validated the input elsewhere.

Only after the connection and command are disposed do we start using the data returned.

vb.net can work with what is called "the default instance" of forms. That is why this code worked. You can also create you own instance.

dt(0)(0).ToString

This refers to the first row, first column in the DataTable. (Arrays and Collections in .net are zero based)

dt(0)(1).ToString

Refers to the first row, second column or the DataTable and so on.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt As New DataTable
    Dim EmployeeNumber = TextBox1.Text.Trim()
    Try
        Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=contacttracing;server=localhost;port=5432;uid=ctadmin;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
                cmd As New Odbc.OdbcCommand("SELECT firstname, middlename, lastname FROM ""TracingApp_fmcustomeremployeesupplier"" where employee_number= @empNum' ", MyCon)
            cmd.Parameters.Add("@empNum", OdbcType.VarChar).Value = EmployeeNumber
            MyCon.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Form2.TextBox1.Text = dt(0)(0).ToString
    Form2.TextBox2.Text = dt(0)(1).ToString
    Form2.TextBox3.Text = dt(0)(2).ToString
    Form2.Show()
End Sub

Upvotes: 1

Related Questions