?
Solved

C# validation against text box entry for Login

Posted on 2011-02-11
4
Medium Priority
?
360 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 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