Solved

C# validation against text box entry for Login

Posted on 2011-02-11
4
352 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
  • 2
4 Comments
 
LVL 9

Expert Comment

by:s_chilkury
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Worked it out.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now