View Single Post
  #1 (permalink)  
Old December 15th, 2006, 02:04 PM
N4th N4th is offline
Registered User
 
Join Date: Dec 2006
Location: london, , United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Nath

When **** becomes valuable the poor will be born without *******s.
Reply With Quote