Solved

.net sql insert statement - how to avoid null values

Posted on 2014-02-07
3
355 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 39

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

18 Experts available now in Live!

Get 1:1 Help Now