2015-07-20 114 views
1

之前就存在我有这样的代码背后:检查用户名按提交按钮

protected void cmdSave_Click(object sender, EventArgs e) 
{ 

    string sFilePath = Server.MapPath("Database3.accdb"); 
    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;"); 
    using (Conn) 
    { 
     Conn.Open(); 
     OleDbCommand myCommand = new OleDbCommand("SELECT COUNT(*) FROM colaborador WHERE [email protected]", Conn); 
     myCommand.Parameters.Add("?", OleDbType.VarChar).Value = HttpContext.Current.User.Identity.Name; 
     int totalRegistos = (int)myCommand.ExecuteScalar(); 
     if (totalRegistos > 0) 
     { 
      //  user already answered 
      lblInfo0.Text = "The user already asnwered"; 
     } 
     else 
     { 
      //  the user didn't asnwered 

      string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)"; 
       // insere na tabela colaborador os campos empresa, empres2, user os valores @ 
      { 
       OleDbCommand myCommand2 = new OleDbCommand(insertCmd, Conn); 
       myCommand2.Parameters.AddWithValue("@Empresa", empresa.Text); 
       myCommand2.Parameters.AddWithValue("@Empresa2", empresa2.Text); 
       myCommand2.Parameters.AddWithValue("@Telemovel", telemovel.Text); 
       myCommand2.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name); 
       Response.Write(myCommand.ExecuteNonQuery()); 
       lblInfo.Text = "Data saved!"; 
       lblInfo.ForeColor = System.Drawing.Color.Green; 
      } 
     } 
    } 
} 

这个工作正常,没有错误,并保存到数据库此外,如果用户名存在说的消息“用户已经回答了”

但是我需要按提交按钮。

有什么办法可以说出这个消息(如果用户名已经存在)之前字段text.box?我怎样才能改变我的代码来做到这一点?

+0

通过编写代码? https://msdn.microsoft.com/en-us/library/5011f09h.aspx –

+0

嗯.. http://stackoverflow.com/questions/31452903/verify-if-username-already-answered-and-display-database -aspc和http://stackoverflow.com/questions/31515456/verify-if-username-exist-to-show-the-database-or-sho-table-to-field和http://stackoverflow.com/questions/31519336/if-username-not-exist-insert-into-else-update-display-info –

+0

@SonerGönül哈哈...目前为止还没有幸运......不能工作...... :( – KikoFHM

回答

0
if (!IsPostBack) 
{ 
    string sFilePath = Server.MapPath("Database3.accdb"); 
    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;"); 
    using (Conn) 
    { 
     Conn.Open(); 
     OleDbCommand myCommand = new OleDbCommand("SELECT COUNT(*) FROM colaborador WHERE [email protected]", Conn); 
     myCommand.Parameters.Add("?", OleDbType.VarChar).Value = HttpContext.Current.User.Identity.Name; 
     int totalRegistos = (int)myCommand.ExecuteScalar(); 
     if (totalRegistos > 0) 
     { 
       // Já registado 
       lblInfo0.Text = "O username já existe na base de dados"; 

       empresa.Enabled = false; 
       empresa2.Enabled = false; 
       telemovel.Enabled = false; 
       cmdSave.Visible = false; 
     } 
    } 
}