NYP-2 2016 6. Hafta Uygulamaları

nyp2_2016_hafta6_ornek1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace alisverissepeti
{
    public partial class Form1 : Form
    {
        DataTable dt1, dt2;
        MySqlDataAdapter da1, da2;

        public Form1()
        {
            InitializeComponent();
            rbalis.Checked = true;

            MySqlConnectionStringBuilder bag = new MySqlConnectionStringBuilder();
            bag.Server = "localhost";
            bag.UserID = "root";
            bag.Password = "123456";
            bag.Database = "hafta6_16_2";

            MySqlConnection conn = new MySqlConnection(bag.ToString());

            // urun tablosu sorgulari
            MySqlCommand cmd_urun_sel = 
                new MySqlCommand("select id, urunadi, alisfiyati, satisfiyati from urun", conn);
            da1 = new MySqlDataAdapter();
            da1.SelectCommand = cmd_urun_sel;

            // sepet tablosu sorgulari
            MySqlCommand cmd_sepet_sel = 
                new MySqlCommand("select id,urunid,miktar,tip from sepet", conn);
            MySqlCommand cmd_sepet_ins = 
                new MySqlCommand("insert into sepet(urunid,miktar,tip) values(@urunid,@miktar,@tip)", conn);
            cmd_sepet_ins.Parameters.Add("@urunid", MySqlDbType.Int32, 11, "urunid");
            cmd_sepet_ins.Parameters.Add("@miktar", MySqlDbType.Int32, 11, "miktar");
            cmd_sepet_ins.Parameters.Add("@tip", MySqlDbType.Int32, 11, "tip");
            MySqlCommand cmd_sepet_del = 
                new MySqlCommand("delete from sepet where id=@id", conn);
            cmd_sepet_del.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");

            da2 = new MySqlDataAdapter();
            da2.SelectCommand = cmd_sepet_sel;
            da2.InsertCommand = cmd_sepet_ins;
            da2.DeleteCommand = cmd_sepet_del;
            urungetir();
            sepetgetir();
        }

        private void urungetir()
        {
            curun.Items.Clear();
            try
            {
                dt1 = new DataTable();
                da1.Fill(dt1);
                int satirsay = dt1.Rows.Count;
                for (int i = 0; i < satirsay; i++)
                    curun.Items.Add(dt1.Rows[i]["urunadi"]);

                curun.Text = "Seçiniz";
            }
            catch
            {
                curun.Text = "Hata";
            }
        }

        private void sepetgetir()
        { 
            double tutar=0;
            lurun.Items.Clear();
            lmiktar.Items.Clear();
            lislem.Items.Clear();
            ltutar.Items.Clear();
            try
            {
                dt2 = new DataTable();
                da2.Fill(dt2);

                int satirsay = dt2.Rows.Count;
                for (int i = 0; i < satirsay; i++)
                {
                    int urunid= (int)dt2.Rows[i]["urunid"];
                    int miktar = (int)dt2.Rows[i]["miktar"];
                    int tip = (int)dt2.Rows[i]["tip"];
                    double uruntutar = 0;
                    DataRow dr = urunbul(urunid);
                    if (dr != null)
                    {
                        lurun.Items.Add(dr["urunadi"]);
                        lmiktar.Items.Add(miktar);
                        if (tip == 0)
                        {
                            lislem.Items.Add("Alış");
                            uruntutar = miktar * (double)dr["alisfiyati"];
                            tutar -= uruntutar;
                        }
                        else
                        {
                            lislem.Items.Add("Satış");
                            uruntutar = miktar * (double)dr["satisfiyati"];
                            tutar += uruntutar;
                        }
                        ltutar.Items.Add(uruntutar);
                    }
                }

                curun.Text = "Seçiniz";
            }
            catch
            {
                curun.Text = "Hata";
            }
            latutar.Text = tutar + " TL";
        }

        private DataRow urunbul(int id)
        {
            int satirsay = dt1.Rows.Count;
            for(int i=0;i<satirsay;i++)
            {
                if ((int)dt1.Rows[i]["id"] == id)
                    return dt1.Rows[i];
            }
            return null;

            
        }

         private DataRow urunbul1(int id)
        {
            try
            {
                DataRow[] dr = dt1.Select("id=" + id); // datatable icerisinde yer alan bir kolon ile ilgili filtre
                if (dr.Length > 0)
                    return dr[0];
            }
            catch
            { 
            
            }

            return null;
        }

        private void bekle_Click(object sender, EventArgs e)
        {
            int miktar;
            int secilen = curun.SelectedIndex;
            bool kontrol = Int32.TryParse(tmiktar.Text, out miktar);

            if (secilen > -1 && kontrol)
            {
                try
                {
                    DataRow dr = dt2.NewRow();
                    dr["urunid"] = dt1.Rows[secilen]["id"];// ekleme siralari ayni oldugu icin 
                    dr["miktar"] = miktar;
                    if (rbalis.Checked)
                        dr["tip"] = 0;
                    else
                        dr["tip"] = 1;

                    dt2.Rows.Add(dr);
                    da2.Update(dt2);
                    curun.Text = "Seçiniz";
                    rbalis.Checked = true;
                    tmiktar.Clear();
                }
                catch
                { }
                sepetgetir(); // liste guncelle
            }
        }

        private void bsil_Click(object sender, EventArgs e)
        {
            int secilen = lurun.SelectedIndex;
            if (secilen > -1)
            {
                try
                {
                    dt2.Rows[secilen].Delete();
                    da2.Update(dt2);
                }
                catch
                { }
                sepetgetir(); // listeyi guncelle
            }
        }
    }
}

 

CREATE SCHEMA `hafta6_16_2` DEFAULT CHARACTER SET utf8 COLLATE utf8_turkish_ci ;

CREATE TABLE `hafta6_16_2`.`urun` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '',
  `urunadi` VARCHAR(45) NULL COMMENT '',
  `alisfiyati` DOUBLE NULL COMMENT '',
  `satisfiyati` DOUBLE NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '');

  CREATE TABLE `hafta6_16_2`.`sepet` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '',
  `urunid` INT NULL COMMENT '',
  `miktar` INT NULL COMMENT '',
  `tip` INT NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '');

INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Masa', '25', '45');
INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Sandalye', '15', '20');
INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Sehpa', '5', '15');
INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('TV', '1200', '1350');
INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Halı', '450', '600');

NYP-2 2016 6. Hafta Uygulamaları

Share