5 Replies - 2045 Views - Last Post: 11 April 2012 - 06:45 AM Rate Topic: -----

#1 plejer_adam  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 90
  • Joined: 17-May 10

Application crashes when using Entity Framework

Posted 10 April 2012 - 12:50 PM

Hello everyone, i am using Entity Framework in Visual Studio 2010 and i am facing the problem that my application crashes if using it long time..

this is the code which i am using in button click event

private void bttnDoniraj_Click(object sender, EventArgs e)
        {
            var context3 = new Suvenirnica10Entities();   
            List<DonacijaKlasa> listDonacija = new List<DonacijaKlasa>();
            string imePrezime = tbIme.Text + " " + tbPrezime.Text;


            /* ovo ispraviti */
            var query = (from m in context3.DonacijaPopis
                         select m.id).Max();

            int brojDonacije = query + 1;

            context3.Dispose();
            

            try
            {
                reportViewer1.Reset();
                reportViewer1.LocalReport.EnableExternalImages = true;
                reportViewer1.LocalReport.ReportEmbeddedResource = "Suvenirnica10.repDonacijaIzvjestaj.rdlc";

                ReportParameter prmDatum = new ReportParameter("datumDonacije", DateTime.Now.ToString());
                ReportParameter prmIznos = new ReportParameter("iznosDonacije", tbIznos.Text);
                ReportParameter prmImePrezime = new ReportParameter("donatorImePrezime", imePrezime);
                ReportParameter prmBrojDonacije = new ReportParameter("brojDonacije", brojDonacije.ToString());
                ReportParameter prmKonobar = new ReportParameter("primioDonaciju", ImeKonobara);

                reportViewer1.LocalReport.SetParameters(new ReportParameter[] { prmIznos, prmImePrezime, prmBrojDonacije, prmKonobar, prmDatum });

                ReportDataSource DataSource = new ReportDataSource("dsDonacija");
                using (var context2 = new Suvenirnica10Entities())
                {
                    context2.CommandTimeout = 10000;
                    var donacijaPopis = new DonacijaPopis { datum = DateTime.Now, izdao = ImeKonobara, iznos = Convert.ToDecimal(tbIznos.Text) };
                    context2.AddToDonacijaPopis(donacijaPopis);
                    context2.SaveChanges();

                    foreach (DataGridViewRow dr in dataGridView17.Rows)
                    {
                        var donacijaPopisPojedinacno = new DonacijaPopisPojedinacno
                        {
                            artikl = this.dataGridView17.Rows[dr.Index].Cells["ArtiklNaziv"].Value.ToString(),
                            datum = DateTime.Now,
                            id_donacije = donacijaPopis.id,
                            ime = tbIme.Text,
                            prezime = tbPrezime.Text,
                            velicina_artikla = this.dataGridView17.Rows[dr.Index].Cells["stringVelicinaArtikla"].Value.ToString(),
                            grupa_artikla = Convert.ToInt32(this.dataGridView17.Rows[dr.Index].Cells["grupaArtikla"].Value),
                            donirano_komada = Convert.ToInt32(this.dataGridView17.Rows[dr.Index].Cells["doniranoColumn"].Value),
                            id_artikl_glavni = Convert.ToInt32(this.dataGridView17.Rows[dr.Index].Cells["idArtiklGlavni"].Value),
                            id_velicina = Convert.ToInt32(this.dataGridView17.Rows[dr.Index].Cells["idVelicinaArtikla"].Value)
                        };

                        context2.AddToDonacijaPopisPojedinacno(donacijaPopisPojedinacno);
                        context2.SaveChanges();


                        int id_velicina = Convert.ToInt32(this.dataGridView17.Rows[dr.Index].Cells["idVelicinaArtikla"].Value);

                        var update = (from n in context2.ArtiklVelicina
                                      where
                                      n.id == id_velicina
                                      select n).Single();

                        update.kolicina = update.kolicina - Convert.ToInt32(this.dataGridView17.Rows[dr.Index].Cells["doniranoColumn"].Value);
                        context2.SaveChanges();
                    }

                }

                MessageBox.Show("Donacija je uspješno izvršena!", "Obavijest", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbIme.Text = "";
                tbPrezime.Text = "";
                tbIznos.Text = "";

                DeleteRows();
                ClearDataGridView();

                DataSource.Value = listDonacija;
                reportViewer1.LocalReport.DataSources.Add(DataSource);

                this.reportViewer1.RefreshReport();
                this.reportViewer1.Refresh();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Došlo je do pogreške prilikom pokušaja donacije : " + ex.Message, "Obavijest", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


i am disposing everytime my context and dont know why it is crashing !!

Thx for help !

Is This A Good Question/Topic? 0
  • +

Replies To: Application crashes when using Entity Framework

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Application crashes when using Entity Framework

Posted 10 April 2012 - 01:18 PM

Any error messages?
Have you checked the event logs?

You can use the Windows Performance Monitor to see how it's utilising your memory.

Also Using Statements are the preferred way of ensuring objects are disposed/closed correctly.

This post has been edited by negligible: 10 April 2012 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 plejer_adam  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 90
  • Joined: 17-May 10

Re: Application crashes when using Entity Framework

Posted 10 April 2012 - 01:32 PM

No errors, just Not Responding..

but i am Using Statements.. :/
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Application crashes when using Entity Framework

Posted 10 April 2012 - 03:46 PM

no, you're not. You're context would look like the following in a using statement

using (var context3 = new Suvenirnica10Entities())
{
    //rest of code
}



creating your context using the above method will keep you from having to call the dispose method. Once the code leaves that using block the dispose method is automatically called.

Edit: ah, I see you're using it for the second context, but not the first


Have you stepped through your code with the debugger to see where it's getting stuck? Also, isn't

context2.CommandTimeout = 10000;


a little excessive? Do you really want to try to make the timeout to be 10000 seconds? Even if you do, it won't happen. The default machine timeout takes precedent and will override this setting. From what I can find it seems the default machine timeout is about 10 minutes (600 seconds). If your insert operation is causing your application to die after about 10 minutes of running then this may be the reason why.

More information on the Machine.config timeout

This post has been edited by Nakor: 10 April 2012 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 plejer_adam  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 90
  • Joined: 17-May 10

Re: Application crashes when using Entity Framework

Posted 10 April 2012 - 10:51 PM

Thank you for helping me !
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Application crashes when using Entity Framework

Posted 11 April 2012 - 06:45 AM

Have you at least once tried debugging and seeing WHERE the application becomes unresponsive? Ever though it might be the report viewer and not Entity Framework that's causing the app to become unresponsive?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1