Login System (ASP.NET C#)

Need some help with this please

Page 1 of 1

9 Replies - 8220 Views - Last Post: 25 June 2010 - 11:56 PM Rate Topic: -----

#1 Kannibal  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-December 09

Login System (ASP.NET C#)

Posted 04 June 2010 - 06:49 AM

i have been trying to create a login system for a small project. The username and password are stored in MS SQL i can log in but the session will remain as not logged in any ideas why guys ?

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SqlCommand command = new SqlCommand("SELECT * FROM Member WHERE MemberUsername = @Username AND MemberPassword = @Password", c.connection);
            c.connection.Open();
            command.Parameters.AddWithValue("@Username", TxtUsername.Text);
            command.Parameters.AddWithValue("@Password", TxtPassword.Text);
            SqlDataReader DataReader = command.ExecuteReader();

            while (DataReader.Read())
            {
                if (DataReader["MemberRoleID"].ToString() == "1")
                {
                    Session["userid"] = TxtUsername.Text;
                    LblWelcome.Text = "Welcome: " + (Session["userid"]);
                    Response.Redirect("Register.aspx");
                    BtnSubmit.Enabled = false;
                }
                else if (DataReader["MemberRoleID"].ToString() == "2")
                {
                    Session["userid"] = TxtUsername.Text;
                    LblWelcome.Text = "Welcome: " + (Session["userid"]);
                    Response.Redirect("PostComments.aspx");
                    BtnSubmit.Enabled = false;
                }
            }
            c.connection.Close();
        }
    }


Is This A Good Question/Topic? 1
  • +

Replies To: Login System (ASP.NET C#)

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Login System (ASP.NET C#)

Posted 04 June 2010 - 06:57 AM

You are going to have to provide some specifics on what you mean by 'session will remain as not logged in'.

Are you using Membership?

Are you simply referring to session variables?

The more detail you provide, the more likely we can help you.
Was This Post Helpful? 0
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Login System (ASP.NET C#)

Posted 04 June 2010 - 07:13 AM

I'm also not sure what you mean when you say "Session will remain as not logged in".

Looking at your code I noticed that you are closing the connection to your database after you are calling the Response.Redirect() method. You should probably move the this method call to After you close the database connection:

 protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SqlCommand command = new SqlCommand("SELECT * FROM Member WHERE MemberUsername = @Username AND MemberPassword = @Password", c.connection);
            c.connection.Open();
            command.Parameters.AddWithValue("@Username", TxtUsername.Text);
            command.Parameters.AddWithValue("@Password", TxtPassword.Text);
            SqlDataReader DataReader = command.ExecuteReader();

            string redirectURL ="";
            while (DataReader.Read())
            {
                if (DataReader["MemberRoleID"].ToString() == "1")
                {
                    Session["userid"] = TxtUsername.Text;
                    redirectURL = "Register.aspx";
                }
                else if (DataReader["MemberRoleID"].ToString() == "2")
                {
                    Session["userid"] = TxtUsername.Text;
                    redirectURL = "PostComments.aspx";
                }
            }
            c.connection.Close()
            //displaying "Welcome xxx" and disabling the button is redundant if you are just
            //going to redirect away from the current page anyways.
            LblWelcome.Text = "Welcome: " + (Session["userid"]);
            BtnSubmit.Enabled = false;
            Response.Redirect(redirectURL);
        }
    }


Please provide more details to get more help with your problem.

-Frinny
Was This Post Helpful? 1
  • +
  • -

#4 Kannibal  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-December 09

Re: Login System (ASP.NET C#)

Posted 04 June 2010 - 08:10 AM

sorry for not providing much but im still a noob at this :(

the login system will work but when i am redirected to the register or post comments page and go back to the login page btn submit will not be disabled its as if the user never did log in i have no idea what to do that is the issue i am dealing with guys im stuck and dont know what to do
Was This Post Helpful? 0
  • +
  • -

#5 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Login System (ASP.NET C#)

Posted 04 June 2010 - 09:04 AM

Ahh see that's what I was getting at in my comments.
After you redirect to another page then it doesn't matter if set the text or disable the button :)

You need to add code in your login page to check if the there is a user name in session already and if there is then disable the button and display the "welcome" message in the Label.

I would do this on the Page Load event.
Was This Post Helpful? 2
  • +
  • -

#6 Kannibal  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-December 09

Re: Login System (ASP.NET C#)

Posted 04 June 2010 - 03:07 PM

i created this and worked in the page load out

       
 if (Session["userid"] == null)
 {
 BtnSubmit.Enabled = true;
 }
 else
 {
 BtnSubmit.Enabled = false;
 }


Was This Post Helpful? 0
  • +
  • -

#7 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Login System (ASP.NET C#)

Posted 05 June 2010 - 01:01 PM

So the problem was solved??!!
Was This Post Helpful? 0
  • +
  • -

#8 Kannibal  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-December 09

Re: Login System (ASP.NET C#)

Posted 05 June 2010 - 04:15 PM

yes added that code to the page load and the login worked great
Was This Post Helpful? 0
  • +
  • -

#9 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Login System (ASP.NET C#)

Posted 06 June 2010 - 08:01 AM

Thank you so much Mr Kannibal for your answer, i really need this code.
Was This Post Helpful? 0
  • +
  • -

#10 Kannibal  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-December 09

Re: Login System (ASP.NET C#)

Posted 25 June 2010 - 11:56 PM

no problem mate :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1