1

我的页面中有 2 个表格。我可以在这两者之间进行选择,以选择语言。

在我的代码中,我有这个:

    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;
        }
    }
}

它正在工作。在这两种语言中,用户都可以插入到数据库中。使用主要语言,用户甚至可以知道他是否已经回答。虽然当我选择第二种语言时,我无法获得if (!IsPostBack)

如何修复我的代码?

4

0 回答 0