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);
}
}
}
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
}
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);
}
Related
I need help with this authentication form.
This is the xml file:
<USER_LIST>
<user>
<name>giulio</name>
<password>prova</password>
</user>
<user>
<name>giulia</name>
<password>prova1</password>
</user>
<user>
<name>renato</name>
<password>zero</password>
</user>
</USER_LIST>
and this is the code i wrote:
private void button4_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("dati.txt");
foreach (XmlNode node in doc.SelectNodes("//user"))
{
String User = node.SelectSingleNode("name").InnerText;
String Pass = node.SelectSingleNode("password").InnerText;
if (User == textBox1.Text && Pass == textBox2.Text)
{
button1.Visible = true;
dlt_btn.Visible = true;
button3.Visible = true;
button3.Visible = true;
button5.Visible = true;
return;
}
else
{
MessageBox.Show("Invalid Username or Password!");
}
}
}
But like this, for example, if i login with the name "renato" and password "zero"
it give me back two times the message box "Invalid Username or Password!" and the third time it show the button needed. I know why but i can't think another way to do it. This is my very first project and i started coding like yesterday, so i'm sorry if ask you a stupid thing like this.
Thank you for your help in advance!
I suppose it's just for learning purpose or an assignment which should be kept as simple, otherwise it's not secure at all.
You don't need to use a loop. Currently your loop loop checks all nodes one by one and for each node which doesn't match with given user/pass, shows the message box, that's why you see the message box until the loop reaches to the correct user/pass.
Without using a loop, you can check if the given user/pass exists in your xml file easily this way:
var userName = userNameTextBox.Text;
var password = passwordTextBox.Text;
var match = System.Xml.Linq.XElement.Load(#"d:\users.xml")
.Elements("user")
.Where(x => x.Element("name")?.Value == userName &&
x.Element("password")?.Value == password)
.Any();
Then if the match is not true, you can show message box.
The issue is that the message box will display each time you check an entry in the XML that doesn't match.
The simplest way to do this with minimal changes to your code is like this:
private void button4_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("dati.txt");
bool found = false;
foreach (XmlNode node in doc.SelectNodes("//user"))
{
String User = node.SelectSingleNode("name").InnerText;
String Pass = node.SelectSingleNode("password").InnerText;
if (User == textBox1.Text && Pass == textBox2.Text)
{
found = true;
break;
}
}
if (found)
{
button1.Visible = true;
dlt_btn.Visible = true;
button3.Visible = true;
button3.Visible = true;
button5.Visible = true;
}
else
{
MessageBox.Show("Invalid Username or Password!");
}
}
We create a variable called found and set it to false. This is to ensure that if the XML is empty or there are no matches that we fail the check.
Then we loop over the results and we set found = true if a match is found. We call break to break out of the loop.
Once the loop is complete we check to see if our local variable is true:
if (found)
This is shorthand for if (found == true)
If it's true then we enable your buttons as before. If it's not true then we display the error message.
It will only display the error message once.
this is a solution, that works:
private void button4_Click(object sender, EventArgs e)
{
string username;
string password;
string CurrentUser = "";
string CurrentPwd = "";
bool LoginStatus = false;
username = textBox1.Text;
password = textBox2.Text;
XmlDocument xmxdoc = new XmlDocument();
xmxdoc.Load("dati.txt");
XmlNodeList xmlnodelist = xmxdoc.GetElementsByTagName("user");
foreach (XmlNode xn in xmlnodelist)
{
XmlNodeList xmlnl = xn.ChildNodes;
foreach (XmlNode xmln in xmlnl)
{
if (xmln.Name == "name")
{
if (xmln.InnerText == username)
{
CurrentUser = username;
}
}
if (xmln.Name == "password")
{
if (xmln.InnerText == password)
{
CurrentPwd = password;
}
}
}
if ((CurrentUser != "") & (CurrentPwd != ""))
{
LoginStatus = true;
}
}
if (LoginStatus == true)
{
button1.Visible = true;
dlt_btn.Visible = true;
button3.Visible = true;
button3.Visible = true;
button5.Visible = true;
return;
}
else
{
MessageBox.Show("Invalid Username or Password!");
}
}
i want to implement a transaction in my code which is separated by functions.
There is one function in which all other functions are called. I want to make this function execute the function calls in a transaction (All or None).
Some of the function calls are queries while some are routine code for C# and Crystal report.
Can Some one help with this?
This function is working perfectly.
But if 2 users are accessing same database and executing this function at same time may get Cuncurrency problem.
the function code is attached.
public bool Generate_PDF(decimal wo_id)
{
if (Fill_WO_Report_Table(wo_id) == false) // this function has queries
return false;
try
{
string exception;
cryRpt = new ReportDocument();
cryRpt.Load(Application.StartupPath + "\\CrRpt_WO_Report.rpt");
DB_Interface.CrystalReport_Login(ref cryRpt, out exception);
string Temp_file_path = #"c:\Reports\WO_Temp.pdf";
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Temp_file_path);
string File_name = str_WO_File_Name + ".pdf";
// option to print at which location
//DialogResult dlg = MessageBox.Show("Select Option For Print :\n\nYes - Print To Default Path\nNo - Print To Custom Path\nCancel - Do Not Print",
// "Print GRN", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
SaveToDialog frm = new SaveToDialog(ref File_name);
DialogResult dlg = frm.ShowDialog();
File_name = frm.GetString();
frm.Dispose();
frm = null;
if (dlg == DialogResult.Yes)
{
if (Convert.ToString(MMS_Vars.PathReport_WO) == "")
throw new Exception("Default Path Not Available.");
string Full_File_name = Convert.ToString(MMS_Vars.PathReport_WO) + "\\" + File_name;
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Full_File_name);
System.Diagnostics.Process.Start(Full_File_name);
}
else if (dlg == DialogResult.No)
{
SaveFileDialog obj_saveFileDialog = new SaveFileDialog();
obj_saveFileDialog.InitialDirectory = Convert.ToString(MMS_Vars.PathReport_WO);
//obj_saveFileDialog.RestoreDirectory = true;
obj_saveFileDialog.FileName = File_name; //set file name to
obj_saveFileDialog.Filter = "PDF Files|*.pdf";
DialogResult result = obj_saveFileDialog.ShowDialog();
if (result == DialogResult.OK && obj_saveFileDialog.FileName != "")
{
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, obj_saveFileDialog.FileName);
System.Diagnostics.Process.Start(obj_saveFileDialog.FileName);
}
else if (result == DialogResult.Cancel)
{
obj_saveFileDialog.Dispose();
cryRpt.Close();
cryRpt.Dispose();
return false;
}
obj_saveFileDialog.Dispose();
}
else if (dlg == DialogResult.Cancel)
{
cryRpt.Close();
cryRpt.Dispose();
return false;
}
cryRpt.Close();
cryRpt.Dispose();
// Drop Report tables
if (Drop_WO_Report_Table() == false) // this function has queries
return false;
return true;
}
catch (Exception exe)
{
MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
You haven't posted any of the functions you do on your db. Are there any writes, etc? From your posted code, adding a transaction will probably not solve your problem. A simple solution can be to add a lock around the function so only one pdf is created at a time:
private static object locker = new object();
public bool Generate_PDF(decimal wo_id)
{
lock(locker){
if (Fill_WO_Report_Table(wo_id) == false) // this function has queries
return false;
........
........
return true;
}
catch (Exception exe)
{
MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}//END LOCK
}
In Form1.Load, I have:
MessageBox.Show("Text blah blah blah");
Until you press OK, this is the icon in the Taskbar:
When you press OK, it changes to the icon:
How do I make it update when it starts?
I changed the icon by changing it in the form properties:
Entire "Load" function:
string word = "1.4";
var url = "http://chipperyman573.com/rtf/textbot.html";
var client = new WebClient();
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
string downloadedString;
while ((downloadedString = reader.ReadLine()) != null)
{
if (downloadedString == word)
{
update = false;
MessageBox.Show("Congrats! You are running the latest version (" + word + ") of Chip Bot!\n\nGot an idea for this program? Use the \"Send feedback\" button to let me know!", "Chip Bot", MessageBoxButtons.OK, MessageBoxIcon.Information);
Text = "Chip Bot" + word + " - Got an idea for this program? Send me some feedback!";
}
else
{
Text = "Chip Bot (UPDATE AVAILABLE)";
go.ForeColor = Color.Gray;
setup.Enabled = false;
otherGroup.Enabled = false;
optionsGroup.Enabled = false;
MessageBox.Show("There is an update! Downloading now! \n\nUNTIL YOU UPDATE THE PROGRAM WILL NOT FUNCTION.", "Chip Bot", MessageBoxButtons.OK, MessageBoxIcon.Information);
url = "";
var web = new WebBrowser();
web.Navigate(url);
}
}
}
It does it regardless of if there is an update (downloadstring != word) or if there isn't one (downloadstring == word)
Try using the Shown event instead, and I fixed your code with the wrong use of the WebClient (I used DownloadStringAsync):
private void Form1_Shown(Object s1, EventArgs e1) {
string word = "1.4";
var url = "http://chipperyman573.com/rtf/textbot.html";
var client = new WebClient();
client.DownloadStringCompleted += (s2, e2) =>
{
if(e2.Error != null)
{
//Maybe do some error handling?
}
else
{
if (e2.Result == word)
{
update = false;
MessageBox.Show("Congrats! You are running the latest version (" + word + ") of Chip Bot!\n\nGot an idea for this program? Use the \"Send feedback\" button to let me know!", "Chip Bot", MessageBoxButtons.OK, MessageBoxIcon.Information);
Text = "Chip Bot" + word + " - Got an idea for this program? Send me some feedback!";
}
else
{
Text = "Chip Bot (UPDATE AVAILABLE)";
go.ForeColor = Color.Gray;
setup.Enabled = false;
otherGroup.Enabled = false;
optionsGroup.Enabled = false;
MessageBox.Show("There is an update! Downloading now! \n\nUNTIL YOU UPDATE THE PROGRAM WILL NOT FUNCTION.", "Chip Bot", MessageBoxButtons.OK, MessageBoxIcon.Information);
url = "";
var web = new WebBrowser();
web.Navigate(url);
}
}
};
client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
}
When I click the button to open a file the OpenFileDialog box opens twice. The first box only opens the image files and the second box text files. If the user decides to close out the first box without choosing a file the second box pops up as well. I'm not sure what I am over looking on this issue. Any help would be appreciated. Thanks!
OpenFileDialog of = new OpenFileDialog();
of.Filter = "All Image Formats|*.jpg;*.png;*.bmp;*.gif;*.ico;*.txt|JPG Image|*.jpg|BMP image|*.bmp|PNG image|*.png|GIF Image|*.gif|Icon|*.ico|Text File|*.txt";
if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
image1.Source = new BitmapImage(new Uri(of.FileName));
// enable the buttons to function (previous page, next page, rotate left/right, zoom in/out)
button1.IsEnabled = false;
button2.IsEnabled = false;
button3.IsEnabled = false;
button4.IsEnabled = false;
button5.IsEnabled = true;
button6.IsEnabled = true;
button7.IsEnabled = true;
button8.IsEnabled = true;
}
catch (ArgumentException)
{
// Show messagebox when argument exception arises, when user tries to open corrupted file
System.Windows.Forms.MessageBox.Show("Invalid File");
}
}
else if(of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
using (StreamReader sr = new StreamReader(of.FileName))
{
textBox2.Text = sr.ReadToEnd();
button1.IsEnabled = true;
button2.IsEnabled = true;
button3.IsEnabled = true;
button4.IsEnabled = true;
button5.IsEnabled = true;
button6.IsEnabled = true;
button7.IsEnabled = true;
button8.IsEnabled = true;
}
}
catch (ArgumentException)
{
System.Windows.Forms.MessageBox.Show("Invalid File");
}
}
You're calling ShowDialog() twice:
if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//...
}
else if(of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
Just call it once, and save the results:
var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
//...
}
// Note that the condition was the same!
else if(dialogResult != System.Windows.Forms.DialogResult.OK)
Edit:
If you want the second dialog to only show when the first is handled, you can do:
var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
//...
// Do second case here - Setup new options
dialogResult = of.ShowDialog(); //Handle text file here
}
You are calling ShowDialog both in the if and else if condition. That method is responsible for showing the dialog and only has the nice side effect of also telling you what the user clicked.
Store the method's result in a variable and check that in your if..elseif conditions instead.
I am trying to write notepad, how do I know if user clicked 'Cancel'? My code doesn't work:
private void SaveAsItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "untitled";
saveFileDialog1.Filter = "Text (*.txt)|*.txt";
saveFileDialog1.ShowDialog();
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
SaveFile.WriteLine(richTextBox1.Text);
SaveFile.Close();
if (DialogResult == DialogResult.Cancel)
{
richTextBox1.Text = "CANCEL";
issaved = false;
}
else
{
issaved = true;
}
}
You're checking the DialogResult property for your main form, but it's the child form that you want to check. So...
var dr = saveFileDialog1.ShowDialog();
if( dr == DialogResult.OK )
{
using(var SaveFile = new StreamWriter(saveFileDialog1.FileName))
{
SaveFile.WriteLine(richTextBox1.Text);
issaved = true;
}
}
else // cancel (or something else)
{
richTextBox1.Text = "CANCEL";
issaved = false;
}
Also, you should wrap your StreamWriter in a using block as I have done above. Your code will fail to close the file if an exception occurs. A using block is syntactic sugar for a try/finally block which calls Dispose() (which in turn calls Close()) in the finally portion.
DialogResult res = saveFileDialog1.ShowDialog();
if (res == DialogResult.Cancel) {
// user cancelled
}
else {
// Write file to disk using the filename chosen by user
}
You are creating the file before checking the result of the dialog. Move the SaveFile variable bit into the "issaved = true" block.
[edit] And as the others said, check the Dialog result properly