Db4objects.Db4o.Ext.DatabaseClosedException while updating in Gridview

575 Views Asked by At

I am trying to make a project which I can edit, update and delete my objects by using Gridview control, I got dbclosed exception error everytime I store new object. I could not find where I got error in fact. I am very new in .NET with DB4o programming, I could not find any good source where I can learn basics, so also any references will be totally appreciated. Thanks in advance.

//Pageload

     Db4oService.OpenDB();
     using (var patientdbcontainer = Db4oService.PatientDBContainer)
                    {
                       var list = patientdbcontainer.Query<Patient>(typeof(Patient)).ToList<Patient>();


                       GridView1.DataSource = list;
                       GridView1.DataBind();
                    }

//Update

public void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //var ID = new Guid((string)e.NewValues["ID"]);

            //    var Name = (string)e.NewValues["Name"];
            //    var IMEI = long.Parse(e.NewValues["IMEI"].ToString());

            Patient updatedPatient = new Patient()
            {
                ID = new Guid((string)e.NewValues["ID"]),
                IMEI =long.Parse(e.NewValues["IMEI"].ToString()),
                Name = (string)e.NewValues["Name"]
            };
            Patient deprecatedPatient;
            using (var patientdbcontainer = Db4oService.PatientDBContainer)
            {

                deprecatedPatient = patientdbcontainer.Query<Patient>(typeof(Patient)).Where<Patient>(n => n.ID == (new Guid((string)e.NewValues["ID"]))).FirstOrDefault<Patient>();
               // deprecatedPatient.Name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
                deprecatedPatient.ID = updatedPatient.ID;
                deprecatedPatient.Name = updatedPatient.Name;
                patientdbcontainer.Delete(deprecatedPatient);

                patientdbcontainer.Store(updatedPatient);
                patientdbcontainer.Commit();

                GridView1.EditIndex = -1;
                GridView1.DataBind();




            }
1

There are 1 best solutions below

2
On

1)If i undersand your code, maybe is better get the patient using the id, and update the properties and store changes (without create and delete).

2) You can try http://usefuldb4o.codeplex.com/.

This project is focused on work with ASP.NET. The source code has a example of ASP.NET with CRUD operations (source code folder Examples/AspNetWebSolutions)

This project has a Nuget package too, with a small example implementation: http://nuget.org/packages/UsefulDB4OToWeb