1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Decrement value in SQL but not negative with C#

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 4, 2024 às 07:13.

  1. Stack

    Stack Membro Participativo

    I want to reduce the amount of my product in the database from the product reduction panel. But if the entered number is more than my product amount, that is, if the amount decreases and becomes negative, I want it to give an error. I could not find this decision mechanism and the necessary commands, can you help me?

    I mean I want to decrement the number (which is amount of product), but I do not want the number (amount of product) to be allowed to go negative. When input is greater than amount, I want to give an error like "We do not have enough product!"

    This is how I try it

    string constr = ConfigurationManager.ConnectionStrings["stok"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmda;
    SqlCommand cmdb;
    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM urunler WHERE barkod = '" + barkod.Text + "'", con);
    SqlDataAdapter db = new SqlDataAdapter("SELECT * FROM urunler WHERE adet >= '0' AND barkod = '" + barkod.Text + "'", con);
    DataSet ds = new DataSet();
    DataSet dl = new DataSet();
    da.Fill(ds);
    db.Fill(dl);

    if (ds.Tables[0].Rows.Count > 0)
    {
    cmda = new SqlCommand("UPDATE urunler SET cikarTarih='" + cikarTarih.Text + "'WHERE barkod='" + barkod.Text + "'", con);
    cmdb = new SqlCommand("UPDATE urunler SET adet=adet-1 WHERE barkod='" + barkod.Text + "'", con);

    for (int i = Convert.ToInt32(adet.Text); i > 0 ; i--)
    {
    cmdb.ExecuteNonQuery();
    if (dl.Tables[0].Rows.Count < 0)
    {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('Hata', 'Ürün adedi yeterli değil!', 'error')", true);
    }
    }
    cmda.ExecuteNonQuery();
    ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('İşlem Tamamlandı', 'Ürün adedi başarıyla güncellendi!', 'success')", true);
    }
    else
    {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('Hata', 'Aradığınız ürün bulunamadı!', 'error')", true);
    }

    con.Close();


    Here is my solution!

    string constr = ConfigurationManager.ConnectionStrings["stok"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmda;
    SqlCommand cmdb;
    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM urunler WHERE barkod = '" + barkod.Text + "'", con);
    SqlDataAdapter db = new SqlDataAdapter("SELECT * FROM urunler WHERE adet >='" + Convert.ToInt32(adet.Text) + "'AND barkod = '" + barkod.Text + "'", con);
    DataSet ds = new DataSet();
    DataSet dl = new DataSet();
    da.Fill(ds);
    db.Fill(dl);

    if (ds.Tables[0].Rows.Count > 0)
    {
    if (dl.Tables[0].Rows.Count > 0)
    {
    cmda = new SqlCommand("UPDATE urunler SET cikarTarih='" + cikarTarih.Text + "'WHERE barkod='" + barkod.Text + "'", con);
    cmdb = new SqlCommand("UPDATE urunler SET adet=adet-1 WHERE barkod='" + barkod.Text + "'", con);

    for (int i = Convert.ToInt32(adet.Text); i > 0 ; i--)
    {
    cmdb.ExecuteNonQuery();
    }
    cmda.ExecuteNonQuery();
    ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('İşlem Tamamlandı', 'Ürün adedi başarıyla güncellendi!', 'success')", true);
    }
    else
    {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('Hata', 'Yeterli ürün bulunamadı!', 'error')", true);
    }
    }
    else
    {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('Hata', 'Aradığınız ürün bulunamadı!', 'error')", true);
    }

    con.Close();

    Continue reading...

Compartilhe esta Página