1

this doesn't update in my database table. Have I over-looked something?

The values are in the textboxes fine. No errors show, weird.

          using (SqlConnection connection = new SqlConnection(@"Data Source = UKMAN1NB10038\SQLEXPRESS; Initial Catalog = TheVets; Integrated Security = True"))
        {
            SqlCommand command = new SqlCommand("UPDATE OwnerTable SET Owner_Fname =@OwnerFname , Owner_Lname = @OwnerLname, Owner_HouseNo = @OwnerHouse, Owner_Street = @OwnerStreet, Owner_County = @OwnerCounty, Owner_PostCode = @OwnerPost, Owner_Tele = @OwnerTele, Owner_Email = @OwnerEmail WHERE Owner_ID = '" + CB_EDIT_OWNER.SelectedText + "'", connection);

            command.CommandType = CommandType.Text;
            command.Connection = connection;



            command.Parameters.AddWithValue("@OwnerFname", TXT_EDIT_FNAME.Text);
            command.Parameters.AddWithValue("@OwnerLname", TXT_EDIT_LNAME.Text);
            command.Parameters.AddWithValue("@OwnerHouse", TXT_EDIT_HOUSE.Text);
            command.Parameters.AddWithValue("@OwnerStreet", TXT_EDIT_STREET.Text);
            command.Parameters.AddWithValue("@OwnerCounty", TXT_EDIT_COUNTY.Text);
            command.Parameters.AddWithValue("@OwnerPost", TXT_EDIT_POSTCODE.Text);
            command.Parameters.AddWithValue("@OwnerTele", TXT_EDIT_TELE.Text);
            command.Parameters.AddWithValue("@OwnerEmail", TXT_EDIT_EMAIL.Text);

            connection.Open();
            command.ExecuteNonQuery();

            connection.Close();
        }
    }
elszeus
  • 131
  • 1
  • 2
  • 13
  • 3
    Why do you use parameters for all of your things, but then you use string adding for `CB_EDIT_OWNER.SelectedText`? why is that not a parameter too? (It is very possible that owner_id not being a parameter is the source of your problem) – Scott Chamberlain Feb 20 '17 at 14:41
  • 1
    Probably the ID... check its runtime value. And consider adding it as a parameter too, it might not be safe for scripting like this. `command` should also be wrapped in using() { }, like the connection. – Cee McSharpface Feb 20 '17 at 14:42
  • Use the debugger, look what `CB_EDIT_OWNER.SelectedText` returns – Tim Schmelter Feb 20 '17 at 14:42
  • @dlatikay command really doesn't. The only reason SqlCommand is disposable is becuse it inherits from `System.ComponentModel.Component`. If nothing is subscribing to it's `Disposed` event there is no need to dispose it. It falls in to the same category as `DataTable` or `Task`, both of those are disposable too but you don't need to dispose of them either normally. – Scott Chamberlain Feb 20 '17 at 14:43
  • Sorted it! Thanks @TimSchmelter - SelectedItem not selectedText – elszeus Feb 20 '17 at 14:45
  • @elszeus if it was a simple bug on your part you can delete your question or post your own answer and accept it. – Scott Chamberlain Feb 20 '17 at 14:46
  • @ScottChamberlain in this case I have written literally thousands of lines of code in vain, and ill-advised many of my poor fellow developers. Is it not recommendable to always stick to the `IDisposable` > using() pattern because we never know if future implementations may start to need it? As for example the ODP.NET implementation of DbCommand does. Also, there is http://stackoverflow.com/a/16985968/1132334 – Cee McSharpface Feb 20 '17 at 14:46
  • Okay, chill out :) – elszeus Feb 20 '17 at 14:48

1 Answers1

3

You need to use SelectedItem not SelectedText on the combobox

Replace CB_EDIT_OWNER.SelectedText with:

CB_EDIT_OWNER.SelectedItem

Then this should work.

Andrew
  • 1,544
  • 1
  • 18
  • 36
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53