1

ページに 2 つのテーブルがあります。言語を選択するために、これら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;
        }
    }
}

それは働いています。どちらの言語でも、ユーザーはデータベースに挿入できます。メイン言語を使用すると、ユーザーはすでに回答したかどうかさえ知ることができます。2番目の言語を選択すると取得できませんがif (!IsPostBack)

コードを修正するにはどうすればよいですか?

4

0 に答える 0