Strange code execution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Default

    Strange code execution

    Hi, I am new to C#, that is why I am not sure what kind of problem it is: Is
    VS files corrupted , or something else. that is the problems description:
    I am working on a small database project. I am not using any data sources
    Mysql, access etc. Instead I use binary formatter to store and read data. at
    the beginning the program checks username/password. it does it in the
    following way:

    if(form2.initia lized)
    {
    for(int i=0; i<database.user s.Count;i++)
    {
    User user;
    user=(User)data base.users[i]
    s1=System.Text. ASCIIEncoding.A SCII.GetString (System.Convert .FromBase64Stri ng(user.usernam e));
    s2=System.Text. ASCIIEncoding.A SCII.GetString
    (System.Convert .FromBase64Stri ng(user.passwor d));
    if(s1==form2.us ername.Text && s2==form2.passw ord.Text)
    {
    accessmode=user .accessmode;
    user.lastlogin= System.DateTime .Now;
    SaveDatabase();
    userlogged=true ;
    break;
    }
    }
    }
    if(!userlogged)
    {
    System.Windows. Forms.MessageBo x.Show("Incorec t
    Username/Password.Access denied!");
    Close(); //Code execution jumps here!!!
    }

    There is a strange problem: suppose user entered correct username/password,
    that is, userogged=true, ok? surprisingly after break takes places code
    execution jumps to Close() and form closes, even MessageBox.Show function
    does not execute. The same occures in another code:

    System.IO.Direc tory.CreateDire ctory(df);
    Form3 form3=new Form3();
    form3.ShowDialo g(this);
    if(!form3.initi alized)
    {
    System.Windows. Forms.MessageBo x.Show("You must have at least one
    administrator username and password. Try next time.");
    Close();
    }
    execution jumps straight to Close() (not to MessageBox.Show (). if
    form3.initializ ed==false, the program shows message box, in another case it
    simply closes.
    Can anyone clarify this point? May be VS files are corrupted, or there is an
    error in code?

  • Nick Malik [Microsoft]

    #2
    Re: Strange code execution

    I'm not seeing a problem with your code.

    1. I'd like to see the code that CALLS each of these.
    2. You say that in the debugger, you are seeing the system jump to the
    "close" call? Normally, I would look for things like misplaced commenting
    causing a block to be balanced incorrectly, or a string that has been left
    open. (One reason I don't particularly care for multi-line strings).
    3. This is a good-old-fashioned stumper. If you think that some files are
    corrupt, you can try creating a new project and copying the code files from
    the old project. In fact, I usually consider it good practice to see if I
    can extract a problem like this into a small snippet that exhibits the
    behavior. That is something you can post or even share with product support
    services. If you create a snippet like this, feel free to post it online.
    That may help.

    --
    --- Nick Malik [Microsoft]
    MCSD, CFPS, Certified Scrummaster


    Disclaimer: Opinions expressed in this forum are my own, and not
    representative of my employer.
    I do not answer questions on behalf of my employer. I'm just a
    programmer helping programmers.
    --
    "Default" <Default@discus sions.microsoft .com> wrote in message
    news:EC227067-6D5F-4A76-8EAB-BAEC448A66B0@mi crosoft.com...[color=blue]
    > Hi, I am new to C#, that is why I am not sure what kind of problem it is:[/color]
    Is[color=blue]
    > VS files corrupted , or something else. that is the problems description:
    > I am working on a small database project. I am not using any data sources
    > Mysql, access etc. Instead I use binary formatter to store and read data.[/color]
    at[color=blue]
    > the beginning the program checks username/password. it does it in the
    > following way:
    >
    > if(form2.initia lized)
    > {
    > for(int i=0; i<database.user s.Count;i++)
    > {
    > User user;
    > user=(User)data base.users[i];
    > s1=System.Text. ASCIIEncoding.A SCII.GetString[/color]
    (System.Convert .FromBase64Stri ng(user.usernam e));[color=blue]
    > s2=System.Text. ASCIIEncoding.A SCII.GetString
    > (System.Convert .FromBase64Stri ng(user.passwor d));
    > if(s1==form2.us ername.Text && s2==form2.passw ord.Text)
    > {
    > accessmode=user .accessmode;
    > user.lastlogin= System.DateTime .Now;
    > SaveDatabase();
    > userlogged=true ;
    > break;
    > }
    > }
    > }
    > if(!userlogged)
    > {
    > System.Windows. Forms.MessageBo x.Show("Incorec t
    > Username/Password.Access denied!");
    > Close(); //Code execution jumps here!!!
    > }
    >
    > There is a strange problem: suppose user entered correct[/color]
    username/password,[color=blue]
    > that is, userogged=true, ok? surprisingly after break takes places code
    > execution jumps to Close() and form closes, even MessageBox.Show function
    > does not execute. The same occures in another code:
    >
    > System.IO.Direc tory.CreateDire ctory(df);
    > Form3 form3=new Form3();
    > form3.ShowDialo g(this);
    > if(!form3.initi alized)
    > {
    > System.Windows. Forms.MessageBo x.Show("You must have at least one
    > administrator username and password. Try next time.");
    > Close();
    > }
    > execution jumps straight to Close() (not to MessageBox.Show (). if
    > form3.initializ ed==false, the program shows message box, in another case[/color]
    it[color=blue]
    > simply closes.
    > Can anyone clarify this point? May be VS files are corrupted, or there is[/color]
    an[color=blue]
    > error in code?
    >[/color]


    Comment

    Working...