protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Garagens garagem = new Garagens();

            lblConsDataRetirada.Text = Session["consDataRetirada"].ToString();
            lblConsDataEntrega.Text = Session["consDataEntrega"].ToString();
            lblConsGaragemRetirada.Text = garagem.GetNomeGaragem(Convert.ToInt32(Session["ConsGaragemRetirada"]));
            lblConsGaragemEntrega.Text = garagem.GetNomeGaragem(Convert.ToInt32(Session["ConsGaragemEntrega"]));

            SqlConnection sqlConnection1 = new SqlConnection("Data Source=ROCKENBACH-NOTE\\SQLEXPRESS;Initial Catalog=ZipCar;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            cmd.CommandText =  "select c.* from carro c inner join carro_has_garagem cg on c.idCarro = cg.idCarro " +
                " where cg.idGaragem = "  + Session["ConsGaragemRetirada"].ToString() +
                " and not exists (select 1 from cliente_aluga_carro ac where c.idCarro = ac.idCarro and ac.dataHoraDevolvido is null)";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

            reader = cmd.ExecuteReader();

            gridCarros.DataSource = reader;
            gridCarros.DataBind();

        }
    }
    private void CarregaGaragem()
    {
        Garagens garagemRetirada = new Garagens();

        dropGaragemRetirada.DataTextField = "nome";
        dropGaragemRetirada.DataValueField = "idGaragem";

        dropGaragemRetirada.DataSource = garagemRetirada.GetAllGaragens();
        dropGaragemRetirada.DataBind();

        Garagens garagemEntrega = new Garagens();

        dropGaragemEntrega.DataTextField = "nome";
        dropGaragemEntrega.DataValueField = "idGaragem";

        dropGaragemEntrega.DataSource = garagemEntrega.GetAllGaragens();
        dropGaragemEntrega.DataBind();
    }
    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        if (txtNome.Text.Equals(string.Empty))
        {
            txtNome.Focus();
            return;
        }

        Garagens Garagens = new Garagens();

        try
        {
            Garagens.GetDadosGaragem(txtNome.Text.ToString());

            PopulaDadosTela(Garagens);
            Session["novaGaragem"] = false;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void CarregaGaragem()
    {
        Garagens garagem = new Garagens();

        ddlGaragem.DataTextField = "nome";
        ddlGaragem.DataValueField = "idGaragem";

        ddlGaragem.DataSource = garagem.GetAllGaragens();

        ddlGaragem.DataBind();

        ddlGaragem.Items.Insert(0, new ListItem("Selecione",(ddlGaragem.Items.Count + 1).ToString()));

        ddlGaragem.SelectedValue = (ddlGaragem.Items.Count + 1).ToString();
    }
    private void InserirRegistro()
    {
        Garagens novo = new Garagens();

        novo = PopulaDadosGaragem(novo);

        novo.Inserir();
    }
    private void Alterar()
    {
        Garagens alterar = new Garagens();

        alterar = PopulaDadosGaragem(alterar);

        alterar.Alterar();
    }
    protected void PopulaDadosTela(Garagens novo)
    {
        idGaragem = novo.IdGaragem;

        txtNome.Text = novo.Nome;

        txtCidade.Text = novo.Cidade;
        txtUf.Text = novo.UF;
        txtLogradouro.Text = novo.Logradouro;
        txtNumero.Text = novo.Num.ToString();
        txtComplemento.Text = novo.Complemento;
        txtBairro.Text = novo.Bairro;
        txtCEP.Text = novo.Cep;
        txtCapacidade.Text = novo.Capacidade.ToString();
        txtLatitude.Text = novo.Latitude;
        txtLongitude.Text = novo.Longitude;
    }
    protected Garagens PopulaDadosGaragem(Garagens novo)
    {
        if ((bool)Session["novaGaragem"] == true)
        {
            novo.IdGaragem = Garagens.BuscaMaxIdGaragem();
        }
        else
        {
            novo.IdGaragem = idGaragem;
        }
        novo.Nome = txtNome.Text;
        novo.Cidade = txtCidade.Text;
        novo.UF = txtUf.Text;
        novo.Logradouro = txtLogradouro.Text;
        novo.Num = Convert.ToInt32(txtNumero.Text);
        novo.Complemento = txtComplemento.Text;
        novo.Bairro = txtBairro.Text;
        novo.Cep = txtCEP.Text;
        novo.Capacidade = Convert.ToInt32(txtCapacidade.Text);
        novo.Latitude = txtLatitude.Text;
        novo.Longitude = txtLongitude.Text;

        return novo;
    }