1

I want to make a login page with an Access database for a school project with asp.net. I dont have really experience with C# so im doing different tutorials.

protected void Login1_Click(object sender, EventArgs e)
{
    string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=http://tmti-16.ict-lab.nl/database/ek2012.mdb";
    string query = "Select Count(*) From users Where username = ? And userpassword = ?";
    int result = 0;
    using (OleDbConnection conn = new OleDbConnection(connect))
    {
        using (OleDbCommand cmd = new OleDbCommand(query, conn))
        {
            cmd.Parameters.AddWithValue("", UserName.Text);
            cmd.Parameters.AddWithValue("", Password.Text);
            conn.Open();
            Session["User"] = UserName.Text;
            result = (int)cmd.ExecuteScalar();
        }
    }
    if (result > 0)
    {
        Response.Redirect("index.aspx");
    }
    else
    {
        Literal1.Text = "Invalid credentials";
    }
}

In Access I have the table 'users' with the 'username' and 'userpassword' rows.

dash
  • 89,546
  • 4
  • 51
  • 71
kay
  • 21
  • 1
  • 4
  • 1
    What is your **problem**?? also you should add the **homework** tag to your question – huMpty duMpty Mar 29 '12 at 10:40
  • Well I cant get a connection with it. I get the following error: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. – kay Mar 29 '12 at 10:48
  • Your connection string should reference a local path for mdb files. If it's an asp.net project, you can put your access database mdb file into the App_Data folder and your connection string data source section becomes Data Source=|DataDirectory|ek2012.mdb; – dash Mar 29 '12 at 10:49
  • Yes, its working! Thanks for helping. Now practicing more c# and looking in future for MVC 3 developing. I'm still consider to learn PHP or C# first :-) – kay Mar 29 '12 at 11:07
  • Hi @Dash any chance of posting that as an answer? – Fionnuala Mar 29 '12 at 11:40
  • @Remou - was planning to - just busy :-) See Below. – dash Mar 29 '12 at 15:22

1 Answers1

3

The problem you are encountering is almost certainly to do with the data source segment of your connection string:

string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=http://tmti-16.ict-lab.nl/database/ek2012.mdb"

The Access mdb file should be on a local path. This can be anywhere on your local file system that the account your web application is running as has permission to access, but, usefully, ASP.Net actually has a better place to put these files, the App_Data folder.

If you place your mdb file in this folder, your connection string would then become:

string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|ek2012.mdb"

The contents of the App_Data folder will not be served to clients so this is a secure place to put your data. It's also a good idea to use it as it keeps the data with the project; often, people will put data related files in a file system folder outside of the web root, which means you then have to remember this dependency when moving the site to another computer.

A common issue you might have with accessing files in App_Data is usually related to permissioning; the user you are running the code as will need read and write permissions in this directory in order to modify the mdb file. This is covered in the section "Configuring Permissions for an Access Database" in this MSDN Article.

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71