Logged-in User appears in Main Form C#

Asked

Viewed 233 times

1

Good afternoon!!!

I have a problem that I don’t know why... I have a program in a Windows Forms C#. It has a Login window that goes into a Main window. I use the same principle of storing the logged-in user session and searching during the running application. And it works because it stores this user in the database at any time I perform the function. But if I ask to show this user on a label or textbox in the window, it does not appear!!! And the interesting thing is that if I put a button in the main window to open a child form and put exactly the SAME CODE in the child form, the said whose user appears... It’s like the main form doesn’t allow it... Can anyone tell me where I’m going wrong?

"Login class"

public class LoginSenha

{
    public static string AUsuario = "";
    public static string ANOME = "";
}

"Form Login" - event click to login

private void btnAcessar_Click(object sender, EventArgs e)    
        {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=R:\Projetos\Unilever\Software\DataBase_Knorr.accdb";
        con.Open();
        OleDbCommand cmd = new OleDbCommand("SELECT Usuario, SENHA FROM USUARIOS WHERE Usuario='" + lblUser.Text + "'and SENHA='" + txtSenha.Text + "'", con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            frm_Fabrima frl = new frm_Fabrima();
            frl.Show();
            this.Visible = false;

            LoginSenha.AUsuario = lblUser.Text;
            LoginSenha.ANOME = lblNOME.Text;

            usuarioConectado = lblUser.Text;

        }
        else
        {
            MessageBox.Show("Usuário/Senha Incorretos");
        }
        con.Close();
    }

"Form Principal"

...No Form_load...

 private void frm_Fabrima_Load(object sender, EventArgs e)
{
        inicializar();

        CarregaSKU();

        txtOrdemProducao.Focus();

        cbSKU.Text = "";

        lblConectado.Text = frm_Login.usuarioConectado;

        lblConectado.Text = LoginSenha.ANOME.ToString();

        rbBartelt1.Checked = true;
    }

From what you’ve seen, I’ve tried two ways to try and look

  • Put the whole part of your source, referring to each excerpt you have already reported in your question... From what you’ve informed us, there’s not much to help you with. I believe that you are not passing the object correctly between the screens, but only with you informing us the rest of the source to confirm this.

  • First, thanks for the help... I changed the post, see if it becomes more understandable the codes

  • All right, now you’ve made it look good, I can’t give you a detailed answer because the Moderator marked your question as pending, but you need to instantiate the class LoginSenha in your Login form and pass it in the constructor of your main screen frm_Fabrima frl = new frm_Fabrima(dadosLogin);

  • Set the Loginsenha class to Static

  • 1

    @Matheusribeiro I tried these changes and it still doesn’t work. Now is it strange that even not instantiating the class and passing the constructor, because it appears in any child form that I create, except in the main? Must be some property of the parent form I have to change... Let’s hope to release the post to communicate better

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.