private void saveCountries(IEnumerable <XElement> countriesWithPhNoFormat) { CustomerManagementModel.Entities cmObjectContext = new CustomerManagementModel.Entities( System.Web.Configuration.WebConfigurationManager. ConnectionStrings["Entities"]. ConnectionString); // Loop through the filtered countries foreach (XElement country in countriesWithPhNoFormat) { // Add the new country to the Countries collection cmObjectContext.Countries.AddObject(new CustomerManagementModel.Country { ID = new Guid(country.Attribute("ID").Value), Name = country.Attribute("Name").Value, PhoneNoFormat = country.Attribute("PhoneNoFormat").Value, DialingCountryCode = country.Attribute("DialingCountryCode").Value, InternationalDialingCode = country.Attribute("InternationalDialingCode").Value, InternetTLD = country.Attribute("InternetTLD").Value }); } // Save to database try { cmObjectContext.SaveChanges(); // Display success message ImportResultLabel.Text = "Rows successfully exported to SQL Server table."; } catch (System.Data.UpdateException) { // Display error ImportResultLabel.Text = "An error occurred exporting to SQL Server.<br/>One or more of the rows already exist in the table.<br/><br/>"; ImportResultLabel.ForeColor = System.Drawing.Color.Red; } catch (Exception ex) { // Display error ImportResultLabel.Text = "An error occurred exporting to SQL Server.<br/>" + ex.Message + "<br/><br/>"; ImportResultLabel.ForeColor = System.Drawing.Color.Red; } finally { cmObjectContext.Dispose(); } }
private void saveCountries(IEnumerable<XElement> countriesWithPhNoFormat) { CustomerManagementModel.Entities cmObjectContext = new CustomerManagementModel.Entities( System.Web.Configuration.WebConfigurationManager. ConnectionStrings["Entities"]. ConnectionString); // Loop through the filtered countries foreach (XElement country in countriesWithPhNoFormat) { // Add the new country to the Countries collection cmObjectContext.Countries.AddObject(new CustomerManagementModel.Country { ID = new Guid(country.Attribute("ID").Value), Name = country.Attribute("Name").Value, PhoneNoFormat = country.Attribute("PhoneNoFormat").Value, DialingCountryCode = country.Attribute("DialingCountryCode").Value, InternationalDialingCode = country.Attribute("InternationalDialingCode").Value, InternetTLD = country.Attribute("InternetTLD").Value }); } // Save to database try { cmObjectContext.SaveChanges(); // Display success message ImportResultLabel.Text = "Rows successfully exported to SQL Server table."; } catch (System.Data.UpdateException) { // Display error ImportResultLabel.Text = "An error occurred exporting to SQL Server.<br/>One or more of the rows already exist in the table.<br/><br/>"; ImportResultLabel.ForeColor = System.Drawing.Color.Red; } catch (Exception ex) { // Display error ImportResultLabel.Text = "An error occurred exporting to SQL Server.<br/>" + ex.Message + "<br/><br/>"; ImportResultLabel.ForeColor = System.Drawing.Color.Red; } finally { cmObjectContext.Dispose(); } }