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
Solved

.net sql insert statement - how to avoid null values

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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