Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have created a window form for login, where user can login with an id and password. After authentication the user can move to the Select_Company form. From there the user select the company and branch and moves to transaction window form.

On initial login the user properly logged in and moved to Select_company window form. When he clicks the log out button on Select_company window form he is moved to the login form again.

On further login from login form he is supposed to move to the select_company form again but the select_company form does not display.

I properly debugged the program. It runs well but I am unable to find the reasons for it not displaying the Select_company window form after further login.

Please advise me on what to do. Thanks in advance.

Below is the code:

Login form:

C#
private void btnLogin_Click(object sender, EventArgs e)
{
    if (validation() == true)
    {
        try
        {
            conn = Aryan.connString();
            Aryan.sqlcon = new System.Data.SqlClient.SqlConnection(conn);
            if (Aryan.sqlcon.State == ConnectionState.Closed)
            {
                Aryan.sqlcon.Open();
            }
            Aryan.sqlcom = new System.Data.SqlClient.SqlCommand("Str_UserLogin", Aryan.sqlcon);
            Aryan.sqlcom.CommandType = CommandType.StoredProcedure;
            Aryan.sqlcom.Parameters.AddWithValue("@LoginId", txtUserName.Text);
            Aryan.sqlcom.Parameters.AddWithValue("@Password", txtPassword.Text);
            System.Data.SqlClient.SqlParameter pm = new System.Data.SqlClient.SqlParameter("@return", SqlDbType.Int);
            pm.Direction = ParameterDirection.Output;
            pm.Size = 50;
            Aryan.sqlcom.Parameters.Add(pm);
            System.Data.SqlClient.SqlParameter pm1 = new System.Data.SqlClient.SqlParameter("@UserId", SqlDbType.Int);
            pm1.Direction = ParameterDirection.Output;
            pm1.Size = 50;
            Aryan.sqlcom.Parameters.Add(pm1);
            System.Data.SqlClient.SqlParameter pm2 = new System.Data.SqlClient.SqlParameter("@UserType", SqlDbType.VarChar);
            pm2.Direction = ParameterDirection.Output;
            pm2.Size = 50;
            Aryan.sqlcom.Parameters.Add(pm2);
            Aryan.sqlcom.ExecuteNonQuery();
            int output = (int)Aryan.sqlcom.Parameters["@return"].Value;
            if (output == 1)
            {
                int usercode = (int)Aryan.sqlcom.Parameters["@UserId"].Value;
                string userType = (string)Aryan.sqlcom.Parameters["@UserType"].Value;
                updateUser(usercode);
                Aryan.GetID = usercode;
                Aryan.CurrentUserType = userType;
                Aryan.CurrentUser = txtUserName.Text;
                Aryan.CurrentSession = session;
                pAccept = true;
                this.Close();
                //this.Visible = false;
            }
            else if (output == 10)
            {
                MessageBox.Show("Invalid Login Id or Password", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtUserName.Focus();
                return;
            }
            else if (output == 20)
            {
                MessageBox.Show("This User Already Logged in. Contact to Administrator !");
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtUserName.Focus();
                return;
            }
            else if (output == 30)
            {
                MessageBox.Show("This User Not Permitted For Any Transaction.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtUserName.Focus();
                //Environment.Exit(0);
            }
            else if (output == 40)
            {
                MessageBox.Show("This User is No Longer Active. Contact to Administrator.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtUserName.Focus();
                return;
                //Environment.Exit(0);
            }
            else
            {
                MessageBox.Show("Problem occur while Login");
                return;
            }
        }

        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
        finally
        {
            Aryan.sqlcon.Close();
        }
    }
}


Log out:

C#
private void LogOut()
        {
            conn = Aryan.connString();
            Aryan.sqlcon = new System.Data.SqlClient.SqlConnection(conn);
            try
            {
                if (Aryan.sqlcon.State == ConnectionState.Closed)
                {
                    Aryan.sqlcon.Open();
                }
                Aryan.sqlcom = new System.Data.SqlClient.SqlCommand("str_LogOut", Aryan.sqlcon);
                Aryan.sqlcom.CommandType = CommandType.StoredProcedure;
                Aryan.sqlcom.Parameters.AddWithValue("@userId", Aryan.GetID);
                Aryan.sqlcom.Parameters.AddWithValue("@LogOut", Aryan.getSqlDateStr(System.DateTime.Now));
                Aryan.sqlcom.Parameters.AddWithValue("@TimeOut", Aryan.getSqlTimeStr(System.DateTime.Now));
                Aryan.sqlcom.Parameters.AddWithValue("@Session", Aryan.CurrentSession);
                Aryan.sqlcom.ExecuteNonQuery();
                Aryan.GetID = 0;
                Aryan.CurrentUser = "";
                Aryan.CurrentSession = 0;
                Aryan.CurrentUserType = "";
                Aryan.CurrentCompany = "";
                Aryan.CurrentBranch = "";
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            finally
            {
                Aryan.sqlcon.Close();
            }
        }

        
        private void btnLogOut_Click(object sender, EventArgs e)
        {
            LogOut();
            lstCompany.Items.Clear();
            lstBranch.Items.Clear();
            this.Visible = false;
            this.Close();
            frmLogin Login = new frmLogin();
            Login.ShowDialog();
        }

MainPage(Parent Form):

C#
private void CheckUser()
        {
            conn = Aryan.connString();
            Aryan.sqlcon = new System.Data.SqlClient.SqlConnection(conn);
            try
            {
                if (Aryan.sqlcon.State == ConnectionState.Closed)
                {
                    Aryan.sqlcon.Open();
                }
                Aryan.sqlcom = new System.Data.SqlClient.SqlCommand("str_CheckUserFirstTime", Aryan.sqlcon);
                Aryan.sqlcom.CommandType = CommandType.StoredProcedure;
                System.Data.SqlClient.SqlParameter pm = new System.Data.SqlClient.SqlParameter("return", SqlDbType.Int);
                pm.Direction = ParameterDirection.Output;
                pm.Size = 50;
                Aryan.sqlcom.Parameters.Add(pm);
                Aryan.sqlcom.ExecuteNonQuery();
                int output = (int)Aryan.sqlcom.Parameters["return"].Value;
                if (output == 0)
                {
                    frmSuperuser Super = new frmSuperuser();            // SuperUser call & create for first time
                    Super.ShowDialog();
                }
                else if (Aryan.CheckInstalledCompany())
                {
                    frmInstallCompany company = new frmInstallCompany();
                    company.ShowDialog();
                }
                else
                {
                    frmLogin Login = new frmLogin();
                    Login.ShowDialog();                    
                    if (Login.pAccept)
                    {                      
                        frmSelectCompany SelectCompany = new frmSelectCompany();                        
                        SelectCompany.ShowDialog();
                        if (SelectCompany.DialogResult == DialogResult.Cancel)
                        {
                            statusBar.Panels[0].Text = "User Name:" + Aryan.CurrentUser;
                            statusBar.Panels[1].Text = "User Type:" + Aryan.CurrentUserType;
                            statusBar.Panels[2].Text = "Session:" + Aryan.CurrentSession;
                            statusBar.Panels[3].Text = "Branch:" + Aryan.CurrentBranch;
                            statusBar.Panels[4].Text = "Clock:";
                            //frmMain main = new frmMain();
                            //main.Text = Aryan.CurrentCompany;
                            //statusBar.Show();
                            //statusBar.Visible = true;
                        }
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return;
            }
            finally
            {
                Aryan.sqlcon.Close();
            }
        }
Posted
Updated 9-Apr-13 11:06am
v4
Comments
Sergey Alexandrovich Kryukov 9-Apr-13 15:21pm    
I would advise: first, add the tag "WinForms" and make the title more relevant to the problem.
—SA
OriginalGriff 9-Apr-13 15:28pm    
We can't really tell from that description.
Post your code to create and show the new form, and we might be able to help.
Use the "Improve question" widget to edit your question and provide better information.
Sergey Alexandrovich Kryukov 9-Apr-13 15:28pm    
The major problem is: you are not asking any question. By just this reason, it's hard to expect advise...
I would advice to use only one form, certainly, showing one or another part of it at a time. Multiple forms is evil. I don't know what's your concerns are...
—SA
Kschuler 9-Apr-13 17:09pm    
That's a lot of code for all of us to look through. It would be nice if you could narrow it down or mark the bits that are relevant. For instance, which line of code is supposed to show the Select_Company form?

Honestly, I think you need to redesign your application, you're not using different layers and are creating database connection all over the place. I propose you redesign to a DAL - BL - GUI layered approach before addressing any further problems. in The Business Layer (BL) you can then keep a status of the user (logged in or out) and create your GUI logic based on that value.

You'll have an easier way to debug and my guess is your problem will be fixed by the time you've finished.
 
Share this answer
 
Comments
LaxmikantYadav 10-Apr-13 2:40am    
Nice Suggestion
The code

C#
frmLogin Login = new frmLogin();
                    Login.ShowDialog();                    
                    if (Login.pAccept)
                    {                      
                        frmSelectCompany SelectCompany = new frmSelectCompany();                        
                        SelectCompany.ShowDialog();


shows that , assuming the Login form is shown, and closed somehow, the select company form will be shown if Login.pAccept is true.

So, either the login form is not closing or pAccept is false when it does close.

I agree with V - this is messy code and very hard to follow; at the very least you need to move stuff into its own methods to make it easier to follow!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900