I have a main form with a combobox linked to a dataset when the user selects an account I have another form that opens the customers details on it. I can get it to display the first customer in the combobox but if select a different customer the form loads blank. I have attached the code in the customers detail form. To load the form I use the selectedindexchanged event from the combobox and the form.show() to load. Any thoughts would be great on this one!

Private Sub CustomerDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Con = New SqlConnection("Data Source=WHITES;Initial Catalog=ESHAN;Integrated Security=True")
        con.Open()
        Dim cmd As New SqlCommand
        Dim rdr As SqlDataReader
        Dim Param As New SqlParameter
        cmd.CommandText = "Select * From dbo.dbo_SLMaster WHERE Name =@Name"
        cmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar, 10)).Value = Main_Screen.cboLinenCust.Text


        cmd.Connection = con

        rdr = cmd.ExecuteReader()

        Try


            While rdr.Read()
                txtAcc.Text = rdr("AccountNo")
                txtName.Text = rdr("Name")
                txtAdd1.Text = rdr("Address1")
                txtAdd2.Text = rdr("Address2")
                txtAdd3.Text = rdr("Address3")
                txtAdd4.Text = rdr("Address4")
                txtPost.Text = rdr("Postcode")
                txtPhone.Text = rdr("Phone")
                txtFax.Text = rdr("FaxNo")
                txtMobile.Text = rdr("MobileNo")
                txtEmail.Text = rdr("Email")



            End While
        Catch ex As Exception
            MessageBox.Show("Error " & ex.ToString)

        End Try

remove the code from the load event and create a method in this form such as:

Friend Sub fillDetails (byval _name as string)
 Con = New SqlConnection("Data Source=WHITES;Initial Catalog=ESHAN;Integrated Security=True")
        con.Open()
        Dim cmd As New SqlCommand
        Dim rdr As SqlDataReader
        Dim Param As New SqlParameter
        cmd.CommandText = "Select * From dbo.dbo_SLMaster WHERE Name =@Name"
        cmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar, 10)).Value = _name

        cmd.Connection = con
        rdr = cmd.ExecuteReader()
        Try
            While rdr.Read()
                txtAcc.Text = rdr("AccountNo")
                txtName.Text = rdr("Name")
                txtAdd1.Text = rdr("Address1")
                txtAdd2.Text = rdr("Address2")
                txtAdd3.Text = rdr("Address3")
                txtAdd4.Text = rdr("Address4")
                txtPost.Text = rdr("Postcode")
                txtPhone.Text = rdr("Phone")
                txtFax.Text = rdr("FaxNo")
                txtMobile.Text = rdr("MobileNo")
                txtEmail.Text = rdr("Email")
            End While
        Catch ex As Exception
            MessageBox.Show("Error " & ex.ToString)
        End Try
me.show
end Sub

in the selectedIndexChanged event you just call: CustomerDetail.fillDetails(cboLinenCust.selectedItem.toString)

Thats great! Thank you so much for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.