user4338480
user4338480

Reputation: 11

validating user input using Try/Parse?

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?

Upvotes: 0

Views: 2087

Answers (1)

fubo
fubo

Reputation: 46005

replace

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

with

if (String.IsNullOrEmpty(txtCustomerName.Text)) 

char represents ony one character

Upvotes: 3

Related Questions