Hello!
Why when I try to save a null value from textbox at sql server 0 is saved?
The field type is number.
I use an insert query to do that
thanx in advance

Recommended Answers

As it is a number datatype a null value wont be stored in the column....it will store 0 as null value....

change it to text and then u can save a null value....

Jump to Post

All 3 Replies

You won't be able to insert an actual NULL/Nothing into the database.
You will need to use either the word " NULL " in your SQL or use DBNull.Value in your parameters (if you're using a parameterized query).

As it is a number datatype a null value wont be stored in the column....it will store 0 as null value....

change it to text and then u can save a null value....

commented: Good catch! +12

Try this :

Dim sql As String 
  If TextBox1.Text.Length <> 0 Then
            str = "'" & TextBox1.Text & "'"
        Else
            str = "NULL"
  End If

  sql = "insert into test(test1) values(" & str & ")"
  dp.save_sql(sql) 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.