CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2011
    Posts
    6

    login form code help usig ms access database

    hi i am new to c# and wondering if you could help me solve the problem to my login function for my website thanks

    Code:
     protected void btnlogin_Click(object sender, EventArgs e)
            {
                OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=CWsystem.mdb");
              //  OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["CWsystem.mdb"].ConnectionString);
                con.Open();
                string cmdStr="select count(*) from Person where UserID='" + txtusername.Text + "'";
                OleDbCommand Checkuser=new OleDbCommand(cmdStr, con);
                int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
                if(temp== 1)
                {
                        string cmdStr2="Select Password from Person where username='" + txtusername.Text + "'";
                    OleDbCommand pass=new OleDbCommand(cmdStr2, con);
                    string password=pass.ExecuteScalar().ToString();
                    con.Close();
    
                    if(password == txtpassword.Text)
                    {
                        Session["New"] = txtusername.Text;
                        Response.Redirect("university.aspx");
                    }
                    else
                    {
                        Label1.Visible = true;
                        Label1.Text = "invalid password";
                    }
                }
                    else
                    {
                        Label1.Visible = true;
                        Label1.Text = "invalid password";
                    }
    
                }
    
            protected void txtpassword_TextChanged(object sender, EventArgs e)
            {
    
            }
            }
        }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: login form code help usig ms access database

    Put a breakpoint on the line if(temp== 1) and start debugging the site by pressing F5. Then single-step through the code and look values of the variables. Are your queries returning what you expect?

  3. #3
    Join Date
    Aug 2011
    Posts
    6

    Re: login form code help usig ms access database

    Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\CWsystem.mdb'.

    don't seem to reconise my database

  4. #4
    Join Date
    Aug 2011
    Posts
    6

    Re: login form code help usig ms access database

    which happens at con.Open();

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: login form code help usig ms access database

    Is the database located at that location? Is the path valid?

    Keep in mind that your connection string is
    Code:
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=CWsystem.mdb");
    Which means it expects the CWSystem.mdb file to be located in the same file directory as your program .exe.

    If the cwsystem.mdb is listed as a file in the visual studio project, right click on it an choose properties. Change the 'Copy to output directory' property to 'Copy always'. One caveat to doing this is that any changes you make to your database won't be persisted between debug sessions of your application (because every time you compile and debug the app, a new copy of the mdb gets put in the output directory).
    Last edited by Arjay; May 9th, 2013 at 01:33 PM.

  6. #6
    Join Date
    Aug 2011
    Posts
    6

    Re: login form code help usig ms access database

    {"Object reference not set to an instance of an object."}

    OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\enterprise web develop\enterprise\CWsystem.mdb");

    this is the same directory as where the database is in the data_app section and the connection to the database is working

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: login form code help usig ms access database

    Quote Originally Posted by dekon View Post
    {"Object reference not set to an instance of an object."}
    Where does this error occur? have you single stepped through the code in a debugger to find the line that throws this error?

    Quote Originally Posted by dekon View Post
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\enterprise web develop\enterprise\CWsystem.mdb");
    You may have to escape the backslashes in this code as follows:
    Code:
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\\enterprise web develop\\enterprise\\CWsystem.mdb");

  8. #8
    Join Date
    Aug 2011
    Posts
    6

    Re: login form code help usig ms access database

    int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());

    data type mismatch

    and thanks was finally able to establish a connection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured