0
if (!char.TryParse(txtCustomerName.Text, out charCustomerName))  //try to read in Layers from TextBox
{
    MessageBox.Show("Customer Name must be entered.", "Invalid Customer Name");
    return;
}

if (!Decimal.TryParse(txtAmountOwed.Text, out decAmountOwed))  //try to read in Layers from TextBox
{
    MessageBox.Show("Amount Owed must be entered without dollar signs or commas.", "Invalid Amount Owed");
    return;
}

if (!int.TryParse(txtDaysOverdue.Text, out intDaysOverdue))  //try to read in Layers from TextBox
{
    MessageBox.Show("Days Overdue must be entered as a whole number.", "Invalid Days Overdue");
    return;
}

When I run the program, it tells me the customer name must be entered even though I entered it. How do I change that?

3
  • 2
    Just curious, why are you trying to use char.TryParse? That will try to parse the String (text) into a char type, which is a single character. Commented Feb 6, 2015 at 14:54
  • You are trying to parse a single character out of a string of characters. Don't you really need just !string.IsNullOrEmpty(txtCustomerName.Text)? Commented Feb 6, 2015 at 14:55
  • Andrei, IsNullOrWhiteSpace is better Commented Feb 6, 2015 at 14:58

1 Answer 1

3

replace

if (!char.TryParse(txtCustomerName.Text, out charCustomerName)) 

with

if (String.IsNullOrEmpty(txtCustomerName.Text)) 

char represents ony one character

Sign up to request clarification or add additional context in comments.

5 Comments

Use IsNullOrWhiteSpace
@L33TS - probably a space is a valid username in his case - he didn't define it in his question
Why is an empty string not valid then? He never specified empty strings weren't valid either, but generally white space or empty strings are never valid for names.
String.IsNullOrEmpty means: nothing was entered. i just want to say that the validation of the username is another issue
White-spaces are nothing either." ", "\t" etc. are all white space characters which shouldn't be valid for a customer name (not a username like you specified.) On top of that IsNullOrWhiteSpace also checks for empty strings. It's generally safer to use. There was a reason IsNullOrWhiteSpace was introduced; simply because usually when you work with empty strings you don't work with white spaces either.

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.