Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im trying to create a login form for a website using ms access database. I'm using visual studio 2010 c# and access 2013. For some reason I can't get it to log in. I'm really new to this so any help is appreciated.

DataLayer:
C#
public class DataConnector
    {
        protected OleDbDataAdapter DataAdapter1 = new OleDbDataAdapter();
        public string ErrorMessage = "";
        public DataConnector(string ConnectionString)
        {
            OleDbConnection Connection1 = new OleDbConnection(ConnectionString);
            this.DataAdapter1.SelectCommand = new OleDbCommand("", Connection1);
            this.DataAdapter1.InsertCommand = new OleDbCommand("", Connection1);
        }
        public DataTable DataSelect(string query)
        {
            DataTable dt = new DataTable();
            try
            {
                DataAdapter1.SelectCommand.CommandText = query;
                DataAdapter1.SelectCommand.Connection.Open();
                DataAdapter1.Fill(dt);
                DataAdapter1.SelectCommand.Connection.Close();
                ErrorMessage = "";
            }
            catch(Exception err)
            {
                ErrorMessage = err.Message;
                DataAdapter1.SelectCommand.Connection.Close();
            }
            return dt;
        }
        public int DataInsert(string query)
        {
            int Result = 0;
            try
            {
                DataAdapter1.InsertCommand.CommandText = query;
                DataAdapter1.InsertCommand.Connection.Open();
                Result = DataAdapter1.InsertCommand.ExecuteNonQuery();
                DataAdapter1.InsertCommand.Connection.Close();
                ErrorMessage = "";
                return Result;
            }
            catch (Exception err)
            {
                ErrorMessage = err.Message;
                DataAdapter1.InsertCommand.Connection.Close();
                return 0;
            }
            
        }
        public int DataUpdate(string query)
        {
            return DataInsert(query);
        }
        public int DataDelete(string query)
        {
            return DataInsert(query);
        }
    }


Default.aspx.cs:
C#
public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {

            DataLayer.DataConnector dat = new DataLayer.DataConnector("Provider=Microsoft.ACE.OLEDB.12.O;"+"Data Source='"+Server.MapPath("site_database.accdb")+"'; Persist Security Info=False;");
            DataTable dt = dat.DataSelect("select UserID from tbl_login where Username = '" + txtUsername.Text + "' and Password = '"+ txtPassword.Text +"' ");
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("members_area.aspx");
            }
            else
                lblerror.Text = "Login failed";
            
        }
    }


I'm not getting any errors and I just can't figure it out. When I try to log in it just stays on the default.aspx page.
Posted
Updated 18-Jan-14 5:55am
v3
Comments
Debug your code, go to each function and check what is happening.

1 solution

There are so many, many ways this could go wrong on you. The first thing to do is to stop doing it like that!

The first thing to not do is build SQL commands by concatenating strings. This isn't causing teh problem you are talking about, but it does leave you wide open to an SQL Injection attack, which can damage or destroy your database. And given that this is a website, that means I could go to your default page, type in the username or password box and delete your Access data. Or log in as any user without knowing their password...
Use parameterized queries at all times!

The second is "Never store passwords in clear text" - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

After that you need to start looking at what is happening - so either run this in the debugger and see what occurs (it's probably an exception, but unless you find out where it is you can;t even start to fix it). So try...catch blocks and logging are the order of the day, combined with using the debugger to follow what is actually happening.

But...why are you "brewing your own"?
Why not let the system handle it: Introduction to Membership[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900