且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.net中的登录表单验证

更新时间:2023-12-04 12:26:58

创建登录表单.添加用户名和密码文本框.
在后面的代码(login.aspx.cs文件)中的登录按钮click事件中执行以下操作
Create a Login form. Add User Name and Password text box.
In code behind(login.aspx.cs file) login button click event do the following
SqlConnection con=new SqlConnection("Your Connection String");

if (con.State == ConnectionState.Open)
  {
    qry = "";
    qry = "select Password,UserID from tblUsers where UserID= ''" +      txtUserName.Text.Trim().Replace("''", "''''") + "'' and Password=''" + txtPassword.Text.Trim().Replace("''", "''''") + "''";
    SqlCommand cmd = new SqlCommand (qry, con);
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        //redirect to other page
    }
    else
    {
      MessageBox.Show("Please check User Name and Password");
      txtUserName.Focus();
    }
 }