C# Sql dynamic update
I am trying to incorporate a function in a contact list that i've made whereby when the user wishes to update someone's details they only have to fill in textbox controls or select from lists and then press 'ONE BUTTON'. what i want to happen when people press that button is for the program to update only the fields that contain values. So if there are 3 textboxes, for instance, and they are for address, name, email. if someone only needs to update address they fill in the address box and press 'UPDATE'. I would only want the addrress field updating but what happens is it updates everything, inlcuding the blank fields.
Now this shouldn't happen because i don't release their parameters UNLESS the controls contain values. ***Please someone tell me if i'm wrong in my method there***
Please look over the code and explain for me?
string updateup =
"UPDATE ContactList SET Address = '" +comboBoxDepartmentUpdate.Text+ "' " +
", FullName = '" +txtNameUpdate.Text+ "' "+
", DirectLine = '" +txtDirectUpdate.Text+ "' "+
", Mobile = '" +txtMobileUpdate.Text+ "' "+
"WHERE Address = '" +comboBoxDepartment.Text+ "' "+
"AND FullName = '" +comboBoxName.Text+ "'"
sqlInsertCommand1.CommandType = CommandType.Text;
sqlInsertCommand1.CommandText = updateup;
sqlInsertCommand1.Connection = conn3;
if(comboBoxDepartment.SelectedIndex >= 0)
sqlInsertCommand1.Parameters.Add("@Address", SqlDbType.Char, 255, "Address").Value = comboBoxDepartmentUpdate.Text;
if(txtNameUpdate.TextLength > 0)
sqlInsertCommand1.Parameters.Add("@FullName", SqlDbType.Char, 255, "FullName").Value = txtNameUpdate.Text;
if(txtDirectUpdate.TextLength > 0)
sqlInsertCommand1.Parameters.Add("@DirectLine", SqlDbType.Char, 255, "DirectLine").Value = txtDirectUpdate.Text;
if(txtMobileUpdate.TextLength > 0)
sqlInsertCommand1.Parameters.Add("@Mobile", SqlDbType.Char, 255, "Mobile").Value = txtMobileUpdate.Text;
The RunQuery is the function that executes the query via the SqlInsertCommand.
I hope i've explain properly. It seems that there's no way of contraining what's executed other than writing a sqlStatement for each case, which would be REALLY annoying if you had 30 controls that needed updating.
Any help would be much appreciated.
When **** becomes valuable the poor will be born without *******s.