2015-07-20 155 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; 
     } 
    } 
}