I want to create an SQL statement that inserts a value taken from a textBox into a column where any value in that column is NULL
I’m doing it in C# and I was wondering if anyone could help me out…
I wrote a pseudo code version of the command:
string newPhoneNumber = textBox.Text;
SqlCommand cmd = new SqlCommand(
"INSERT INTO table ([Tag ID])
VALUES ('" + newPhoneNumber + "')";
WHERE columnName = NULL"
cmd.ExecuteNonQuery();
You cannot Use
Insert QuerywithWhereClauseSince your values are already there, even if you write
ColumnName IS NUllas other answers have said, it will not work.You have to use
Update queryMoreover
NULLis not a value. Its a state. Which means (in front-end) that memory has not been assigned.For e.g. strings in C# – if memory is not assigned they are null. They do not have null.
Note/Warning related to question, not related to answer:
Always be aware of SQL Injection. Use Parameterised Query (the one with @).
More on SQL Injection