Search code examples
databasevb.netcomboboxinsertvaluemember

combo box .ValueMember not working for me


Calling database value in combo box for DisplayMember and ValueMember , but ValueMember property not working here .......

        sql = "select NAME_DESC,ACCOUNT_CODE from ACCOUNT_CONTROLS ORDER by 
        NAME_DESC"


        cmd = New OracleCommand(sql, sgcnn)
        adapter.SelectCommand = cmd
        adapter.Fill(ds)
        adapter.Dispose()
        cmd.Dispose()


        CmbConAcc.DataSource = ds.Tables(0)
        CmbConAcc.DisplayMember = "NAME_DESC"
        CmbConAcc.ValueMember = "ACCOUNT_CODE"

   

--- trying to insert ValueMember data into database

Dim cmd As OracleCommand = New OracleCommand()

    cmd.CommandText = "INSERT INTO ACCOUNT_HEADS (HEAD_CODE,NAME_DESC,ACCOUNT_CODE,REMARKS)" &
                          "values( trim(" & TxtAccCode.Text & "),trim(' " & TxtNDesc.Text & " '),trim(' " & CmbConAcc.ValueMember & " '),trim(' " &
                          txtRemarks.Text & " ')) "

Solution

  • If you set the ValueMember here:

    CmbConAcc.ValueMember = "ACCOUNT_CODE"
    

    why would you expect to get anything other than that out?

    The ValueMember is the name of a property/column of the data source. When the user makes a selection, the value from that property/column of the SelectedItem is exposed by the SelectedValue property. That's what you need to use.

    That's what the code example in the documentation for the ValueMember property demonstrates and that's why you should ALWAYS read the documentation. VS has a Help menu for a reason.