protected void LinkButtonSaveQuestion_Click(object sender, EventArgs e)
        {
            try
            {
                int? continentId = Convert.ToInt32(Request.Params["continentId"]);

                if (continentId != null && continentId > 0)
                {
                    WorldInfoEntities context = new WorldInfoEntities();
                    Continent continent = context.Continents.Find(continentId);

                    if (continent != null)
                    {
                        continent.Name = this.TextBoxContinentName.Text;
                        context.SaveChanges();
                        Response.Redirect("~/ManageContinents.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                this.TextBoxErrorMessage.Visible = true;
                this.TextBoxErrorMessage.Text = ex.Message;
            }
        }
        protected void LinkButtonSaveTown_Click(object sender, EventArgs e)
        {
            try
            {
                int? townId = Convert.ToInt32(Request.Params["townId"]);

                if (townId != null && townId > 0)
                {
                    WorldInfoEntities context = new WorldInfoEntities();
                    Town town = context.Towns.Find(townId);

                    if (town != null)
                    {
                        town.Name = this.TextBoxTownName.Text;
                        town.Population = long.Parse(this.TextBoxPopulation.Text);
                        town.CountryId = int.Parse(this.ListBoxCountries.SelectedValue);
                        context.SaveChanges();
                        Response.Redirect("~/ManageTowns.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                this.LiteralErrorMessage.Visible = true;
                this.LiteralErrorMessage.Text = ex.Message;
            }
        }
 protected void LinkButtonSaveQuestion_Click(object sender, EventArgs e)
 {
     if (Page.IsValid)
     {
         WorldInfoEntities context = new WorldInfoEntities();
         Continent continent = new Continent();
         continent.Name = this.TextBoxContinentName.Text;
         context.Continents.Add(continent);
         context.SaveChanges();
         Response.Redirect("~/ManageContinents.aspx");
     }
 }
 protected void LinkButtonSaveTown_Click(object sender, EventArgs e)
 {
     if (Page.IsValid)
     {
         WorldInfoEntities context = new WorldInfoEntities();
         Town town = new Town();
         town.Name = this.TextBoxTownName.Text;
         town.Population = long.Parse(this.TextBoxPopulation.Text);
         town.CountryId = int.Parse(this.ListBoxCountries.SelectedValue);
         context.Towns.Add(town);
         context.SaveChanges();
         Response.Redirect("~/ManageTowns.aspx");
     }
 }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            int? continentId = Convert.ToInt32(Request.Params["continentId"]);

            if (continentId != null && continentId > 0)
            {
                WorldInfoEntities context = new WorldInfoEntities();
                Continent continent = context.Continents.Find(continentId);
                this.TextBoxContinentName.Text = continent.Name;
            }
            else
            {
                Response.Redirect("~/ManageContinents.aspx");
            }
        }
 protected void LinkButtonSaveCountry_Click(object sender, EventArgs e)
 {
     if (Page.IsValid)
     {
         WorldInfoEntities context = new WorldInfoEntities();
         Country country = new Country();
         country.Name = this.TextBoxCountryName.Text;
         country.Population = long.Parse(this.TextBoxPopulation.Text);
         country.Language = this.TextBoxLanguage.Text;
         country.ContinentId = int.Parse(this.ListBoxContinents.SelectedValue);
         country.Flag = this.FileUploadFlag.FileBytes;
         context.Countries.Add(country);
         context.SaveChanges();
         Response.Redirect("~/ManageCountries.aspx");
     }
 }
 protected void LinkButtonDeleteTown_Command(object sender, CommandEventArgs e)
 {
     try
     {
         var id = int.Parse(e.CommandArgument.ToString());
         WorldInfoEntities context = new WorldInfoEntities();
         Town town = context.Towns.Find(id);
         context.Towns.Remove(town);
         context.SaveChanges();
         Response.Redirect("~/ManageTowns.aspx");
     }
     catch (Exception ex)
     {
         this.LiteralErrorMessage.Visible = true;
         this.LiteralErrorMessage.Text = ex.Message;
     }
 }
        private Byte[] RetrieveCountryImage(int countryId, HttpContext context)
        {
            WorldInfoEntities db = new WorldInfoEntities();
            var country = db.Countries.Find(countryId);
            if (country != null)
            {
                if (country.Flag != null)
                {
                    return country.Flag;
                }
                else
                {
                    return File.ReadAllBytes(context.Server.MapPath("~/Images/default_flag.png"));
                }

            }

            return null;
        }
        protected void LinkButtonSaveCountry_Click(object sender, EventArgs e)
        {
            try
            {
                int? countryId = Convert.ToInt32(Request.Params["countryId"]);

                if (countryId != null && countryId > 0)
                {
                    WorldInfoEntities context = new WorldInfoEntities();
                    Country country = context.Countries.Find(countryId);

                    if (country != null)
                    {
                        country.Name = this.TextBoxCountryName.Text;
                        country.Population = long.Parse(this.TextBoxPopulation.Text);
                        country.Language = this.TextBoxLanguage.Text;
                        country.ContinentId = int.Parse(this.ListBoxContinents.SelectedValue);
                        if (this.FileUploadFlag.FileBytes.Length > 0)
                        {
                            country.Flag = this.FileUploadFlag.FileBytes;
                        }

                        if (this.CheckRemoveFlag.Checked)
                        {
                            country.Flag = null;
                        }

                        context.SaveChanges();
                        Response.Redirect("~/ManageCountries.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                this.LiteralErrorMessage.Visible = true;
                this.LiteralErrorMessage.Text = ex.Message;
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            int? townId = Convert.ToInt32(Request.Params["townId"]);

            if (townId != null && townId > 0)
            {
                WorldInfoEntities context = new WorldInfoEntities();
                Town town = context.Towns.Find(townId);
                this.ListBoxCountries.DataSource = context.Countries.ToList();
                this.ListBoxCountries.DataTextField = "Name";
                this.ListBoxCountries.DataValueField = "Id";
                this.ListBoxCountries.DataBind();
                var countryItem = this.ListBoxCountries.Items.FindByText(town.Country.Name);
                countryItem.Selected = true;
                this.TextBoxTownName.Text = town.Name;
                this.TextBoxPopulation.Text = town.Population.ToString();
            }
            else
            {
                Response.Redirect("~/ManageTowns.aspx");
            }
        }