Reputation: 27
I am trying to make a winforms application so when something is clicked it checks a webpage for it's response.
I have tested the web page to see if it is a PHP error but it works fine from that side.
It is completely ignoring the else if statement and skips to the else statement below it even though the response is "Unassigned".
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://fms.psrpc.co.uk/apiconfirmD.php?" + ApiKey);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (response)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
if (reader.ReadToEnd() == "Changed")
{
label2.Visible = false;
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
timer1.Enabled = true;
}
else if (reader.ReadToEnd() == "Unassigned")
{
string message = "Error Code: B66794O37945O46791K@@Error booking on.@Please make sure you have been assigned to a vehicle.@@If this error persists please contact K.McCrudden.";
message = message.Replace("@", "" + System.Environment.NewLine);
string title = "Error!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
}
else
{
string message = "Error Code: B002875O46883O84655K@@Error booking on.@Please make sure you have booked a shift and have been assigned.@@If this error persists please contact K.McCrudden.";
message = message.Replace("@", "" + System.Environment.NewLine);
string title = "Error!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
}
}
}
Upvotes: 1
Views: 1621
Reputation: 16389
No, it is not being ignored. You are reading all the data in your first if
block by calling reader.ReadToEnd()
. This way, there is no further data available to read in your else if
statement; it returns empty string. Thus condition does not match and final else
block gets executed.
Modify the code something like below. Notice the temporary data
variable in below code.
StreamReader reader = new StreamReader(response.GetResponseStream());
string data = reader.ReadToEnd();//Read the data in temp variable.
//Use this variable to check the conditions further.
if (data == "Changed")
{
//Your code here
}
else if (data == "Unassigned")
{
//Your code here
}
else
{
//Your code here
}
Upvotes: 4
Reputation: 14432
You have an error in your general logic. When you enter the first if statement, you're reading to the end of the stream with the code reader.ReadToEnd()
. In the next statement (the else
), you're reading the stream again, but it has already been read, so it will return an empty string, thus the last else statement will effectively be hit.
You can also read about this on MSDN: StreamReader.ReadToEnd() Method.
Definition of the return value:
The rest of the stream as a string, from the current position to the end. If the current position is at the end of the stream, returns an empty string ("").
Your code should look like this:
StreamReader reader = new StreamReader(response.GetResponseStream());
var result = reader.ReadToEnd();
if(result == "Changed")
{
label2.Visible = false;
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
timer1.Enabled = true;
}
else if(result == "Unassigned")
{
string message = "Error Code: B66794O37945O46791K@@Error booking on.@Please make sure you have been assigned to a vehicle.@@If this error persists please contact K.McCrudden.";
message = message.Replace("@", "" + System.Environment.NewLine);
string title = "Error!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
}
else
{
string message = "Error Code: B002875O46883O84655K@@Error booking on.@Please make sure you have booked a shift and have been assigned.@@If this error persists please contact K.McCrudden.";
message = message.Replace("@", "" + System.Environment.NewLine);
string title = "Error!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
}
Upvotes: 2