Exemple #1
0
 void ClearBoxes()
 {
     id.Clear();
     label.Clear();
     price.Clear();
     qty.Clear();
     priceafterQ.Clear();
     disCount.Clear();
     TotalPrice.Clear();
     choosebtn.Focus();
 }
        private void add_Click(object sender, EventArgs e)
        {
            int a = 0;
            //count rows
            //int rows =  Orderdgv.Rows.Count;
            //rows = rows - 1;
            int insertid = 0;
            //Count number of products from grid view
            int sum = 0;

            for (int i = 0; i < Orderdgv.Rows.Count; ++i)
            {
                sum += Convert.ToInt32(Orderdgv.Rows[i].Cells[3].Value);
            }
            // MessageBox.Show(sum.ToString()) ;



            //Remaining amount checks
            if (ReceivedAmount.Text == "")
            {
                if (Convert.ToInt32(TotalPrice.Text) < 0)
                {
                    ReceivedAmount.Text = TotalPrice.Text;
                    ReceivedAmount.Text = "-" + ReceivedAmount.Text;
                }
                ReceivedAmount.Text = TotalPrice.Text;
            }
            //inseted bill id

            int remaining = Convert.ToInt32(ReceivedAmount.Text) - Convert.ToInt32(TotalPrice.Text);

            if (remaining < 0)
            {
                MessageBox.Show("INVALID AMOUNT RECEIVED");
            }
            else
            {
                try
                {
                    //Add BIll details into billdb
                    // only add billid,qty,amount , seller name
                    MessageBox.Show(a.ToString());
                    a++;
                    DateTime dateTime = DateTime.Now;
                    Con.Open();
                    String query = "insert into billdb (items, amount, seller,date) values('" + sum.ToString() + "','" + TotalPrice.Text + "','"
                                   + Form1.SetValueForText1 + "','" + dateTime.ToShortDateString() + "'); select @@identity;";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    //cmd.ExecuteNonQuery();
                    insertid = int.Parse(cmd.ExecuteScalar().ToString());
                    MessageBox.Show(insertid.ToString());

                    Con.Close();
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                }

                int check = Convert.ToInt32(TotalPrice.Text);

                if (check < 0)
                {
                    MessageBox.Show("Total Products = " + sum.ToString() + "\n" + "Total Bill = " + TotalPrice.Text + "\n" + "Customer Remaining Change = " + remaining);
                }
                if (check > 0)
                {
                    //Show the change
                    MessageBox.Show("Total Products = " + sum.ToString() + "\n" + "Total Bill = " + TotalPrice.Text + "\n" + "Amount Recieved= " + ReceivedAmount.Text + "\n" + "Change = " + remaining);
                }

                int i = 0;
                //MessageBox.Show("Yahan se");

                //

                //Insert items into table

                //
                for (i = 0; i < Orderdgv.Rows.Count - 1; i++)
                {
                    try
                    {
                        Con.Open();
                        // MessageBox.Show("Yahse");
                        string StrQuery = "INSERT INTO solditems (pid,billid, pname, pprice,pqty,total) VALUES ('" + Orderdgv.Rows[i].Cells[0].Value + "', '" + insertid + "', '"
                                          + Orderdgv.Rows[i].Cells[1].Value + "', '" + Orderdgv.Rows[i].Cells[2].Value + "', '" + Orderdgv.Rows[i].Cells[3].Value + "', '" + Orderdgv.Rows[i].Cells[4].Value + "')";
                        SqlCommand cmd = new SqlCommand(StrQuery, Con);
                        cmd.ExecuteNonQuery();
                        Con.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        Con.Close();
                    }
                }



                Orderdgv.Rows.Clear();
                ProQty.Clear();
                ProName.Clear();
                ProPrice.Clear();
                TotalPrice.Clear();
                ReceivedAmount.Clear();
            }
        }