using sql commands C#
Hi peeps. I realise this is a possible cross over of categories but i need help anyhow!
Ok i am becoming pretty clued in with the SQL statements now. So i don't talk loads of rubbish and confuse people firstly, here's my statement:
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+ "'";
Now the statement works fine. But:
1. Can someone please tell my why i DO NOT need parameters for this to work when updating?
2. What i want is for people to be able to press a button and it will update all the fields BUT ONLY IF they have entered something into them. So i tried this:
if(comboBoxDepartment.SelectedIndex >= 0)
cmdUpdate.Parameters.Add("@Address",SqlDbType.Char ,255,"Address").Value = comboBoxDepartmentUpdate.Text;
if(txtNameUpdate.TextLength > 0)
cmdUpdate.Parameters.Add("@FullName",SqlDbType.Cha r,255,"FullName").Value = txtNameUpdate.Text;
if(txtDirectUpdate.TextLength > 0)
cmdUpdate.Parameters.Add("@DirectLine",SqlDbType.C har,255,"DirectLine").Value = txtDirectUpdate.Text;
if(txtMobileUpdate.TextLength > 0)
cmdUpdate.Parameters.Add("@Mobile",SqlDbType.Char, 255,"Mobile").Value = txtMobileUpdate.Text;
i was hoping that this would only release the parameters if people had selected or entered some data.
However i found out that you don't actually need to define the parameters for some strange reason so the update statement just updates everything including BLANK fields!!!
now i know i could do switch-case but that would mean creating at least 14 different statements, i know there must be an easier way to say, if the field is blank then don't update???
Hopefully it's clear as to what i'm getting at please ask for more info if it's needed!
Thanks a lot
When **** becomes valuable the poor will be born without *******s.