DataBinding Newbie Help - Stuck

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
So i am trying to recreate a VB6 program into VB.net, but am having trouble understanding DataBinding.
I have the following windows form:
-See image in next post.

So when this form loads, I need to fill the combo box with values to choose from. The combobox has several columns. Here is my code so far:
VB.NET:
    Private Sub LoadEmps()

        If mstroracle = "ORAP" Then
            oracletype = My.Settings.ORAP_Conn
        ElseIf mstroracle = "ORAT" Then
            oracletype = My.Settings.ORAT_Conn
        End If

        'Build ConnectionString based on user login details
        Dim odbcbuilder As New OdbcConnectionStringBuilder(oracletype)
        odbcbuilder.Add("UID", mstruser)
        odbcbuilder.Add("PWD", oldpassword)
        'Attempt to connect to Oracle Database
        oracle_conn2.ConnectionString = odbcbuilder.ConnectionString
        oracle_conn2.Open()

        Dim strTSQL As String
        strTSQL = "SELECT   s.esec_empl_id,s.esec_user_id,s.esec_empl_type,s.esec_lastlog_dt,s.esec_priv_01," & vbCrLf
        strTSQL = strTSQL & "         s.esec_priv_02,s.esec_priv_03,s.esec_priv_04,s.esec_priv_05,s.esec_priv_06," & vbCrLf
        strTSQL = strTSQL & "         s.esec_priv_07,s.esec_priv_08,s.esec_priv_09,s.esec_priv_10,s.esec_priv_11," & vbCrLf
        strTSQL = strTSQL & "         s.esec_priv_12,s.esec_priv_13,s.esec_priv_14,s.esec_priv_15,s.esec_priv_16," & vbCrLf
        strTSQL = strTSQL & "         CASE " & vbCrLf
        strTSQL = strTSQL & "              WHEN e.empl_last_name IS NULL " & vbCrLf
        strTSQL = strTSQL & "                   THEN '' " & vbCrLf
        strTSQL = strTSQL & "                   ELSE e.empl_last_name||', '||e.empl_first_name|| " & vbCrLf
        strTSQL = strTSQL & "                   CASE " & vbCrLf
        strTSQL = strTSQL & "                        WHEN e.empl_middle_init = ' ' " & vbCrLf
        strTSQL = strTSQL & "                             THEN '' " & vbCrLf
        strTSQL = strTSQL & "                             ELSE ' '||e.empl_middle_init||'.' " & vbCrLf
        strTSQL = strTSQL & "                        END " & vbCrLf
        strTSQL = strTSQL & "               END EMPLOYEE " & vbCrLf
        strTSQL = strTSQL & "FROM     fset.tfsetesec s LEFT JOIN fset.tfsetempl e ON s.esec_empl_id=TRIM(e.empl_id) " & vbCrLf
        strTSQL = strTSQL & "ORDER BY 2 ASC "

        oracle_da = New Odbc.OdbcDataAdapter(strTSQL, oracle_conn2)
        oracle_da.Fill(oracle_ds, "Security")

    End Sub

This is where I am stuck. The combobox should contain s.esec_empl_id,s.esec_user_id, and EMPLOYEE from the above sql. Then when someone selects someone from the list, it should populate the fields and add check marks to the appropriate boxes. These values are stored in the other parts of the sql above. What do I do next. I am stuck here. After changes are made, I need to update, delete etc the record.

Any help that you could provide would be very much appreciated.
 
Image didnt post for some reason.
untitled.JPG
 
Can anyone help? Is there a simpler way. I know how to do it in legendary ado, but not this ado.net databinding stuff. Please help.
 
Please, can anyone help. I cannot go any further on my project until I can figure this out. Please help. Thank you.
 
Data-binding has nothing whatsoever to do with ADO.NET. ADO.NET is a data access technology while data-binding is purely a presentation, i.e. UI, task. In a well architected application the two will take place in completely separate projects. Data-binding is about creating a relationship between data and a UI element. That data may come from a database but it may not and it's irrelevant either way. You bind a list to your control and what type of list it is and how it was populated is of absolutely no concern to the data-binding mechanism.

So, you already have your list, i.e. the DataTable you populated with data from the database. To bind that to a ComboBox you simply need to set three properties:

1. Set the DisplayMember to the name of the column that you want to display to the user.
2. Optionally set the ValueMember to the name of the column that you want to use in code, which is usually the primary key.
3. Assign the list, i.e. the DataTable, to the DataSource property.

The standard ComboBox control can only display the contents of one property/column of the items in the list. If you want to show data from multiple columns then you have two choices:

1. Add a new column to the DataTable and populate it with the combination of the data from the other columns. That new column's name is then used as the DisplayMember.
2. Create a customised ComboBox control that supports display of multiple columns. There are various examples online, which you should be able to find by searching for "multi-column combobox .net".
 
Ok, so in my case what do I do.

I have
oracle_da.fill(oracle_ds,"Security")

So the results are in the named table Security, correct? So to say I want to assign esec_user_id to to the combo box, what do I do?
me.combobox1.displaymember = ?????
 
You do what I said. What did I say?
1. Set the DisplayMember to the name of the column that you want to display to the user.
That's what you do.
 
Nvm i figured it out. Next, when i select a item from the drop down, how do i set the other controls to the corresponding values
 
how do i set the other controls to the corresponding values
You bind them too, e.g.
VB.NET:
myTextBox.DataBindings.Add("Text", myDataTable, "ColumnName")
You do that at the same time as you bind the ComboBox. You then don't need any code when the user changes the selection because data-binding will take care of it.
 

Latest posts

Back
Top