Solved

.net sql insert statement - how to avoid null values

Posted on 2014-02-07
3
356 Views
Last Modified: 2014-02-10
I have an insert statement in my aspx.cs file.  I'm trying to avoid having a Null value in my record.  Not sure the best way to handle this when the user does not provide data in this text box?
I'm currently  just adding a space to every entry.  (I would prefer a space over "N/A".)
command.Parameters.Add("@MiddleName", SqlDbType.VarChar, 50).Value = 
TextBoxMiddleName.Text.Trim() + " ";

Open in new window

Thanks.
0
Comment
Question by:javierpdx
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39843240
This should do

command.Parameters.Add("@MiddleName", SqlDbType.VarChar, 50).Value =
TextBoxMiddleName.Text.Trim().Length == 0 ? "N/A" :  TextBoxMiddleName.Text.Trim();
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39843268
Another way would be to add a required field validator to the front end forcing the user to fill in something before submitting it to the db.
0
 

Author Closing Comment

by:javierpdx
ID: 39848096
This is a cleaner solution than what I would have used.  I appreciate it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

22 Experts available now in Live!

Get 1:1 Help Now