Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writng NULL value to Sql Database if text box is empty

Posted on 2008-10-18
4
Medium Priority
?
347 Views
Last Modified: 2010-05-18
I have text boxes on my form, if the text box for "Middle Name" isn't populated when I use an INSERT command I want the system "NULL" value written to the column in the table in my DB. See attached code I'm using so far.

Thanks for your help
Dim strSQL As String = "INSERT INTO Contact (FirstName, LastName, MiddleName) VALUES (@First, @Last, @MiddleName)"
 
        Dim dbComm As New SqlClient.SqlCommand(strSQL, dbConnection)
        dbComm.Parameters.Add("@First", SqlDbType.VarChar)
        dbComm.Parameters.Add("@Last", SqlDbType.VarChar)
        dbComm.Parameters.Add("@MiddleName", SqlDbType.VarChar)
 
        dbComm.Parameters("@First").Value = txtFirstName.Text
        dbComm.Parameters("@Last").Value = txtLastName.Text
        dbComm.Parameters("@MiddleName").Value = txtMiddleName.Text
                
        dbComm.ExecuteNonQuery()

Open in new window

0
Comment
Question by:rfgraham
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22750013
this should do:

dbComm.Parameters("@MiddleName").Value = iif(txtMiddleName.Text = "", DBNull.Value, txtMiddleName.Text)

Open in new window

0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22750018
Try this....

    dbComm.Parameters("@MiddleName").Value = Iif(txtMiddleName.Text = "", "NULL", txtMiddleName.Text)

Wayne
0
 

Author Closing Comment

by:rfgraham
ID: 31507482
Super, thanks
0
 
LVL 13

Expert Comment

by:sm394
ID: 22750104
if(txtMiddleName.Text.Equals("") then
 dbComm.Parameters.Add("@MiddleName", SqlDbType.NVarChar, 255).Value = DBNull.Value
endif
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question