1

I have 2 tables in my page. I can select between those 2, to choice the language.

In my code I have this:

    public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        username.Text = "[" + HttpContext.Current.User.Identity.Name + "]"; // identifica o user ligado
        useren.Text = "[" + HttpContext.Current.User.Identity.Name + "]";

        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 username=@username", 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";
                        Label1.Text = "You already answered before";
                        empresa.Enabled = false;
                        empresa2.Enabled = false;
                        telemovel.Enabled = false;
                        cmdSave.Visible = false;
                        business.Enabled = false;
                        business2.Enabled = false;
                        mobile.Enabled = false;
                        cmdSave.Visible = false;
                }
            }
        }



    }

    // gravar na base de dados PT
    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;");
        string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)";
        using (Conn)     // insere na tabela colaborador os campos empresa, empres2, user os valores @ 
        {
            Conn.Open();
            OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
            myCommand.Parameters.AddWithValue("@Empresa", empresa.Text);
            myCommand.Parameters.AddWithValue("@Empresa2", empresa2.Text);
            myCommand.Parameters.AddWithValue("@Telemovel", telemovel.Text);
            myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name);
            Response.Write(" ");
            myCommand.ExecuteNonQuery();
            lblInfo.Text = "Dados guardados!";
            lblInfo.ForeColor = System.Drawing.Color.Green;
        }
    }  




    // gravar na base de dados EN
    protected void cmdSave_Click2(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;");
        string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@useren)";
        using (Conn)
        {
            Conn.Open();
            OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
            myCommand.Parameters.AddWithValue("@Empresa", business.Text);
            myCommand.Parameters.AddWithValue("@Empresa2", business2.Text);
            myCommand.Parameters.AddWithValue("@Telemovel", mobile.Text);
            myCommand.Parameters.AddWithValue("@useren", HttpContext.Current.User.Identity.Name); 
            myCommand.ExecuteNonQuery(); 
            Label1.Text = "Saved Successfull!";
            Label1.ForeColor = System.Drawing.Color.Green;
        }
    }
}

It's working. In both language the user can do the insert into the db. With the main language the user can even know if he already answered. Although when I select the 2nd language I can't get the if (!IsPostBack)

How can I fix my code?

Magnus
  • 45,362
  • 8
  • 80
  • 118
KikoFHM
  • 122
  • 14
  • 2
    What is it that you Can not get? The value of ispostback? So what actually happen? – Rasmus Damgaard Nielsen Jul 28 '15 at 08:29
  • By any chance does changing the language cause a postback? – Paddy Jul 28 '15 at 08:33
  • I already fixed. Don't do anything.... just reboot the PC when i debug again everything it's fine.... weird s*** :S – KikoFHM Jul 28 '15 at 08:44
  • In one language it says the user already answered and deactivate the text.box to prevent field again. But when i select another language should react the same way... but it's not. It don't detect the user already answered and the user can field de text.box over and over .... – KikoFHM Jul 28 '15 at 08:47

0 Answers0