Subject: using sql commands C#
Posted By: N4th Post Date: 12/15/2006 1:04:01 PM
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.Char,255,"FullName").Value = txtNameUpdate.Text;
}

if(txtDirectUpdate.TextLength > 0)
{
cmdUpdate.Parameters.Add("@DirectLine",SqlDbType.Char,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.

Go to topic 53457

Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89
Return to index page 88
Return to index page 87
Return to index page 86
Return to index page 85
Return to index page 84
Return to index page 83