Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert NULL into database if form field is empty

Tags:

asp.net

vb.net

I have a form and stored procedure that inserts the data from the form. It works fine except that if a field isn't filled in it doesn't insert a NULL into SQL it inserts "".

I've tried a few different ways but none seem to insert NULL, the one below still inserts "", can anyone point me in the right direction?

Here is the required part of the code, if you require more just let me know.

Dim rdr As SqlDataReader
            Dim cmdInsert As SqlCommand = New SqlCommand()
            cmdInsert.CommandText = "spPersonalDetailsInsert"
            cmdInsert.CommandType = CommandType.StoredProcedure
            cmdInsert.Connection = connSQL


            Dim firstname, lastname, address, address1, town, county, postcode As SqlParameter
            'convert to null if ""
            Dim frmFirstName As String
            If pd_first_name.Text = "" Then
                frmFirstName = Convert.DBNull
            Else
                frmFirstName = pd_first_name.Text
            End If

            firstname = New SqlParameter()
            firstname.ParameterName = "@firstname"
            firstname.SqlDbType = SqlDbType.NVarChar
            firstname.Size = 50
            firstname.Direction = ParameterDirection.Input
            firstname.Value = frmFirstName

EDIT

I tested the following code:

If pd_first_name.Text = "" Then
            frmFirstName = DBNull.Value
        Else
            frmFirstName = pd_first_name.Text
        End If

But it still doesn't insert NULL so I tested this:

            If pd_first_name.Text = "" Then
                Response.Write("NULL")
                address1.Value = DBNull.Value
            Else
                Response.Write("NOT NULL")
                address1.Value = pd_address1.Text
            End If

So if I enter nothing into address1 field it should write NULL to screen but it always writes NOT NULL. What does an empty form field equal? in classic ASP it was always "".

like image 435
Jammer Avatar asked Jan 18 '11 09:01

Jammer


People also ask

How insert NULL instead of empty string?

For example to insert NULL instead of an empty string using NULLIF() , we could do the following: INSERT INTO `user` (`name`, `job_title`) VALUES ('john', NULLIF('$jobTitle', '')); This would insert NULL in the job_title column when the value of the variable " $jobTitle " matches an empty string.

How do I replace an empty column with NULL in SQL?

There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.

How do you insert a NULL into a database?

You also can specify the NULL keyword in the VALUES clause to indicate that a column should be assigned a NULL value. The following example inserts values into three columns of the orders table: INSERT INTO orders (orders_num, order_date, customer_num) VALUES (0, NULL, 123);

How do you insert an empty value in a NOT NULL column?

To understand the above syntax, let us create a column with NOT NULL constraint while you can insert an empty string. SET @anyVariableName=” ”; UPDATE yourTableName SET yourColumnName= @anyVariableName; Implement the above syntax for the given table.


1 Answers

You need to use DBNull.Value

            If String.IsNullOrEmpty(pd_first_name.Text.ToString().Trim) = true Then
                frmFirstName = DBNull.Value
            Else
                frmFirstName = pd_first_name.Text
            End If
like image 86
codingbadger Avatar answered Oct 11 '22 22:10

codingbadger