Solved

C# validation against text box entry for Login

Posted on 2011-02-11
4
358 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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