1

I use LINQ to Access DB in my Asp.net page.

I have a form with some textbox and a submit button before user see the form all of the textbox be empty with this code:

txtPName.Text =null;
txtPFamily.Text = null;
txtPUsername.Text = null;
txtPPassword.Text = null;

and after click button the values insert in Databse, but if a textbox was empty I want null value insert in database but insert empty in database:

 Users u = new Users()
 {
      FirstName = txtPName.Text,
      LastName = txtPFamily.Text,
      Username = txtPUsername.Text,
      Password = txtPPassword.Text
 };
1
  • Just wondering, why do you want it to be null? For me, null means it has never been set, "" means it has been set but left blank intentionally. Commented Sep 4, 2013 at 7:50

3 Answers 3

7

You can use the conditional operator. Since Text properties never return null you can safely check if their Length is 0 (text is empty):

Users u = new Users()
{
    FirstName = txtPName.Text.Length == 0 ? null : txtPName.Text,
    LastName = txtPFamily.Text.Length == 0 ? null : txtPFamily.Text,
    Username = txtPUsername.Text.Length == 0 ? null : txtPUsername.Text,
    Password = txtPPassword.Text.Length == 0 ? null : txtPPassword.Text
};

If you also want to treat white-spaces as "empty", use String.IsNullOrWhiteSpace, e.g.:

FirstName = String.IsNullOrWhiteSpace(txtPName.Text) ? null : txtPName.Text
Sign up to request clarification or add additional context in comments.

5 Comments

string.IsNullOrWhiteSpace() could be better as it would null whitespace entries.
@Oliver: I have already included string.IsNullOrWhiteSpace in my answer.
The check added for Whitespaces is fantastic.This helped me avoid posting one question of mine :) . Upvoted.
Assumption is the mother of all.... ;) It feels so unnatural to assume text is not null. Imo the second approach is way better practice.
You can always use string.IsNullOrEmpty or string.IsNullOrWhiteSpace instead. However, it is worth nothing(especially because OP tries to set it to null explitely) that in ASP.NET a text is never null. The pattern is always that string.Empty is returned instead then. This is the shortened source(ILSpy, TextBox.Text-get): if (text != null) { return text; } return string.Empty;
0

You can use the string.IsNullOrEmpty or string.IsNullOrWhiteSpace methods to check your TextBox value.

//This will set a string of your Textbox to either a value (if it has one) or null

string yourValueToPutIntoDatabase = (string.IsNullOrEmpty(yourTextBox.Text)) ? yourTextBox.Text : null;

You could also use it in an if-statement if you are more comfortable :

string valueToPutInDatabase;
if(string.IsNullOrEmpty(yourTextBox.Text))
{
 //Your textbox was empty
 valueToPutInDatabase = null;
}
else
{
 valueToPutInDatabase = yourTextBox.Text;
}

Try this out..

Comments

0

try this

if(txtPName.Text==""){
u.FirstName =null;
}
if(txtPFamily.Text==""){
u.LastName =null;
}
if(txtPUsername.Text==""){
u.Username =null;
}
if(txtPPassword.Text==""){
u.Password =null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.