0
Good morning,
I am trying to validate a situation that I explain, for example we added a category "Miscellaneous", and this same category is used in the product "Keys", let’s imagine that we will delete the category "Parts", the product "Keys" with category "Miscellaneous" remains.
What I really want to validate is if we’re going to delete a category, check that that same category is not in use in any product.
What I tried was the following in the Stored Procedure:
SELECT Categoria FROM Categorias WHERE (Categoria) NOT IN (SELECT Categoria FROM Produtos);
Code C#:
if (BLL.BD.backoffice.VerificaCatgProdutoUso(Categoria).Rows.Count != 0)
{
lblCatgsInfo.Text = "Categoria " + Convert.ToString(selectedRow.Cells[1].Text) + " em uso num determinado produto, tente de novo";
lblCatgsInfo.Visible = true;
lblCatgsInfo.ForeColor = System.Drawing.Color.Red;
}
if (BLL.BD.backoffice.VerificaCatgProdutoUso(Categoria).Rows.Count != 1)
{
int resultado = BLL.BD.backoffice.EliminarCatgProduto(IDCatg);
lblCatgsInfo.Text = "A categoria " + Convert.ToString(selectedRow.Cells[1].Text) + " com o ID " + Convert.ToString(selectedRow.Cells[0].Text) + " foi eliminada com sucesso!";
lblCatgsInfo.ForeColor = System.Drawing.Color.Green;
lblCatgsInfo.Visible = true;
GridViewManage.DataBind();
}
However it is not working, someone can inform me what is incorrect?
Thanks in advance.
I don’t understand very well, but is the category not being used as FK? If so, you should delete in a cascateada. I also didn’t understand what you said you tried in SP. I saw only a select and not a delete. You tried putting a BP in your method: Eliminarcatgproduct(Idcatg) to see what happens? Whether you are really calling delete or not.
– pnet
I just want stored to check if the "parts" category is actually being used by any product. If the category "parts" exists in the "category" column of the "products" table, I wanted it to validate the error so as not to delete the category, because it is being used by a product. If not in use, DELETE can be performed, this is all functional, what I really need is the validation of the category in use by the product
– Chirag Geiantilal
By what I saw you failed to compare the category table category category of the products table. I edited my answer. There you can adapt what you want.
– pnet