Solved

C# validation against text box entry for Login

Posted on 2011-02-11
4
356 Views
Last Modified: 2012-06-27
Hi, I am currently Trying to authenticate my users against a database i have tried myself and after pressing the submit page nothing happens I have ran the sql query against my database and it returns the required information for my code. Any one got any idea why it isnt working

Working query provides the Answer 1
SELECT COUNT(*) FROM Students WHERE (StudentEmail = 'ssmith@gmail.com') AND (StudentPassword = 'cccc') 

Open in new window


C# code
    protected void lbtn_1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection connection = null;
        try
        {
            // Conenction to the Database to check entry values against database
            string conn = ConfigurationManager.ConnectionStrings["MyString"].ConnectionString;
            connection = new SqlConnection(conn);

            connection.Open();
            SqlCommand studentpasscount = new SqlCommand ("SELECT COUNT(*) FROM Students WHERE (StudentEmail = '" +txtlogin.Text+ "') AND (StudentPassword = '" +txtpassword.Text+ "')");
            string Accounts = null;


            if (Accounts == "1")
            {
                Response.Redirect("StudentHomePage.aspx");
                
            }

            else if (Accounts == "0")
            {
                connection.Open();
                SqlCommand lecpasscount = new SqlCommand("SELECT COUNT(*) FROM Lecturers WHERE (LecturerEmail='" + txtlogin.Text + "') AND (LecturerPassword='" + txtpassword.Text + "')");
            }
            if (Accounts == "1")
            {
                Response.Redirect("LecturerHomePage.aspx");

            }

            else if (Accounts == "0")
            {
                connection.Open();
                SqlCommand adminpasscount = new SqlCommand("SELECT COUNT(*) FROM Administrators WHERE (AdminEmail='" + txtlogin.Text + "') AND (AdminPassword='" + txtpassword.Text + "')");
            }
            if (Accounts == "1")
            {
                Response.Redirect("AdminHomePage.aspx");

            }

            else if (Accounts == "0")
            {
                Response.Redirect("LoginError.aspx");
            }


         }
       
        finally
        {

            connection.Close();
        }

    }

Open in new window


txtlogin and txtpassword are the ID's of two textbox's i have on the page and the submit button is called lbtn_1


EDIT: forgot to add that i need it to cycle through the three tables checking against all of them before finally sending the user to the error page, the query at the top i used just as an example of finding a match in the student table.  Hope thats clear

Thanks for the help!
Nick
0
Comment
Question by:Nick1988
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 34873612
I guess your string Accounts = null; needs to be set somewhere in the code when the query fetches records from DB.

This Accounts is been checked every where in the code.
0
 
LVL 15

Expert Comment

by:tim_cs
ID: 34874285
Also,

For security reasons if you do nothing else you should really have your queries in a stored procedure.  Your login process at the moment could be really easy to hack.  
0
 

Accepted Solution

by:
Nick1988 earned 0 total points
ID: 35027340
protected void lbtn_1_Click(object sender, EventArgs e)
    {
        SqlConnection connection = null;
        try
        {
            // This code is used during login. The code connects to the Database and checks the users entered values against records stored in the database

            //stored database conenction connection information can be found in the web.config file which stores the full connection string 
            string conn = ConfigurationManager.ConnectionStrings["Your Connection"].ConnectionString;
            connection = new SqlConnection(conn);

            // string for checking login against lecturer table 
            string Accountl = null;

            //string for checking login against student table
            string Accounts = null;

            //string for checking login against admin table
            string Accounta = null;

            //opens the connection to the Database
            connection.Open();

            //stores the sql query as a procedure allowing it to be called on login, query counts matches against the login fields and if they find one match it allows the user through
            string sqllogs = "SELECT COUNT(*) FROM Students WHERE StudentEmail = '" + txtlogin.Text + "' AND StudentPassword = '" + txtpassword.Text + "'";

            //calls the sql query from stored procedure 
            SqlCommand log = new SqlCommand(sqllogs, connection);

            //converts the result to a string
            Accounts = Convert.ToString(log.ExecuteScalar());

            //if the query finds a result it will forward the user onto the correct user page
            if (Accounts == "1")
            {
                Response.Redirect("StudentHomePage.aspx");

            }
            //if no results are found it will try the next query in the list 
            else if (Accounts == "0")
            {
                string sqllogl = "SELECT COUNT(*) FROM Lecturers WHERE LecturerEmail = '" + txtlogin.Text + "' AND LecturerPassword = '" + txtpassword.Text + "'";


                SqlCommand logl = new SqlCommand(sqllogl, connection);


                Accountl = Convert.ToString(logl.ExecuteScalar());

            }
            if (Accountl == "1")
            {
                Response.Redirect("LecturerHomePage.aspx");

            }

            else if (Accountl == "0")
            {

                string sqlloga = "SELECT COUNT(*) FROM Administrators WHERE AdminEmail = '" + txtlogin.Text + "' AND AdminPassword = '" + txtpassword.Text + "'";


                SqlCommand loga = new SqlCommand(sqlloga, connection);


                Accounta = Convert.ToString(loga.ExecuteScalar());
            }
            if (Accounta == "1")
            {
                Response.Redirect("AdminHomePage.aspx");

            }
            //after the last query has been tested if no results come back the user is sent to a login error page
            else if (Accounta == "0")
            {
                Response.Redirect("LoginError.aspx");
            }


        }

        finally
        {
            //closes the conenction to the database
            connection.Close();
        }

    }

Open in new window


Solved
0
 

Author Closing Comment

by:Nick1988
ID: 35067586
Worked it out.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question