输入数据(用户名和密码)

时间:2011-11-30 18:40:58

标签: c# asp.net

问题在于: 例如,如果我输入“A”作为用户名并输入“A”作为密码,则单击添加用户没有任何反应。但是如果我回去并且现在为用户名键入“B”并为该密码键入“B”并单击添加用户则添加前一个条目“A”& “A”进入表格。就像它背后一样?

我知道我有些不合适的地方。

    public partial class frmManageUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnAddUser_Click1(object sender, EventArgs e)
    {
                //string userName, userPassword;



        if (txtUserName.Text == "" || txtUserName.Text == null)
        {
            lblError.Text = ("User Name may not be empty");
            lblError.ForeColor = System.Drawing.Color.Red;
            return;
        }
       // else

           // userName = (txtUserName.Text);


        if (txtPassword.Text == "" || txtPassword.Text == null)
        {
            lblError.Text = ("Password may not be empty");
            lblError.ForeColor = System.Drawing.Color.Red;
            return;
        }
        //else
           // userPassword = (txtPassword.Text);

        //using (OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString"))
        OleDbConnection conn =  
    new OleDbConnection(ConfigurationManager.ConnectionStrings["PayrollSystem_DBConnectionString"].ConnectionString);

            {

                string insert = "Insert INTO tblUserLogin (UserName, UserPassword, SecurityLevel) Values (@UserName, @UserPassword, @SecurityLevel)";
                OleDbCommand cmd = new OleDbCommand(insert, conn);
                cmd.Parameters.Add("@UserName", txtUserName.Text);
                cmd.Parameters.Add("@UserPassword", txtPassword.Text);
                cmd.Parameters.Add("@SecurityLevel", drpdwnlstSecurityLevel.SelectedValue);
                conn.Open();
                cmd.ExecuteNonQuery();      
            }

        Session["UserName"] = txtUserName.Text;
        Session["Password"] = txtPassword.Text;
        Session["SecurityLevel"] = drpdwnlstSecurityLevel.SelectedValue;
        Server.Transfer("frmManageUsers.aspx");

        //Server.Transfer("grdUserLogin"); 

    }
    protected void drpdwnlstSecurityLevel_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

1 个答案:

答案 0 :(得分:5)

您的代码似乎缺少conn.Close()。这会导致写入表的延迟。另外,请考虑将conn放入using语句中以正确处理对象。