private void SaveButton_Click(object sender, EventArgs e)
 {
     try
     {
         District district = new District();
         district.Name = nameTextBox.Text;
         bool isAdded = _districtManager.Add(district);
         if (isAdded)
         {
             MessageBox.Show("Added");
             return;
         }
         MessageBox.Show("Failed");
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message);
     }
 }
Exemple #2
0
        private void simpleButtonSave_Click(object sender, EventArgs e)
        {
            if (this.textEditCity.Text == "")
            {
                MessageBox.Show("資料不全,無法儲存!");
                return;
            }
            if (this.textEditDistrict.Text == "")
            {
                MessageBox.Show("資料不全,無法儲存!");
                return;
            }
            if (this.textEditStreet.Text == "")
            {
                MessageBox.Show("資料不全,無法儲存!");
                return;
            }
            if (city == null)
            {
                city = new City();
            }
            if (district == null)
            {
                district = new District();
            }
            if (shreet == null)
            {
                shreet = new Shreet();
            }
            city.CityName         = this.textEditCity.Text;
            district.DistrictName = this.textEditDistrict.Text;
            shreet.ShreetName     = this.textEditStreet.Text;
            if (flog == 1)
            {
                if (CityManager.GetCityByName(city.CityName) == null)
                {
                    bool i = CityManager.Add(city);
                    if (i == true)
                    {
                        district.CityId = CityManager.GetCityByName(city.CityName)[0].CityId;
                        bool j = DistrictManager.Add(district);
                        if (j == true)
                        {
                            shreet.DistrictId = DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].DistrictId;
                            bool k = ShreetManager.Add(shreet);
                            if (k == true)
                            {
                                MessageBox.Show("添加成功!");
                                //this.bindingSourceDistrict.DataSource = DistrictManager.GetDistrictByCityID(district.CityId);
                                //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);
                                IList <District> list1 = null;
                                DataTable        table = DistrictManager.GetDistrictByCityIDTwo(district.CityId);
                                if (table != null)
                                {
                                    if (table.Rows.Count > 0)
                                    {
                                        list1 = new List <District>();
                                        foreach (DataRow row in table.Rows)
                                        {
                                            District district1 = new District();
                                            district1.DistrictId   = int.Parse(row["districtId"].ToString());
                                            district1.DistrictName = row["districtName"].ToString();
                                            district1.CityId       = int.Parse(row["cityId"].ToString());
                                            list1.Add(district1);
                                        }
                                    }
                                }
                                this.bindingSourceDistrict.DataSource = list1;

                                IList <Shreet> list   = null;
                                DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                                if (table1 != null)
                                {
                                    if (table1.Rows.Count > 0)
                                    {
                                        list = new List <Shreet>();
                                        foreach (DataRow row in table1.Rows)
                                        {
                                            Shreet shreet1 = new Shreet();
                                            shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                                            shreet1.ShreetName = row["shreetName"].ToString();
                                            shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                                            list.Add(shreet1);
                                        }
                                    }
                                }
                                this.bindingSourceStreet.DataSource = list;
                            }
                        }
                    }
                }
                else
                {
                    if (DistrictManager.GetDistrictByName(district.DistrictName) == null)
                    {
                        district.CityId = CityManager.GetCityByName(city.CityName)[0].CityId;
                        bool i = DistrictManager.Add(district);
                        if (i == true)
                        {
                            shreet.DistrictId = DistrictManager.GetDistrictByName(district.DistrictName)[0].DistrictId;
                            bool k = ShreetManager.Add(shreet);
                            if (k == true)
                            {
                                MessageBox.Show("添加成功!");
                                //this.bindingSourceDistrict.DataSource = DistrictManager.GetDistrictByCityID(district.CityId);
                                //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);
                                IList <District> list1 = null;
                                DataTable        table = DistrictManager.GetDistrictByCityIDTwo(district.CityId);
                                if (table != null)
                                {
                                    if (table.Rows.Count > 0)
                                    {
                                        list1 = new List <District>();
                                        foreach (DataRow row in table.Rows)
                                        {
                                            District district1 = new District();
                                            district1.DistrictId   = int.Parse(row["districtId"].ToString());
                                            district1.DistrictName = row["districtName"].ToString();
                                            district1.CityId       = int.Parse(row["cityId"].ToString());
                                            list1.Add(district1);
                                        }
                                    }
                                }
                                this.bindingSourceDistrict.DataSource = list1;

                                IList <Shreet> list   = null;
                                DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                                if (table1 != null)
                                {
                                    if (table1.Rows.Count > 0)
                                    {
                                        list = new List <Shreet>();
                                        foreach (DataRow row in table1.Rows)
                                        {
                                            Shreet shreet1 = new Shreet();
                                            shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                                            shreet1.ShreetName = row["shreetName"].ToString();
                                            shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                                            list.Add(shreet1);
                                        }
                                    }
                                }
                                this.bindingSourceStreet.DataSource = list;
                            }
                        }
                    }
                    else
                    {
                        if (DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId) == null)
                        {
                            district.CityId = CityManager.GetCityByName(city.CityName)[0].CityId;
                            bool i = DistrictManager.Add(district);
                            if (i == true)
                            {
                                shreet.DistrictId = DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].DistrictId;
                                bool k = ShreetManager.Add(shreet);
                                if (k == true)
                                {
                                    MessageBox.Show("添加成功!");
                                    //this.bindingSourceDistrict.DataSource = DistrictManager.GetDistrictByCityID(district.CityId);
                                    //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);
                                    IList <District> list1 = null;
                                    DataTable        table = DistrictManager.GetDistrictByCityIDTwo(district.CityId);
                                    if (table != null)
                                    {
                                        if (table.Rows.Count > 0)
                                        {
                                            list1 = new List <District>();
                                            foreach (DataRow row in table.Rows)
                                            {
                                                District district1 = new District();
                                                district1.DistrictId   = int.Parse(row["districtId"].ToString());
                                                district1.DistrictName = row["districtName"].ToString();
                                                district1.CityId       = int.Parse(row["cityId"].ToString());
                                                list1.Add(district1);
                                            }
                                        }
                                    }
                                    this.bindingSourceDistrict.DataSource = list1;

                                    IList <Shreet> list   = null;
                                    DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                                    if (table1 != null)
                                    {
                                        if (table1.Rows.Count > 0)
                                        {
                                            list = new List <Shreet>();
                                            foreach (DataRow row in table1.Rows)
                                            {
                                                Shreet shreet1 = new Shreet();
                                                shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                                                shreet1.ShreetName = row["shreetName"].ToString();
                                                shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                                                list.Add(shreet1);
                                            }
                                        }
                                    }
                                    this.bindingSourceStreet.DataSource = list;
                                }
                            }
                        }
                        else
                        {
                            if (DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].CityId != CityManager.GetCityByName(city.CityName)[0].CityId)
                            {
                                district.CityId = CityManager.GetCityByName(city.CityName)[0].CityId;
                                bool i = DistrictManager.Add(district);
                                if (i == true)
                                {
                                    shreet.DistrictId = DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].DistrictId;
                                    bool k = ShreetManager.Add(shreet);
                                    if (k == true)
                                    {
                                        MessageBox.Show("添加成功!");
                                        //this.bindingSourceDistrict.DataSource = DistrictManager.GetDistrictByCityID(district.CityId);
                                        //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);
                                        IList <District> list1 = null;
                                        DataTable        table = DistrictManager.GetDistrictByCityIDTwo(district.CityId);
                                        if (table != null)
                                        {
                                            if (table.Rows.Count > 0)
                                            {
                                                list1 = new List <District>();
                                                foreach (DataRow row in table.Rows)
                                                {
                                                    District district1 = new District();
                                                    district1.DistrictId   = int.Parse(row["districtId"].ToString());
                                                    district1.DistrictName = row["districtName"].ToString();
                                                    district1.CityId       = int.Parse(row["cityId"].ToString());
                                                    list1.Add(district1);
                                                }
                                            }
                                        }
                                        this.bindingSourceDistrict.DataSource = list1;

                                        IList <Shreet> list   = null;
                                        DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                                        if (table1 != null)
                                        {
                                            if (table1.Rows.Count > 0)
                                            {
                                                list = new List <Shreet>();
                                                foreach (DataRow row in table1.Rows)
                                                {
                                                    Shreet shreet1 = new Shreet();
                                                    shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                                                    shreet1.ShreetName = row["shreetName"].ToString();
                                                    shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                                                    list.Add(shreet1);
                                                }
                                            }
                                        }
                                        this.bindingSourceStreet.DataSource = list;
                                    }
                                }
                            }
                            else
                            {
                                if (ShreetManager.GetShreetByName(shreet.ShreetName) == null)
                                {
                                    shreet.DistrictId = DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].DistrictId;

                                    bool i = ShreetManager.Add(shreet);
                                    if (i == true)
                                    {
                                        MessageBox.Show("添加成功!");
                                        //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);

                                        IList <Shreet> list   = null;
                                        DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                                        if (table1 != null)
                                        {
                                            if (table1.Rows.Count > 0)
                                            {
                                                list = new List <Shreet>();
                                                foreach (DataRow row in table1.Rows)
                                                {
                                                    Shreet shreet1 = new Shreet();
                                                    shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                                                    shreet1.ShreetName = row["shreetName"].ToString();
                                                    shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                                                    list.Add(shreet1);
                                                }
                                            }
                                        }
                                        this.bindingSourceStreet.DataSource = list;
                                    }
                                }
                                else
                                {
                                    int iCount = 0;
                                    foreach (Shreet ss in ShreetManager.GetShreetByName(shreet.ShreetName))
                                    {
                                        if (ss.DistrictId == DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].DistrictId)
                                        {
                                            iCount = 1;
                                        }
                                    }
                                    if (iCount == 0)
                                    {
                                        shreet.DistrictId = DistrictManager.GetDistrictByNameAndCityId(district.DistrictName, district.CityId)[0].DistrictId;
                                        bool i = ShreetManager.Add(shreet);
                                        if (i == true)
                                        {
                                            MessageBox.Show("添加成功!");
                                            //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);

                                            IList <Shreet> list   = null;
                                            DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                                            if (table1 != null)
                                            {
                                                if (table1.Rows.Count > 0)
                                                {
                                                    list = new List <Shreet>();
                                                    foreach (DataRow row in table1.Rows)
                                                    {
                                                        Shreet shreet1 = new Shreet();
                                                        shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                                                        shreet1.ShreetName = row["shreetName"].ToString();
                                                        shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                                                        list.Add(shreet1);
                                                    }
                                                }
                                            }
                                            this.bindingSourceStreet.DataSource = list;
                                        }
                                    }
                                    else
                                    {
                                        iCount = 0;
                                        jjyy   = 1;
                                        MessageBox.Show("資料重複,無法新增此筆資料!");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (flog == 2)
            {
                ShreetManager.Update(shreet);
                if (DistrictManager.GetDistrictByName(district.DistrictName) == null)
                {
                    DistrictManager.Update(district);
                }
                else
                {
                    int iCount = 0;
                    foreach (District d in DistrictManager.GetDistrictByName(district.DistrictName))
                    {
                        if (d.CityId == city.CityId)
                        {
                            iCount = 1;
                        }
                    }
                    if (iCount == 0)
                    {
                        DistrictManager.Update(district);
                    }
                    iCount = 0;
                }
                if (CityManager.GetCityByName(city.CityName) == null)
                {
                    CityManager.Update(city);
                }

                MessageBox.Show("修改成功!");
                //this.bindingSourceDistrict.DataSource = DistrictManager.GetDistrictByCityID(district.CityId);
                //this.bindingSourceStreet.DataSource = ShreetManager.GetShreetByDistrictId(shreet.DistrictId);
                IList <District> list1 = null;
                DataTable        table = DistrictManager.GetDistrictByCityIDTwo(district.CityId);
                if (table != null)
                {
                    if (table.Rows.Count > 0)
                    {
                        list1 = new List <District>();
                        foreach (DataRow row in table.Rows)
                        {
                            District district1 = new District();
                            district1.DistrictId   = int.Parse(row["districtId"].ToString());
                            district1.DistrictName = row["districtName"].ToString();
                            district1.CityId       = int.Parse(row["cityId"].ToString());
                            list1.Add(district1);
                        }
                    }
                }
                this.bindingSourceDistrict.DataSource = list1;

                IList <Shreet> list   = null;
                DataTable      table1 = ShreetManager.GetShreetByDistrictIdTwo(shreet.DistrictId);
                if (table1 != null)
                {
                    if (table1.Rows.Count > 0)
                    {
                        list = new List <Shreet>();
                        foreach (DataRow row in table1.Rows)
                        {
                            Shreet shreet1 = new Shreet();
                            shreet1.ShreetId   = int.Parse(row["shreetId"].ToString());
                            shreet1.ShreetName = row["shreetName"].ToString();
                            shreet1.DistrictId = int.Parse(row["districtId"].ToString());
                            list.Add(shreet1);
                        }
                    }
                }
                this.bindingSourceStreet.DataSource = list;
            }
            flog = 0;
            Bind();
        }