-
May 8th, 2013, 12:57 PM
#1
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)
{
}
}
}
-
May 8th, 2013, 08:19 PM
#2
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?
-
May 9th, 2013, 06:22 AM
#3
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
-
May 9th, 2013, 06:37 AM
#4
Re: login form code help usig ms access database
which happens at con.Open();
-
May 9th, 2013, 01:26 PM
#5
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.
-
May 10th, 2013, 04:57 AM
#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
-
May 10th, 2013, 12:11 PM
#7
Re: login form code help usig ms access database
Originally Posted by dekon
{"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?
Originally Posted by dekon
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");
-
May 10th, 2013, 02:34 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|