Esempio n. 1
0
    //Apagar
    protected void ButtonAP_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);

        con.Open();
        String     GetImagem        = "select Imagem from Produtos where Id_Produtos = @Id_Produtos";
        SqlCommand comandoGetImagem = new SqlCommand(GetImagem, con);

        comandoGetImagem.Parameters.AddWithValue("@Id_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
        string PathdaImagem = comandoGetImagem.ExecuteScalar().ToString();

        if ((System.IO.File.Exists(Server.MapPath(PathdaImagem))))
        {
            System.IO.File.Delete(Server.MapPath(PathdaImagem));
        }
        String     Apagarvariedades = "delete from ProdutosVariedades WHERE FKId_produtos = @FKId_Produtos";
        SqlCommand comando2         = new SqlCommand(Apagarvariedades, con);

        comando2.Parameters.AddWithValue("@FKId_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
        comando2.ExecuteNonQuery();
        String     Apagarproduto = "delete from Produtos WHERE Id_Produtos = @Id_Produtos";
        SqlCommand comando       = new SqlCommand(Apagarproduto, con);

        comando.Parameters.AddWithValue("@Id_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
        comando.ExecuteNonQuery();
        con.Close();
        GridViewIP.DataBind();
        GridViewIPV.DataBind();
        IPModo(1);
    }
Esempio n. 2
0
 //Editar
 protected void ButtonEP_Click(object sender, EventArgs e)
 {
     if (CheckBoxIPVariedade.Checked == false && DropDownListIPVariedades.Items.Count == 0)
     {
         ClientScript.RegisterStartupScript(this.GetType(), "Aviso", "alert('Impossível inserir, o seu Colectivo de Produtos contém variedades, no entanto a sua lista de variedades encontra-se vazia.');", true);
     }
     else
     {
         //Verificar que o diretório aonde a imagem será guardada existe, se não existir cria-se
         if (!Directory.Exists(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem))
         {
             Directory.CreateDirectory(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem);
         }
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);
         con.Open();
         //Verificar que não há um produto diferente na base de dados com o mesmo nome, marca e quantidade
         String     Procurarrepetidos = "select count(*) from Produtos where Nome = @Nome and Marca = @Marca and Quantidade = @Quantidade and Id_Produtos != @Id_Produtos";
         SqlCommand comando           = new SqlCommand(Procurarrepetidos, con);
         comando.Parameters.AddWithValue("@Nome", TextBoxIPNome.Text);
         comando.Parameters.AddWithValue("@Marca", DropDownListIPMarca.SelectedValue);
         comando.Parameters.AddWithValue("@Quantidade", TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue);
         comando.Parameters.AddWithValue("@Id_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
         int temp = Convert.ToInt32(comando.ExecuteScalar().ToString());
         if (temp > 0)
         {
             ClientScript.RegisterStartupScript(this.GetType(), "Aviso", "alert('Impossível editar, o Colectivo de Produtos que pretende editar já existe na base de dados sob um Id diferente. Já existe um Colectivo de Produtos com o mesmo Nome, Marca, Quantidade por Unidade mas Id diferentes.');", true);
         }
         else if (temp == 0)
         {
             //Obter o link da imagem do produto
             string filename = TextBoxIPNome.Text + " " + DropDownListIPMarca.SelectedItem + " " + TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue + " " + DropDownListIPCategoria.SelectedItem + " " + DropDownListIPSubcategoria.SelectedItem + ".png";
             filename = filename.Replace("/", "_");
             String     GetImagem        = "select Imagem from Produtos where Id_Produtos=@Id_Produtos";
             SqlCommand comandoGetImagem = new SqlCommand(GetImagem, con);
             comandoGetImagem.Parameters.AddWithValue("@Id_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
             string PathdaImagem = "";
             if (comandoGetImagem.ExecuteScalar() != null)
             {
                 PathdaImagem = comandoGetImagem.ExecuteScalar().ToString();
             }
             //Começar a preparar o query de atualização do produto
             String     EditarProduto        = "update Produtos set Nome = @Nome, Imagem = @Imagem, Marca = @Marca, Subcategoria = @Subcategoria, PesoUnidade = @PesoUnidade, Preco = @Preco, Quantidade = @Quantidade where Id_Produtos = @Id_Produtos";
             SqlCommand comandoEditarProduto = new SqlCommand(EditarProduto, con);
             comandoEditarProduto.Parameters.AddWithValue("@Id_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
             comandoEditarProduto.Parameters.AddWithValue("@Nome", TextBoxIPNome.Text);
             comandoEditarProduto.Parameters.AddWithValue("@Marca", DropDownListIPMarca.SelectedValue);
             comandoEditarProduto.Parameters.AddWithValue("@Subcategoria", DropDownListIPSubcategoria.SelectedValue);
             comandoEditarProduto.Parameters.AddWithValue("@PesoUnidade", RadioButtonListIPPesoouUnid.SelectedValue);
             comandoEditarProduto.Parameters.AddWithValue("@Preco", double.Parse(TextBoxIPPreco.Text));
             if (RadioButtonListIPPesoouUnid.SelectedValue == "0")
             {
                 comandoEditarProduto.Parameters.AddWithValue("@Quantidade", "");
             }
             else if (RadioButtonListIPPesoouUnid.SelectedValue == "1")
             {
                 comandoEditarProduto.Parameters.AddWithValue("@Quantidade", TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue);
             }
             //Verificar as diferentes possibilidades face à atualização da imagem
             //Caso a imagem seja editada
             if (FileUploadIPImagem.HasFile == true)
             {
                 if (!Directory.Exists(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem))
                 {
                     Directory.CreateDirectory(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem);
                 }
                 if ((System.IO.File.Exists(Server.MapPath(PathdaImagem))))
                 {
                     System.IO.File.Delete(Server.MapPath(PathdaImagem));
                 }
                 FileUploadIPImagem.SaveAs(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
                 comandoEditarProduto.Parameters.AddWithValue("@Imagem", "~/Imagens/Produtos/" + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
             }
             //Caso a imagem não seja editada, mas outros atributos do produto o tenham sido
             else if ("~/Imagens/Produtos/" + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename != PathdaImagem)
             {
                 //Se o novo diretório a que a imagem passará a pertencer não existir, cria-se o novo diretório, e tenta-se mover o ficheiro
                 //ao mover o ficheiro, se o link da imagem, guardado na base de dados, não corresponder a nenhuma imagem nos diretórios, o programa entra no catch e avisa de que a imagem não foi encontrada
                 if (!Directory.Exists(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem))
                 {
                     Directory.CreateDirectory(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem);
                 }
                 try
                 {
                     System.IO.File.Move(Server.MapPath(PathdaImagem), Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/ a mover " + filename);
                     System.IO.File.Move(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/ a mover " + filename, Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
                     comandoEditarProduto.Parameters.AddWithValue("@Imagem", "~/Imagens/Produtos/" + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
                 }
                 catch
                 {
                     ClientScript.RegisterStartupScript(this.GetType(), "Aviso", "alert('A imagem não foi encontrada. Note que não se devem modificar as imagens nos seus diretórios em (~/Imagens/Produtos/...).');", true);
                     comandoEditarProduto.Parameters.AddWithValue("@Imagem", "~/Imagens/Produtos/" + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
                 }
             }
             else
             {
                 comandoEditarProduto.Parameters.AddWithValue("@Imagem", PathdaImagem);
             }
             comandoEditarProduto.ExecuteNonQuery();
             //Após a atualização do produto, segue-se a atualização das variedades do produto
             String     Procurarvariedades = "select * from ProdutosVariedades where FKId_Produtos = @FKId_Produtos";
             SqlCommand comando2           = new SqlCommand(Procurarvariedades, con);
             comando2.Parameters.AddWithValue("@FKId_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
             SqlDataReader dr         = comando2.ExecuteReader();
             string[]      variedades = new string[0];
             while (dr.Read())
             {
                 Array.Resize(ref variedades, variedades.Length + 1);
                 variedades[variedades.Length - 1] = dr["variedade"].ToString();
             }
             dr.Close();
             for (int i = 0; i < variedades.Length; i++)
             {
                 if (!DropDownListIPVariedades.Items.Contains(new ListItem(variedades[i].ToString())))
                 {
                     String     Apagarvariedades        = "delete from ProdutosVariedades WHERE FKId_produtos = @FKId_Produtos and Variedade = @Variedade";
                     SqlCommand comandoApagarvariedades = new SqlCommand(Apagarvariedades, con);
                     comandoApagarvariedades.Parameters.AddWithValue("@FKId_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
                     comandoApagarvariedades.Parameters.AddWithValue("@Variedade", variedades[i].ToString());
                     comandoApagarvariedades.ExecuteNonQuery();
                 }
             }
             foreach (ListItem ltItem in DropDownListIPVariedades.Items)
             {
                 if (!variedades.Contains(ltItem.Text))
                 {
                     String     Inserirvariedades        = "insert into ProdutosVariedades (FKId_Produtos,Variedade,Inventario) values (@FKId_Produtos,@Variedade,@Inventario)";
                     SqlCommand comandoInserirvariedades = new SqlCommand(Inserirvariedades, con);
                     comandoInserirvariedades.Parameters.AddWithValue("@FKId_Produtos", GridViewIP.SelectedRow.Cells[1].Text);
                     comandoInserirvariedades.Parameters.AddWithValue("@Variedade", ltItem.Text);
                     comandoInserirvariedades.Parameters.AddWithValue("@Inventario", 200);
                     comandoInserirvariedades.ExecuteNonQuery();
                 }
             }
             con.Close();
             GridViewIP.DataBind();
             GridViewIPV.DataBind();
         }
     }
 }
Esempio n. 3
0
 //Inserir
 protected void ButtonIP_Click(object sender, EventArgs e)
 {
     if (CheckBoxIPVariedade.Checked == false && DropDownListIPVariedades.Items.Count == 0)
     {
         ClientScript.RegisterStartupScript(this.GetType(), "Aviso", "alert('Impossível inserir, o seu Colectivo de Produtos contém variedades, no entanto a sua lista de variedades encontra-se vazia.');", true);
     }
     else
     {
         string filename = TextBoxIPNome.Text + " " + DropDownListIPMarca.SelectedItem + " " + TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue + " " + DropDownListIPCategoria.SelectedItem + " " + DropDownListIPSubcategoria.SelectedItem + ".png";
         filename = filename.Replace("/", "_");
         //Se o diretório não existir, cria-se
         if (!Directory.Exists(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem))
         {
             Directory.CreateDirectory(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem);
         }
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);
         con.Open();
         String     Procurarrepetidos = "select count(*) from Produtos where Nome = @Nome and Marca = @Marca and Quantidade = @Quantidade";
         SqlCommand comando           = new SqlCommand(Procurarrepetidos, con);
         comando.Parameters.AddWithValue("@Nome", TextBoxIPNome.Text);
         comando.Parameters.AddWithValue("@Marca", DropDownListIPMarca.SelectedValue);
         comando.Parameters.AddWithValue("@Quantidade", TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue);
         int temp = Convert.ToInt32(comando.ExecuteScalar().ToString());
         if (temp > 0)
         {
             ClientScript.RegisterStartupScript(this.GetType(), "Aviso", "alert('Impossível inserir, o Colectivo de Produtos que pretende inserir já existe na base de dados. Já existe um Colectivo de Produtos com o mesmo Nome, Marca e Quantidade por Unidade.');", true);
         }
         else if (temp == 0)
         {
             Guid       newGUID        = Guid.NewGuid();
             String     Inserirproduto = "insert into Produtos (Nome,Marca,Imagem,Subcategoria,PesoUnidade,Preco,Quantidade,DisponivelOnline) values (@Nome,@Marca,@Imagem,@Subcategoria,@PesoUnidade,@Preco,@Quantidade,@DisponivelOnline)";
             SqlCommand comando2       = new SqlCommand(Inserirproduto, con);
             comando2.Parameters.AddWithValue("@Nome", TextBoxIPNome.Text);
             comando2.Parameters.AddWithValue("@Marca", DropDownListIPMarca.SelectedValue);
             comando2.Parameters.AddWithValue("@Imagem", "~/Imagens/Produtos/" + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
             comando2.Parameters.AddWithValue("@Subcategoria", DropDownListIPSubcategoria.SelectedValue);
             comando2.Parameters.AddWithValue("@PesoUnidade", RadioButtonListIPPesoouUnid.SelectedValue);
             comando2.Parameters.AddWithValue("@Preco", TextBoxIPPreco.Text);
             if (RadioButtonListIPPesoouUnid.SelectedValue == "0")
             {
                 comando2.Parameters.AddWithValue("@Quantidade", "");
             }
             else if (RadioButtonListIPPesoouUnid.SelectedValue == "1")
             {
                 comando2.Parameters.AddWithValue("@Quantidade", TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue);
             }
             comando2.Parameters.AddWithValue("@DisponivelOnline", true);
             comando2.ExecuteNonQuery();
             String     GetIdProdutos = "select Id_Produtos from Produtos where Nome = @Nome and Marca = @Marca and Imagem = @Imagem and Subcategoria = @Subcategoria and PesoUnidade = @PesoUnidade and Preco = @Preco and Quantidade = @Quantidade";
             SqlCommand comando3      = new SqlCommand(GetIdProdutos, con);
             comando3.Parameters.AddWithValue("@Nome", TextBoxIPNome.Text);
             comando3.Parameters.AddWithValue("@Marca", DropDownListIPMarca.SelectedValue);
             comando3.Parameters.AddWithValue("@Imagem", "~/Imagens/Produtos/" + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
             comando3.Parameters.AddWithValue("@Subcategoria", DropDownListIPSubcategoria.SelectedValue);
             comando3.Parameters.AddWithValue("@PesoUnidade", RadioButtonListIPPesoouUnid.SelectedValue);
             comando3.Parameters.AddWithValue("@Preco", TextBoxIPPreco.Text);
             if (RadioButtonListIPPesoouUnid.SelectedValue == "0")
             {
                 comando3.Parameters.AddWithValue("@Quantidade", "");
             }
             else if (RadioButtonListIPPesoouUnid.SelectedValue == "1")
             {
                 comando3.Parameters.AddWithValue("@Quantidade", TextBoxIPQuantidade.Text + DropDownListIPQuantidade.SelectedValue);
             }
             int        IdProdutos        = Convert.ToInt32(comando3.ExecuteScalar().ToString());
             String     Inserirvariedades = "insert into ProdutosVariedades (FKId_Produtos,Variedade,Inventario) values (@FKId_Produtos,@Variedade,@Inventario)";
             SqlCommand comando4          = new SqlCommand(Inserirvariedades, con);
             comando4.Parameters.AddWithValue("@FKId_Produtos", IdProdutos);
             comando4.Parameters.AddWithValue("@Inventario", 200);
             if (CheckBoxIPVariedade.Checked == false)
             {
                 foreach (ListItem ltItem in DropDownListIPVariedades.Items)
                 {
                     if (comando4.Parameters.Contains("@Variedade"))
                     {
                         comando4.Parameters.RemoveAt("@Variedade");
                     }
                     comando4.Parameters.AddWithValue("@Variedade", ltItem.Text);
                     comando4.ExecuteNonQuery();
                 }
             }
             else if (CheckBoxIPVariedade.Checked == true)
             {
                 comando4.Parameters.AddWithValue("@Variedade", "Sem variedades");
                 comando4.ExecuteNonQuery();
             }
             if ((System.IO.File.Exists(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename)))
             {
                 System.IO.File.Delete(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
             }
             FileUploadIPImagem.SaveAs(Server.MapPath("~/Imagens/Produtos/") + DropDownListIPCategoria.SelectedItem + "/" + DropDownListIPSubcategoria.SelectedItem + "/" + DropDownListIPMarca.SelectedItem + "/" + filename);
             GridViewIP.DataBind();
             GridViewIPV.DataBind();
             IPModo(1);
             GridViewIP.PageIndex  = GridViewIP.PageCount - 1;
             GridViewIPV.PageIndex = GridViewIPV.PageCount - 1;
         }
         con.Close();
     }
 }