Reputation: 199
This is C# program I have a problem in this line of code:
I input numbers greater that or equal 400=
it runs
I input numbers less that or equal 500=
it runs
I input numbers between 400 and 500 ex 401 to 499=
it runs
I empty input= erorr
I input letters= erorr
Take note that I run the code that wil only accept integer separately it runs and also the code that will not accept empty input it also runs
What do you think is wrong?
int parsedValue;
if (int.Parse(txtVacate.Text) <= 400 || int.Parse(txtVacate.Text) >= 500)
MessageBox.Show("Romms provided is not vacant or does not exist at all.");
else if (txtVacate.Text == " ")
MessageBox.Show("You provide empty");
else if (!int.TryParse(txtVacate.Text, out parsedValue))
MessageBox.Show("Please provide right info");
else
{
MySqlConnection connection = null;
string hostname = "localhost";
string database = "aparece_hoteldb";
string username = "root";
string password = "";
connection = new MySqlConnection("host=" + hostname +
";database=" + database +
";username=" + username +
";password=" + password + ";");
string table = "reservations";
string query = "DELETE FROM reservations WHERE RoomNumber = " + txtVacate.Text;
connection.Open();
MySqlDataAdapter da_res = null;
DataSet ds_res = null;
ds_res = new DataSet();
da_res = new MySqlDataAdapter(query, connection);
da_res.Fill(ds_res, table);
MessageBox.Show("Room" + " " + txtVacate.Text + " " + "is now Vacant please reload!");
dataGridView2.DataSource = ds_res.Tables[table];
this.Close();
Upvotes: 1
Views: 92
Reputation: 33853
You can simplify this a few steps.
First, check to make sure the input is not null, whitespaces, or empty.
If there is a value, parse it to make sure it is a numerical value.
if (!string.IsNullOrWhiteSpace(txtVacate.Text))
{
in parsedValue = 0;
bool isValid = int.TryParse(txtVacate.Text, out parsedValue));
if (isValid)
{
if (parsedValue <= 400 || parsedValue >= 500)
MessageBox.Show("Romms provided is not vacant or does not exist at all.");
else
{
// main stuff here
}
}
else
{
MessageBox.Show("Please provide right info");
}
else
{
MessageBox.Show("You provide empty");
}
While complex, this should properly handle all use cases
Upvotes: 1
Reputation: 68440
I'm afraid your logic is wrong. If txtVacate.Text
doesn't contain an integer, your code will blow up. This is the correct order for evaluating your different conditions.
int parsedValue = 0;
bool isValid = int.TryParse(txtVacate.Text, out parsedValue));
if (txtVacate.Text == " ") // Better: if (txtVacate.Text.Trim() == string.Empty)
{
MessageBox.Show("You provide empty");
}
else if (!isValid)
{
MessageBox.Show("Please provide right info");
}
else if (parsedValue <= 400 || parsedValue >= 500)
{
MessageBox.Show("Romms provided is not vacant or does not exist at all.");
}
else
{
...
}
Upvotes: 3
Reputation: 1562
Your first if statement uses
int.Parse(txtVacate.Text)
For empty or non-numeric (text) values, this will throw a FormatException, so your 'else if' cases will never be evaluated.
You should use TryParse, instead of Parse in your first 'if', or catch the FormatException and handle it accordingly.
Upvotes: 1