コード例 #1
0
        protected void AddDemo_Click(object sender, EventArgs e)
        {
            try
            {
                var data = new CodeDemo();
                data.Name = DemoName.Text; // Grab the name from the form and put it in the object
                if (!string.IsNullOrWhiteSpace(Description.Text))
                {
                    data.Description = Description.Text;
                }

                var controller = new CodeDemoController();
                controller.AddDemo(data);
                PopulateGridView();
            }
            catch (Exception ex)
            {
                Exception inner = ex;
                // this nexrt statement drills down on the details of the execption
                while (inner.InnerException != null)
                {
                    inner = inner.InnerException;
                }
                string message;
                if (inner is DbEntityValidationException)
                {
                    message = ExtractValidationErrorDetails(inner as DbEntityValidationException);
                }
                else
                {
                    message           = inner.Message;
                    MessageLabel.Text = message;
                }
            }
        }
コード例 #2
0
 public void AddDemo(CodeDemo info)
 {
     using (var context = new DemoLibraryContext())
     {
         context.CodeDemos.Add(info);
         context.SaveChanges();
     }
 }