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();
        }
    }
8
  • 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) Commented Feb 20, 2017 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. Commented Feb 20, 2017 at 14:42
  • Use the debugger, look what CB_EDIT_OWNER.SelectedText returns Commented Feb 20, 2017 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. Commented Feb 20, 2017 at 14:43
  • Sorted it! Thanks @TimSchmelter - SelectedItem not selectedText Commented Feb 20, 2017 at 14:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If you want to allow people to type in their own values from items not in the combo box's list then you must use SelectedText.
how often are you going to want the user to enter their own values in a combobox?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.