Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Writng NULL value to Sql Database if text box is empty

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
rfgraham
Asked:
rfgraham
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:

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

Open in new window

0
 
Wayne Taylor (webtubbs)Commented:
Try this....

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

Wayne
0
 
rfgrahamAuthor Commented:
Super, thanks
0
 
sm394Commented:
if(txtMiddleName.Text.Equals("") then
 dbComm.Parameters.Add("@MiddleName", SqlDbType.NVarChar, 255).Value = DBNull.Value
endif
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now