NYP-2 2016 7. Hafta Uygulamaları

nyp2_2016_hafta7_1

nyp2_2016_hafta7_2

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 tarifeler
{
    public partial class Form1 : Form
    {
        MySqlDataAdapter da_paket, da_musteri;
        DataTable dt_paket, dt_musteri;
        public Form1()
        {
            InitializeComponent();

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

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

            // paket tablosu
            MySqlCommand cmd_paket_sel = 
                new MySqlCommand("select id,adi,dk,sms,mb from paket", conn);
            MySqlCommand cmd_paket_ins =
                new MySqlCommand("insert into paket(adi,dk,sms,mb) values(@adi,@dk,@sms,@mb)", conn);
            cmd_paket_ins.Parameters.Add("@adi", MySqlDbType.VarChar, 45, "adi");
            cmd_paket_ins.Parameters.Add("@dk", MySqlDbType.Double, 11, "dk");
            cmd_paket_ins.Parameters.Add("@sms", MySqlDbType.Double, 11, "sms");
            cmd_paket_ins.Parameters.Add("@mb", MySqlDbType.Double, 11, "mb");

            MySqlCommand cmd_paket_upd = 
                new MySqlCommand("update paket set dk=@dk, sms=@sms, mb=@mb where id=@id", conn);
            cmd_paket_upd.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");
            cmd_paket_upd.Parameters.Add("@dk", MySqlDbType.Double, 11, "dk");
            cmd_paket_upd.Parameters.Add("@sms", MySqlDbType.Double, 11, "sms");
            cmd_paket_upd.Parameters.Add("@mb", MySqlDbType.Double, 11, "mb");


            da_paket = new MySqlDataAdapter();
            da_paket.SelectCommand = cmd_paket_sel;
            da_paket.InsertCommand = cmd_paket_ins;
            da_paket.UpdateCommand = cmd_paket_upd;

            // musteri tablosu
            MySqlCommand cmd_musteri_sel = 
                new MySqlCommand("select id,musteriadi,paketid,k_dk,k_sms,k_mb from musteri", conn);
            MySqlCommand cmd_musteri_ins =
                new MySqlCommand("insert into musteri(musteriadi,paketid,k_dk,k_sms,k_mb) values(@musteriadi,@paketid,@k_dk,@k_sms,@k_mb)", conn);
            cmd_musteri_ins.Parameters.Add("@musteriadi", MySqlDbType.VarChar, 45, "musteriadi");
            cmd_musteri_ins.Parameters.Add("@paketid", MySqlDbType.Int32, 11, "paketid");
            cmd_musteri_ins.Parameters.Add("@k_dk", MySqlDbType.Int32, 11, "k_dk");
            cmd_musteri_ins.Parameters.Add("@k_sms", MySqlDbType.Int32, 11, "k_sms");
            cmd_musteri_ins.Parameters.Add("@k_mb", MySqlDbType.Int32, 11, "k_mb");

            MySqlCommand cmd_musteri_del = 
                new MySqlCommand("delete from musteri where id=@id", conn);
            cmd_musteri_del.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");

            da_musteri = new MySqlDataAdapter();
            da_musteri.SelectCommand = cmd_musteri_sel;
            da_musteri.InsertCommand = cmd_musteri_ins;
            da_musteri.DeleteCommand = cmd_musteri_del;

            paketgetir();
            musterigetir();
        }


        private void paketgetir()
        {
            ctarife.Items.Clear();
            try
            {
                dt_paket = new DataTable();
                da_paket.Fill(dt_paket);
                int satirsay = dt_paket.Rows.Count;
                for (int i = 0; i < satirsay; i++)
                    ctarife.Items.Add(dt_paket.Rows[i]["adi"]);
            }
            catch
            { 
            }

            ctarife.Text = "Seçiniz";
        }

        private void musterigetir()
        {
            lmusteri.Items.Clear();
            ltarife.Items.Clear();
            ldk.Items.Clear();
            lsms.Items.Clear();
            lmb.Items.Clear();
            ltutar.Items.Clear();

            try
            {
                dt_musteri = new DataTable();
                da_musteri.Fill(dt_musteri);
                int satirsay = dt_musteri.Rows.Count;

                for (int i = 0; i < satirsay; i++)
                {

                    int paketid = (int)dt_musteri.Rows[i]["paketid"];

                    DataRow dr = tarifeyigetir(paketid);

                    if (dr != null)
                    {
                        double tutar = 
                            (int)dt_musteri.Rows[i]["k_dk"] * (double)dr["dk"] + 
                            (int)dt_musteri.Rows[i]["k_sms"] * (double)dr["sms"] + 
                            (int)dt_musteri.Rows[i]["k_mb"] * (double)dr["mb"];

                        lmusteri.Items.Add(dt_musteri.Rows[i]["musteriadi"]);
                        ltarife.Items.Add(dr["adi"]);
                        ldk.Items.Add(dt_musteri.Rows[i]["k_dk"]);
                        lsms.Items.Add(dt_musteri.Rows[i]["k_sms"]);
                        lmb.Items.Add(dt_musteri.Rows[i]["k_mb"]);
                        ltutar.Items.Add(tutar);
                    
                    
                    }
                
                }

            }
            catch
            { 
            
            }
        }


        private DataRow tarifeyigetir(int id)
        {
            try
            {
                DataRow[] drs=dt_paket.Select("id=" + id);
                if (drs.Length > 0)
                    return drs[0];
            }
            catch
            { 
            }

            return null;
        
        }

        private void bekle_Click(object sender, EventArgs e)
        {
            int dk, sms, mb;
            bool k1 = Int32.TryParse(tdk.Text, out dk);
            bool k2 = Int32.TryParse(tsms.Text, out sms);
            bool k3 = Int32.TryParse(tmb.Text, out mb);
            int secilen = ctarife.SelectedIndex;

            if(secilen>-1 && k1 && k2 && k3 && tmusteri.Text.Length>0)
            {
                try
                {
                    DataRow dr = dt_musteri.NewRow();
                    dr["musteriadi"] = tmusteri.Text;
                    dr["paketid"] =(int)dt_paket.Rows[secilen]["id"];
                    dr["k_dk"] = dk;
                    dr["k_sms"] = sms;
                    dr["k_mb"] = mb;
                    dt_musteri.Rows.Add(dr);
                    da_musteri.Update(dt_musteri);
                    
                    tmusteri.Clear();
                    ctarife.Text = "Seçiniz";
                    tdk.Clear();
                    tsms.Clear();
                    tmb.Clear();
                }
                catch
                { }

                musterigetir();
            }
        }

        private void bsil_Click(object sender, EventArgs e)
        {
            int secilen = lmusteri.SelectedIndex;
            if (secilen > -1)
            {
                try
                {
                    dt_musteri.Rows[secilen].Delete();
                    da_musteri.Update(dt_musteri);
                }
                catch
                { }
                musterigetir();
            }


        }

        private void btarife_Click(object sender, EventArgs e)
        {
            ftarifeler frm = new ftarifeler(da_paket);
            frm.ShowDialog();
            paketgetir();
        }
    }
}

 

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 tarifeler
{
    public partial class ftarifeler: Form
    {
        MySqlDataAdapter da;
        DataTable dt;
        public ftarifeler(MySqlDataAdapter _da)
        {
            InitializeComponent();
            da = _da;
            verigetir();
        }

        private void verigetir()
        {
            lad.Items.Clear();
            ldk.Items.Clear();
            lsms.Items.Clear();
            lmb.Items.Clear();
            try
            {
                dt = new DataTable();
                da.Fill(dt);
                int satirsay = dt.Rows.Count;
                for (int i = 0; i < satirsay; i++)
                {
                    lad.Items.Add(dt.Rows[i]["adi"]);
                    ldk.Items.Add(dt.Rows[i]["dk"]);
                    lsms.Items.Add(dt.Rows[i]["sms"]);
                    lmb.Items.Add(dt.Rows[i]["mb"]);
                }
            }
            catch
            { }
        }

        private void bekle_Click(object sender, EventArgs e)
        {
            double fdk, fsms, fmb;
            bool k1 = Double.TryParse(tdk.Text, out fdk);
            bool k2 = Double.TryParse(tsms.Text, out fsms);
            bool k3 = Double.TryParse(tmb.Text, out fmb);

            if (tad.Text.Length > 0 && k1 && k2 && k3)
            {
                try
                {
                    DataRow dr = dt.NewRow();
                    dr["adi"] = tad.Text;
                    dr["dk"] = fdk;
                    dr["sms"] = fsms;
                    dr["mb"] = fmb;

                    dt.Rows.Add(dr);
                    da.Update(dt);

                    tad.Clear();
                    tdk.Clear();
                    tsms.Clear();
                    tmb.Clear();
                }
                catch { 
                }

                verigetir();

            }
        }

        private void bduzenle_Click(object sender, EventArgs e)
        {
            double fdk, fsms, fmb;
            bool k1 = Double.TryParse(tdk.Text, out fdk);
            bool k2 = Double.TryParse(tsms.Text, out fsms);
            bool k3 = Double.TryParse(tmb.Text, out fmb);
            int secilen = lad.SelectedIndex;

            if ( secilen>-1 && k1 && k2 && k3)
            {
                try
                {
                    DataRow dr = dt.Rows[secilen];
                    dr["dk"] = fdk;
                    dr["sms"] = fsms;
                    dr["mb"] = fmb;
                    da.Update(dt);

                    tad.Clear();
                    tdk.Clear();
                    tsms.Clear();
                    tmb.Clear();
                
                }
                catch
                { }

                verigetir();
            }

        }

        private void bkapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

 

NYP-2 2016 Hafta-7

Share