lundi 27 juillet 2015

IF - Else username existence

I have this in my code behind

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 username=@username", Conn);
                myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name);
                int totalRegistos = (int)myCommand.ExecuteScalar();
                if (totalRegistos > 0)
                {
                        //    Already added
                        lblInfo0.Text = "You already answered";
                }
                else
                {
                        //    to be added
                        string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)";
                        {
                                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;
                        }
                }
        }
}

when i debbug doesn't show me any error. However, since I already exist in database, everytime i debbug doesn't say that. What i'm doing wrong? the purpose is if i already answered say that.

Aucun commentaire:

Enregistrer un commentaire