Solved

Writng NULL value to Sql Database if text box is empty

Posted on 2008-10-18
4
339 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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)
Comment Utility
Try this....

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

Wayne
0
 

Author Closing Comment

by:rfgraham
Comment Utility
Super, thanks
0
 
LVL 13

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 41
Problem to go to Web page 2 46
Trouble connecting to SqlServer database 4 29
Get String split 5 31
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now