Using Combobox selected value as parametar in SQL Command

to simplify. I have created 2 Windows forms:
First form (named Hierarchy_load) contain Combobox control loaded with values from SQL Server database.
Second form (named Form1) represent treeview populated from SQLSerevr database.  
I need to forward selected value in combobox from Hierarchy_load into Form1_load sub and according to forwarded parameter treeview will display data from database. For unknown reason my code forwards nothing to Form1_load. Please help...attached my code (forwarded value as parameter in command should be defined in "Hierarchy_load.ComboBox1.Text".
Please help how to solve this problem
Thank you for your time and effort....
Kind regards...
Imports System.Data.SqlClient

Public Class Form1

    Private con As SqlConnection
    Private WithEvents cmdSelectHID As New SqlCommand()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DSNWind As DataSet
        Dim CNnwind As New SqlClient.SqlConnection("Data Source=S_A-PC\SQLEXPRESS1;Initial Catalog=AdventureWorks;Integrated Security=True")
Dim DACustomers As New SqlClient.SqlDataAdapter
        DACustomers.SelectCommand = cmdSelectHID
        DACustomers.SelectCommand.Connection = CNnwind
        DACustomers.SelectCommand.CommandText = "SELECT * FROM CostC1 WHERE HID = @param"
        DACustomers.SelectCommand.Parameters.Add("@param", SqlDbType.NVarChar)
        DACustomers.SelectCommand.Parameters("@param").Value = Hierarchy_load.ComboBox1.Text

        DSNWind = New DataSet()
        DACustomers.Fill(DSNWind, "dtCosts1")

        DSNWind.Relations.Add("Parenttochild", DSNWind.Tables("dtCosts1").Columns("ID"), DSNWind.Tables("dtCosts1").Columns("Reports"), False)


    End Sub

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to pass a reference to the Form1 of the form containing combobox. Declare a public variable and assign it when creating the form. Or use a custom constructor on form1

Public Class Form1
     Inherits Windows.Form
Dim Otherform as Hierarchy_Load

Public Sub New(frm As Hierarchy_load)
OtherForm = frm

Then use this OtherForm to get the combo text
alsamAuthor Commented:
Thanks for answer...
I lost mysef in try to understand and how to introduce this part in my code...
Could you just help me and use my attached code to adjust the same with your proposed solution...
Thank you very much....appreciate your time and effort
You have this line in your code above

Public Class Form1

Add following below the above line

Dim frm as Hierarchy_load
Public Sub New(f as Hierarchy_load)
frm = f

Then change this line
DACustomers.SelectCommand.Parameters("@param").Value = Hierarchy_load.ComboBox1.Text

DACustomers.SelectCommand.Parameters("@param").Value = frm.ComboBox1.Text

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alsamAuthor Commented:
Thank you.
Works perfectly.
Hope to have you as help in future.
Short, clean and now very understanable solution for me.
Thank you once more time.
Glad to help now and in future :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.