Solved

Writng NULL value to Sql Database if text box is empty

Posted on 2008-10-18
4
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

Accepted Solution

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

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

Open in new window

0
 
LVL 47

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

615 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