Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
when I input data to sql server there is something that is usually filled in and there are those that don't, when there is empty data I want to set 'NULL'.

C#
string query = "UPDATE MASTER_DOSEN SET NIP = '" + NIP
                + "', NIDN = '" + NIDN
                + "', GELAR_DEPAN = '" + GELAR_DEPAN
                + "', NAMA = '" + NAMA
                + "', GELAR_BELAKANG = '" + GELAR_BELAKANG
                + "', HOMEBASE = '" + HOMEBASE
                + "', KDPST = '" + KDPST
                + "', TMP_LAHIR = '" + TMP_LAHIR
                + "', TGL_LAHIR = '" + TGL_LAHIR
                + "', DOMISILI = '" + DOMISILI
                + "', ALAMAT = '" + ALAMAT
                + "', HANDPHONE = '" + HANDPHONE
                + "', EMAIL = '" + EMAIL + "' WHERE ID = '" + lblID_Dosen.Text.Trim() + "'";


What I have tried:

I have tried it

C#
if (NIP == "")
            {
                NIP = "NULL";
            }
            if (NIDN == "")
            {
                NIDN = "NULL";
            }
            if (GELAR_DEPAN == "")
            {
                GELAR_DEPAN = "NULL";
            }
            if (KDPST == "")
            {
                KDPST = "NULL";
            }
            if (TMP_LAHIR == "")
            {
                TMP_LAHIR = "NULL";
            }
            if (DOMISILI == "")
            {
                DOMISILI = "NULL";
            }

            clsSQLServer db = new clsSQLServer("SIDANG");
            string query = "UPDATE MASTER_DOSEN SET NIP = '" + NIP
                + "', NIDN = '" + NIDN
                + "', GELAR_DEPAN = '" + GELAR_DEPAN
                + "', NAMA = '" + NAMA
                + "', GELAR_BELAKANG = '" + GELAR_BELAKANG
                + "', HOMEBASE = '" + HOMEBASE
                + "', KDPST = '" + KDPST
                + "', TMP_LAHIR = '" + TMP_LAHIR
                + "', TGL_LAHIR = '" + TGL_LAHIR
                + "', DOMISILI = '" + DOMISILI
                + "', ALAMAT = '" + ALAMAT
                + "', HANDPHONE = '" + HANDPHONE
                + "', EMAIL = '" + EMAIL + "' WHERE ID = '" + lblID_Dosen.Text.Trim() + "'";
            int rec = db.SQLCommand(query);


when i see in database like this
NIP = 'NULL'
NIDN = 'NULL'

ETC
Posted
Updated 28-Jan-19 1:11am
Comments
Afzaal Ahmad Zeeshan 8-Nov-18 4:22am    
Isn't no data the null? :-/ Just make sure your table and the columns are able to accept the NULL values, if the program throws an exception on saving NULL.

Why tell the platform to insert a nothing, when nothing is already being inserted?

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

As an extra bonus, it'll fix your problem as well...
 
Share this answer
 
TMP_LAHIR = "NULL"
There's a world of difference between "NULL" and NULL.

The first one is a sting of characters, just like "OOPS". The second one is a defined value in, for example, SQL Server.

The way you implement the query statement, you wrap the value in single quotes whether it's real or NULL, so you have sent the server a string instead of NULL


You need to process it so that you have a value "pre-wrapped" in single quotes if it's not NULL and without the single quotes if it is. I actually use a function to do this (php). It would look something like in a generic code format.
val = (val=='')?"NULL":"'val'";
Now, when plugged in to your query, only append the val and do not wrap in single quotes.
 
Share this answer
 
v2
C#
string query = "UPDATE MASTER_DOSEN SET NIP = '" + NIP
                + "', NIDN = '" + NIDN
                + "', GELAR_DEPAN = '" + GELAR_DEPAN
                + "', NAMA = '" + NAMA
                + "', GELAR_BELAKANG = '" + GELAR_BELAKANG
                + "', HOMEBASE = '" + HOMEBASE
                + "', KDPST = '" + KDPST
                + "', TMP_LAHIR = '" + TMP_LAHIR
                + "', TGL_LAHIR = '" + TGL_LAHIR
                + "', DOMISILI = '" + DOMISILI
                + "', ALAMAT = '" + ALAMAT
                + "', HANDPHONE = '" + HANDPHONE
                + "', EMAIL = '" + EMAIL + "' WHERE ID = '" + lblID_Dosen.Text.Trim() + "'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Check null value for variable; if null then use DbNull.Value to insert null value in db
 
Share this answer
 
use nullif


string query = "UPDATE MASTER_DOSEN SET NIP = nullif('" + NIP + "', ''), 
                    NIDN = nullif('" + NIDN+ "', ''),  
                    GELAR_DEPAN = '" + GELAR_DEPAN
                + "', NAMA = '" + NAMA
                + "', GELAR_BELAKANG = '" + GELAR_BELAKANG
                + "', HOMEBASE = '" + HOMEBASE
                + "', KDPST = '" + KDPST
                + "', TMP_LAHIR = '" + TMP_LAHIR
                + "', TGL_LAHIR = '" + TGL_LAHIR
                + "', DOMISILI = '" + DOMISILI
                + "', ALAMAT = '" + ALAMAT
                + "', HANDPHONE = '" + HANDPHONE
                + "', EMAIL = '" + EMAIL + "' WHERE ID = '" + lblID_Dosen.Text.Trim() + "'";
 
Share this answer
 
Comments
Richard Deeming 28-Jan-19 10:45am    
Try reading the other solutions. You've copied an extremely serious security vulnerability from the question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900