Solved

.net sql insert statement - how to avoid null values

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

724 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