user225269
user225269

Reputation: 10913

Problem updating values in combobox in vb.net

I have this code, but I have a problem. When I update but do not really made any changes to the value and press the update button, the data becomes null. And it will seem that I deleted the value.

I've taught of a solution, that is to add both combobox1.selectedtext and combobox1.selecteditem to the function. But it doesn't work.

combobox1.selecteditem is working when you try to alter the values when you update. But will save a null value when you don't alter the values using the combobox

combobox1.selectedtext will save the data into the database even without altering. But will not save the data if you try to alter it.

-And I incorporated both of them, but still only one is performing, and I think it is the one that I added first:

Dim shikai As New Updater





Try

    shikai.id = TextBox1.Text
    shikai.fname = TextBox2.Text
    shikai.mi = TextBox3.Text
    shikai.lname = TextBox4.Text

    shikai.ad = TextBox5.Text
    shikai.contact = TextBox9.Text

    shikai.year = ComboBox1.SelectedText
    shikai.section = ComboBox2.SelectedText
    shikai.gender = ComboBox3.SelectedText
    shikai.religion = ComboBox4.SelectedText

    shikai.year = ComboBox1.SelectedItem
    shikai.section = ComboBox2.SelectedItem
    shikai.gender = ComboBox3.SelectedItem
    shikai.religion = ComboBox4.SelectedItem


    shikai.bday = TextBox6.Text



    shikai.updates()
    MsgBox("Successfully updated!")

Please help, what would be a simple workaround to solve this problem?

Upvotes: 0

Views: 1465

Answers (1)

tobrien
tobrien

Reputation: 618

a few things to remember ---

  1. a 'selected____' anything is only non-null when something is, uhm, SELECTED. To ensure that SOMETHING is selected even at start add a line like: ComboBox1.SelectedIndex = 0.

  2. If your recordset has non-string types (like a DATE field might be) then be sure to first check then coerce the string coming back as TEXT to the correct type. I.e....

if isDate(ComboBox1.SelectedText) then ... 'its ok to use this coerced text.

  1. Since a combobox (as well as a listbox) can hold an entire CLASS (i.e. any kind of OBJECT) ... any SelectedItem assignment had better match EXACTLY to the type that was .Items.Add 'ed originally to the control.

Upvotes: 1

Related Questions