Reputation: 319
I have login
form and agent
form in my application. The first scenario of my application is when the user login successfully all the credentials(server , username, password, status
) will be save in text file and I already finish it.
My second scenario is when the user close the agent
form and open the application again, the application will read all the credential in text file and will be login automatically and I already finish it but my problem is when I open the application both form show the login
and agent
form where the expected result is only the agent
form will display.
Here is my code for login form:
private void login_Load_1(object sender, EventArgs e)
{
Class.loginFunction logs = new Class.loginFunction();
string isLogged, user, pass, server;
try
{
isLogged = logs.readConfigFile(4);
user = logs.readConfigFile(2);
pass = logs.readConfigFile(3);
server = logs.readConfigFile(1);
if (isLogged.Equals("1"))
{
logs.loginAuthentication(user, pass);
}
else
{
txtServer.Text = server;
}
}
catch (Exception ex)
{
MessageBox.Show("Exception Thrown: " + ex.Message);
}
}
Here is the code for the method of login:
public void loginAuthentication(string agentId, string pass)
{
login idx = new login();
WebServiceInfo webInfo = new WebServiceInfo();
LoginInfo logInfo = new LoginInfo();
eDataNewUi eNewUi = new eDataNewUi();
try
{
logInfo.serverAddress = readConfigFile(1);
webInfo.url = "http://" + logInfo.serverAddress + "/eDataTran/service/main/agentLogin"; // store Url Of service in string
webInfo.jsonData = "{\"agentId\":\"" + agentId + "\" ,\"password\":\"" + pass + "\"}";
// Convert our JSON in into bytes using ascii encoding
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(webInfo.jsonData);
// HttpWebRequest
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(webInfo.url);
webrequest.Method = "POST";
webrequest.ContentType = "application/json";
webrequest.ContentLength = data.Length;
// Get stream data out of webrequest object
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
// Declare & read the response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// Fetch the response from the POST web service
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string result = loResponseStream.ReadToEnd();
loResponseStream.Close();
JObject o = JObject.Parse(result);
string responseCode = o["Data"][0]["ResponseCode"].ToString();
switch (responseCode)
{
case "0":
MessageBox.Show("Username and Password is not valid");
break;
case "1":
idx.Hide(); // this part where the form is not hiding
eNewUi.Show();
eNewUi.tssUserId.Text = "User Id: " + agentId;
getAgentId(agentId);
break;
}
webresponse.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception Response: " + ex.Message);
}
}
Here is my program.cs
static Mutex mutex = new Mutex(false, "oreilly.com OneAtATimeDemo");
[STAThread]
static void Main()
{
if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false))
{
MessageBox.Show("Application is running already!");
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new login());
}
finally
{
mutex.ReleaseMutex();
}
}
Upvotes: 0
Views: 114
Reputation: 3691
You have not mentioned anything about the Program.cs
but I have seen some similar problems so I will just assume the contents is similar to the following
private static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
Application already creates a Login
object for you and in method loginAuthentication
public void loginAuthentication(string agentId, string pass){
login idx = new login();//Here you created another Login object
....
}
As far as I am concerned your program successfully comes to case "1":
switch (responseCode) {
case "0":
MessageBox.Show("Username and Password is not valid");
break;
case "1":
//This is the part where you are trying to hide a form, which is not even shown
idx.Hide();
Suggested Main:
private static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var hasCredentialFile = CheckCredentialFile();
Application.Run(hasCredentialFile ? new Agent():Login());
}
Upvotes: 1
Reputation: 693
As per info of the scenarios, you would be needing to check if the Textfile is generated. You should add a checking method on your Program.cs
if this TextFile was generated else your application would fire the LoginFrm
Sample Code:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(TextFileChecker())
{
Application.Run(new AgentFrm());
}
else
{
Application.Run(new LoginFrm());
}
}
private bool TextFileChecker()
{
//run a checking method for the textfile
}
}
Upvotes: 2