I tried to establish a login page using Asp.net. It seems pretty easy to complement this function. I added two textboxes and a submit button in the web page. After inputting the username and password and clicking the login button, system will send the data to the database. Those data will check the login table in the database and then reply the match rows' number which is used to determine the authorization. The codes are below:
protected void loginButton_Click(object sender, EventArgs e)
{
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source= C:\\Documents and Settings\\Administrator\\桌面\\study temp\\dataset\\dataset\\testdb.mdb;";
string select = "select count(*) from loginTable where username = '" + username.Text + "' and password = '" + password.Text + "'";
OleDbCommand command = new OleDbCommand(select);
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
command.Connection = conn;
string count = command.ExecuteScalar().ToString();
conn.Close();
if (count == "1")
{
Response.Redirect("Default.aspx");
}
else
{
verification.Text = "invalid username or password";
}
}
Although the function is realized, I think it's better to create a new event which includes a cancelEventArgs to cancel the event when the username or password is invalided. I will try to improve the code in recent days.
Tuesday, April 7, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment