protected void GridViewImoveis_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Conexao con = new Conexao();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dtResultado = null;
            cmd.CommandText = @"select i.codImovel, f.codFotosImoveis, f.nomeFoto, t.tipo, i.valorVenda,
                                i.quadra + i.lote  as Endereco, s.situacao
                                from tbimovel i
                                inner join tbtipoimovel t on i.codTipoImovel = t.codTipoImovel
                                inner join tbfotosimoveis f on i.codImovel = f.codImovel
                                inner join tbsituacaoimovel s on i.codSituacaoImovel = i.codSituacaoImovel
                                where t.codTipoImovel=@codTipoImovel and i.codSituacaoImovel=@situacaoImovel";
            cmd.Parameters.AddWithValue("@codTipoImovel", ddlTipo.SelectedIndex);
            cmd.Parameters.AddWithValue("@situacaoImovel", ddlSituacao.SelectedIndex);
            dtResultado = con.Consultar(cmd);

            GridViewImoveis.DataSource = dtResultado;
            GridViewImoveis.DataBind();

            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Image status = (Image)e.Row.Cells[2].FindControl("FotoImovel");

                    status.ImageUrl = Convert.ToInt32(e.Row.Cells[1].Text.Trim()) == 0 ?
                      "~/imagens/casaPadrao.png" : "fotos/" + e.Row.Cells[1].Text.Trim() + ".jpg";
                    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor= '#75AF74'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor= '#FFFFFF'");
                }
                e.Row.Cells[0].Visible = false; // codImovel
                e.Row.Cells[1].Visible = false; // codFoto
            }
        }
        protected void btnCadastrar_Click(object sender, EventArgs e)
        {
            Conexao con = new Conexao();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt = null;

            try
            {

                cmd.CommandText = @"SELECT cpf FROM tbPessoaFisica WHERE cpf LIKE @cpf";
                cmd.Parameters.AddWithValue("@cpf", txtCpf.Text);

                dt = con.Consultar(cmd);

                if (dt.Rows.Count == 0)
                {
                    cmd.Parameters.Clear();
                    cmd.CommandText = @"INSERT INTO tbPessoa (codTipoPessoa, codTipoUsuario, nome,
                                    dtCadastro, username, password,
                                    telFixo, telCel)
                                    VALUES (@codTipoPessoa, @codTipoUsuario, @nome,
                                            NOW(), @username, @password,
                                            @telFixo, @telCel)";

                    cmd.Parameters.AddWithValue("@codTipoPessoa", 1);
                    cmd.Parameters.AddWithValue("@codTipoUsuario", 3);
                    cmd.Parameters.AddWithValue("@nome", txtNome.Text);
                    cmd.Parameters.AddWithValue("@username", txtUsuario.Text);
                    cmd.Parameters.AddWithValue("@password", txtSenha1.Text);
                    cmd.Parameters.AddWithValue("@telFixo", txtTelFixo.Text);
                    cmd.Parameters.AddWithValue("@telCel", txtTelCel.Text);
                    con.Executar(cmd);

                    cmd.Parameters.Clear();

                    cmd.CommandText = @"INSERT INTO tbPessoaFisica (cpf, codPessoa, dtNascimento, sexo, estadoCivil)
                                        VALUES(@cpf, LAST_INSERT_ID(), @dtNascimento, @sexo,
                                        @estadoCivil)";
                    cmd.Parameters.AddWithValue("@cpf", txtCpf.Text);
                    cmd.Parameters.AddWithValue("@dtNascimento", Convert.ToDateTime(txtDtNascimento.Text));
                    cmd.Parameters.AddWithValue("@sexo", ddlSexo.SelectedValue);
                    cmd.Parameters.AddWithValue("@estadoCivil", ddlEstadoCivil.SelectedValue);

                    con.Executar(cmd);
                    lbMensagem.Text = "Cadastro efetuado com sucesso!";
                }
                else
                {
                    lbMensagem.Text = "Tentativa de cadastro duplicado!";
                }
            }
            catch (Exception ex)
            {
                lbMensagem.Text = ex.Message;
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try {
                Conexao con = new Conexao();
                MySqlCommand cmd = new MySqlCommand();
                DataTable dtResultado = null;
                cmd.CommandText = @"SELECT p.codTipoUsuario, p.codPessoa FROM tbpessoa p
                                WHERE username LIKE @username AND password LIKE @password";
                cmd.Parameters.AddWithValue("@username", txtUsuario.Text);
                cmd.Parameters.AddWithValue("@password", txtSenha.Text);

                dtResultado = con.Consultar(cmd);
                if (dtResultado.Rows.Count > 0)
                {
                    int tipoUser = (int)dtResultado.Rows[0]["codTipoUsuario"];
                    Session["codPessoa"] = dtResultado.Rows[0]["codPessoa"];
                    Session["codTipoUsuario"] = tipoUser;
                    //Session.Timeout = 1;

                    cmd.Parameters.Clear();
                    cmd.CommandText = @"UPDATE tbpessoa SET dtUltimoAcesso = NOW() WHERE codPessoa = @codPessoa";
                    cmd.Parameters.AddWithValue("@codPessoa",Session["codPessoa"]);
                    con.Executar(cmd);

                    if (tipoUser == 1)
                        Response.Redirect("~/frmInicioCliente.aspx");
                    else if(tipoUser == 3)
                        Response.Redirect("~/frmInicioVendedor.aspx");
                    else
                        Response.Redirect("~/frmInicioGeral.aspx");
                }
                else
                {
                    lbMensagem.Text = "Usuário Inválido";
                    txtUsuario.Text = string.Empty;
                    txtSenha.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {
                lbMensagem.Text = ex.Message;
            }
        }
 private void PreencheDropDownList()
 {
     try
     {
         Conexao con = new Conexao();
         MySqlCommand cmd = new MySqlCommand();
         DataTable dtResultado = null;
         cmd.CommandText = @"select p.codPessoa as CodigoPessoa, p.nome as Nome, f.cpf as CPF, p.username as Usuario , p.dtUltimoAcesso as UltimoAcesso
                         from tbpessoa p inner join tbpessoafisica f on p.codPessoa = f.codPessoa
                         where p.codTipoUsuario = 3;";
         dtResultado = con.Consultar(cmd);
         GridView_Funcionario.DataSource = dtResultado;
         GridView_Funcionario.DataBind();
     }
     catch (Exception ex)
     {
         lbMensagem.Text = ex.Message;
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            int iSessao = Convert.ToInt32(Session["p.codImovel"]);

            Conexao con = new Conexao();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dtResultado = null;
            cmd.CommandText = @"select t.tipo, s.situacao, i.valorVenda,
                                'Quadra: ' + i.quadra + 'Lote:' + i.lote as endereco,
                                i.areaLargura, i.areaConstruida, i.codProprietario
                                from tbimovel i
                                inner join tbtipoimovel t on t.codTipoImovel = i.codTipoImovel
                                inner join tbSituacaoImovel s on i.codSituacaoImovel = s.codSituacaoImovel
                                inner join tbEndereco e on e.codTipoEndereco = i.codImovel
                                inner join tbPessoa p on p.codpessoa= i.codProprietario
                                where i.codImovel = @codImovel";
            cmd.Parameters.AddWithValue("codImovel", iSessao);
            dtResultado = con.Consultar(cmd);

            lbAreaConstrucao.Text = dtResultado.Rows[0]["areaConstruida"].ToString();
        }
        protected void btBuscarCNPJ_Click(object sender, EventArgs e)
        {
            Conexao con = new Conexao();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt = null;

            try
            {
                cmd.CommandText = @"SELECT * FROM tbPessoa p INNER JOIN tbPessoaJuridica pj
                                    ON p.codPessoa = pj.codPessoa
                                    WHERE pj.cnpj LIKE @cnpj";
                cmd.Parameters.AddWithValue("@cnpj", txtCnpj.Text);
                dt = con.Consultar(cmd);
                lbCodPessoa.Text = dt.Rows[0]["codPessoa"].ToString();
                lbInscricaoEst.Text = dt.Rows[0]["inscricaoEstadual"].ToString();
                lbNomeFantasia.Text = dt.Rows[0]["nomeFantasia"].ToString();
                lbRazaoSocial.Text = dt.Rows[0]["razaoSocial"].ToString();
            }
            catch (Exception ex)
            {
                lblMensagem.Text = ex.Message;
            }
        }
        protected void btBuscarCPF_Click(object sender, EventArgs e)
        {
            Conexao con = new Conexao();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt = null;

            try
            {
                cmd.CommandText = @"SELECT * FROM tbPessoa p INNER JOIN tbPessoaFisica pf
                                    ON p.codPessoa = pf.codPessoa
                                    WHERE pf.cpf LIKE @cpf";
                cmd.Parameters.AddWithValue("@cpf", txtCpf.Text);
                dt = con.Consultar(cmd);
                lbCodPessoa.Text = dt.Rows[0]["codPessoa"].ToString();
                lbNome.Text = dt.Rows[0]["nome"].ToString();
                lbDtNascimento.Text = dt.Rows[0]["dtNascimento"].ToString();
                lbSexo.Text = dt.Rows[0]["sexo"].ToString();

            }
            catch (Exception ex)
            {
                lblMensagem.Text = ex.Message;
            }
        }
        protected void btSalvar_Click(object sender, EventArgs e)
        {
            Conexao con = new Conexao();
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt = null;

            try
            {
                if (rbCasa.Checked)
                {
                    cmd.CommandText = @"INSERT INTO tbimovel (codProprietario, codTipoImovel, codSituacaoImovel, quadra, lote,
                                        areaConstruida, areaLargura, areaComprimento, valorVenda, valorComissao)
                                        VALUES(@codProprietario, @codTipoImovel, @codSituacaoImovel, @quadra, @lote,
                                        @areaConstruida, @areaLargura, @areaComprimento, @valorVenda, @valorComissao)";
                    cmd.Parameters.AddWithValue("@areaConstruida", txtAreaConstrucao.Text);
                }
                else
                {
                    cmd.CommandText = @"INSERT INTO tbimovel (codProprietario, codTipoImovel, codSituacaoImovel, quadra, lote,
                                        areaLargura, areaComprimento, valorVenda, valorComissao)
                                        VALUES(@codProprietario, @codTipoImovel, @codSituacaoImovel, @quadra, @lote,
                                        @areaLargura, @areaComprimento, @valorVenda, @valorComissao)";
                }

                cmd.Parameters.AddWithValue("@codProprietario", lbCodPessoa.Text);
                if(rbCasa.Checked)
                    cmd.Parameters.AddWithValue("@codTipoImovel", 1);
                else
                    cmd.Parameters.AddWithValue("@codTipoImovel", 2);
                cmd.Parameters.AddWithValue("@codSituacaoImovel", 1);
                cmd.Parameters.AddWithValue("@quadra", txtQuadra.Text);
                cmd.Parameters.AddWithValue("@lote", txtLote.Text);
                cmd.Parameters.AddWithValue("@areaLargura", txtAreaLargura.Text);
                cmd.Parameters.AddWithValue("@areaComprimento", txtAreaComprimento.Text);
                cmd.Parameters.AddWithValue("@valorVenda", txtValorImovel.Text);
                cmd.Parameters.AddWithValue("@valorComissao", txtComissao.Text);
                con.Executar(cmd);

                lbMensagem.Text = "Cadastro efetuado com sucesso!";
                LimpaCampos();

            }
            catch (Exception ex)
            {
                lbMensagem.Text = ex.Message;
            }
        }