Datagrid selecteditem index out of range


Hey,

I have a datagrid in my ASP-app.

When I want do somethings on the datagrid, I always test if I selected an item otherwise I give a error.
ex.
if (this.DGselectedtransacties.SelectedItem == null)
{            
  error = "vvv";
}
else
{ do things
}

But sometimes (when I didn't select an item - in reality i had two items in the datagrid, I removed a selected item, then one item stays in the grid but it is not selected - I forgat to push on the select-command)
When I push button to do something with the selected item (but there isn't ) on the test (see above) i receive following error :
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I don't understand. I do the test especially to not have any problems. And then on the test he crashes.

What do I do wrong? Maybe when I select one item, I have to says to all the others that they are unselected? It is automatically?

Thanks for help.
haezebanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aacoolCommented:
The following code works fine for me - could you post your additional code, as well as any properties you have changed for your datagrid. Also, how are you loading the grid?

private void Button1_Click(object sender, System.EventArgs e)
{
      if (this.DataGrid1.SelectedItem == null)
      {
            TextBox1.Text = "No item selected";
      }
      else
      {
            TextBox1.Text = DataGrid1.SelectedItem.ToString();
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
haezebanAuthor Commented:
Hey,

here is all my code  for that form (it is a lot, and you will see that I am not a expert)

Short explanation
They are 4 grids on my screen : DGtransacties and DGselectedtransacties + DGfacturen and DGselectedfacturen and each grid has a button.
BTselectfactuur : the selecteditem in DGfacturen will be moved to DGselectedfacturen
DGunselectfactuur :the selecteditem in DGselectedfacturen will be moved to DGfacturen
BTselectbanktransactie :the selecteditem in DGtransacties will be moved to DGselectedtransacties
BTunselectbanktransactie :the selecteditem in DGselectedtransacties will be moved to DGtransacties

The MatchButton : Matches (make a link between) the items in DGselectedbanktransatie and DGselectedfacturen.  After match they don't return to the grids above, they are removed.

Mostly (in 99% of the cases) the switch between the datagrids runs correct, but sometimes not. And i think that it is in the case mentioned in previous mail.

Here is the code (a lot) - Thanks for the help

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Toep
{
      /// <summary>
      /// Summary description for RUMatchManueel.
      /// </summary>
      public class RUMatchManueel : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Button MatchButton;
            protected System.Web.UI.WebControls.ImageButton MUhome;
            protected System.Web.UI.WebControls.ImageButton MUnieuwdossier;
            protected System.Web.UI.WebControls.ImageButton MUdetaildossier;
            protected System.Web.UI.WebControls.ImageButton MUwijzigendossieradmin;
            protected System.Web.UI.WebControls.ImageButton MUwijzigendossierbetaalplan;
            protected System.Web.UI.WebControls.ImageButton MUwijzigendossierfacturatie;
            protected System.Web.UI.WebControls.ImageButton MUbrieven;
            protected System.Web.UI.WebControls.ImageButton MUbatch;
            protected System.Web.UI.WebControls.ImageButton MUadministrator;
            protected System.Web.UI.WebControls.DataGrid DGtransacties;
            protected System.Web.UI.WebControls.DataGrid DGfacturen;
            protected System.Web.UI.WebControls.DataGrid DGselectedtransacties;
            protected System.Web.UI.WebControls.DataGrid DGselectedfacturen;
            protected System.Web.UI.WebControls.ImageButton BTselectbanktransactie;
            protected System.Web.UI.WebControls.ImageButton BTselectfactuur;
            protected System.Web.UI.WebControls.ImageButton BTunselectbanktransactie;
            protected System.Web.UI.WebControls.ImageButton BTunselectfactuur;
            protected System.Web.UI.WebControls.TextBox TBfilter;
            protected System.Web.UI.WebControls.DropDownList DLfilter;
            protected System.Web.UI.WebControls.ImageButton MUcontractstop;
            protected System.Web.UI.WebControls.Button VorigeButton;
            protected System.Web.UI.WebControls.DropDownList DLBank;
            protected System.Web.UI.WebControls.Button ToekenenButton;
            protected System.Web.UI.WebControls.ImageButton BTunselectbanktransactieAll;
            protected System.Web.UI.WebControls.DropDownList DLcubicrekening;
            protected System.Web.UI.WebControls.ImageButton BTSplit;
            protected System.Web.UI.WebControls.ImageButton BTunsplit;
            
            RekeningUittreksel ru;
            RekeningUittreksels rekeninguittreksels;
            VorderingDossiers vorderingdossiers;
            Facturen selectedfacturen = new Facturen();
            Facturen onbetaaldefacturen;
            Facturen subsetonbetaaldefacturen;
            
            Transacties selectedtransacties = new Transacties();
            Transacties nietgematchtetransacties = new Transacties();
            Transacties deletedtransacties = new Transacties();

            private void Page_Load(object sender, System.EventArgs e)
            {
                  if (Session["Login"] == null)
                        Response.Redirect("Login.aspx");

                  this.MUbrieven.Visible = false;
                  this.MUcontractstop.Visible = false;
                  this.MUdetaildossier.Visible = false;
                  this.MUwijzigendossieradmin.Visible = false;
                  this.MUwijzigendossierbetaalplan.Visible = false;
                  this.MUwijzigendossierfacturatie .Visible = false;

                  // Rekeninguittreksels van geslecteerde bank

                  RecupOhraDB2 DB = new RecupOhraDB2();
                  rekeninguittreksels = DB.getRekeningUittreksels(DateTime.MinValue,DateTime.MaxValue).getRekeningUittrekselsByBank(DLBank.SelectedValue);            
      
                  if (!Page.IsPostBack)
                  {
                                    
                        // Not matched banktransactions
                        foreach (RekeningUittreksel ru in rekeninguittreksels)
                        {
                              foreach (Transactie trans in ru.Transacties.GetNietGematchteTransacties())
                              {
                                    nietgematchtetransacties.Add(trans);
                              }
                        }
                        this.DGtransacties.CurrentPageIndex = 0;
                        this.DGtransacties.DataSource = nietgematchtetransacties;
                        this.DGtransacties.DataBind();
                        Session["NietGematchteTransacties"] = nietgematchtetransacties;

                        // ******************* Unpaied invoices
                        // Zoek alle onbetaalde facturen en zet in object OnbetaaldeFacturen (of onvolledig betaalde) en binden
                        vorderingdossiers = DB.getOnbetaaldeFacturen();

                        // Zoek in deze dossiers alle onbetaalde facturen en neem de pointer op in de collectie onbetaalde facturen
                        onbetaaldefacturen = new Facturen();
                        foreach(VorderingDossier dos in vorderingdossiers)
                        {
                              foreach(Factuur fac in dos.Facturen)
                              {
                                    if (fac.IsBetaald() != 1)
                                          onbetaaldefacturen.Add(fac);
                              }
                        }

                        subsetonbetaaldefacturen = (Facturen) onbetaaldefacturen.Clone();

                        // Bind  Unpaied invoices
                        this.DGfacturen.CurrentPageIndex = 0;
                this.DGfacturen.DataSource = subsetonbetaaldefacturen;
                        this.DGfacturen.DataBind();
                        Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                        Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;
                        Session["VorderingDossiersOnbetaaldeFacturen"] = vorderingdossiers;
                        
                  
                        Session["SelectedFacturen"] = selectedfacturen;
                        Session["SelectedTransacties"] = selectedtransacties;
                        Session["DeletedTransacties"] = deletedtransacties;
                  }
                  else
                  {
                        onbetaaldefacturen = (Facturen) Session["OnbetaaldeFacturen"];
                        subsetonbetaaldefacturen = (Facturen) Session["SubsetOnbetaaldeFacturen"];
                        nietgematchtetransacties = (Transacties) Session["NietGematchteTransacties"];
                        selectedfacturen = (Facturen) Session["SelectedFacturen"];
                        selectedtransacties = (Transacties) Session["SelectedTransacties"];
                        vorderingdossiers = (VorderingDossiers) Session["VorderingDossiersOnbetaaldeFacturen"];
                        deletedtransacties = (Transacties) Session["DeletedTransacties"];
                  }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.DLBank.SelectedIndexChanged += new System.EventHandler(this.DLBank_SelectedIndexChanged);
                  this.BTselectbanktransactie.Click += new System.Web.UI.ImageClickEventHandler(this.BTselectbanktransactie_Click);
                  this.DGfacturen.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DGfacturen_PageIndexChanged);
                  this.DLfilter.SelectedIndexChanged += new System.EventHandler(this.DLfilter_SelectedIndexChanged);
                  this.TBfilter.TextChanged += new System.EventHandler(this.TBfilter_TextChanged);
                  this.BTselectfactuur.Click += new System.Web.UI.ImageClickEventHandler(this.BTselectfactuur_Click);
                  this.DGselectedtransacties.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DGselectedtransacties_PageIndexChanged);
                  this.ToekenenButton.Click += new System.EventHandler(this.ToekenenButton_Click);
                  this.BTunselectbanktransactie.Click += new System.Web.UI.ImageClickEventHandler(this.BTunselectbanktransactie_Click);
                  this.BTunselectbanktransactieAll.Click += new System.Web.UI.ImageClickEventHandler(this.BTunselectbanktransactieAll_Click);
                  this.BTSplit.Click += new System.Web.UI.ImageClickEventHandler(this.BTSplit_Click);
                  this.BTunsplit.Click += new System.Web.UI.ImageClickEventHandler(this.BTunsplit_Click);
                  this.DGselectedfacturen.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DGselectedfacturen_PageIndexChanged);
                  this.BTunselectfactuur.Click += new System.Web.UI.ImageClickEventHandler(this.BTunselectfactuur_Click);
                  this.VorigeButton.Click += new System.EventHandler(this.VorigeButton_Click);
                  this.MatchButton.Click += new System.EventHandler(this.MatchButton_Click);
                  this.MUhome.Click += new System.Web.UI.ImageClickEventHandler(this.MUhome_Click);
                  this.MUnieuwdossier.Click += new System.Web.UI.ImageClickEventHandler(this.MUnieuwdossier_Click);
                  this.MUbatch.Click += new System.Web.UI.ImageClickEventHandler(this.MUbatch_Click);
                  this.MUadministrator.Click += new System.Web.UI.ImageClickEventHandler(this.MUadministrator_Click);
                  this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion
            private void DLBank_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  // Legen geselecteerde transacties (facturen mogen blijven)
                  // Verwijderen uit DGselectedtransacties
                  selectedtransacties.Clear();
                  this.DGselectedtransacties.CurrentPageIndex = 0;
                  this.DGselectedtransacties.DataSource = selectedtransacties;
                  this.DGselectedtransacties.DataBind();

                  // Zoeken alle niet gematchte transacties van RU en binden
                  RecupOhraDB2 DB = new RecupOhraDB2();
                  rekeninguittreksels = DB.getRekeningUittreksels(DateTime.MinValue,DateTime.MaxValue).getRekeningUittrekselsByBank(DLBank.SelectedValue);            
      
                  // Niet gematchte transacties
                  nietgematchtetransacties.Clear();
                  foreach (RekeningUittreksel ru in rekeninguittreksels)
                  {
                        foreach (Transactie trans in ru.Transacties.GetNietGematchteTransacties())
                        {
                              nietgematchtetransacties.Add(trans);
                        }
                  }

                  this.DGtransacties.CurrentPageIndex = 0;
                  this.DGtransacties.DataSource = nietgematchtetransacties;
                  this.DGtransacties.DataBind();
                  Session["NietGematchteTransacties"] = nietgematchtetransacties;

                  // Huidige Collecties bewaren in sessionvars
                  Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                  Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;
                  Session["NietGematchteTransacties"] = nietgematchtetransacties;
                  Session["SelectedFacturen"] = selectedfacturen;
                  Session["SelectedTransacties"] = selectedtransacties;
                  Session["VorderingDossiersOnbetaaldeFacturen"] = vorderingdossiers;      
                  Session["DeletedTransacties"] = deletedtransacties;
            }

            private void BTselectbanktransactie_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
            
// Button to move the selected banktransaction to collection selectedtransactions
// this collection is bound to DGselectedtransacties

// WHY DO I CLONE? is it nesscary
                  if (this.DGtransacties.SelectedItem != null)
                  {
                        // Transactie clonen om in selectedtransacties te zetten
                        // Beide collecties moeten huneigen objecten hebben, geen werwijzing naar elkaar (probleem internecoll Id voor elke collectie)
                        Transactie trans = new Transactie();

                        trans = (Transactie) nietgematchtetransacties.getTransactieByInterneCollectieId(Convert.ToInt32(DGtransacties.SelectedItem.Cells[0].Text)).Clone();
                        // Add to DGselectedtransacties
                        selectedtransacties.Add(trans);
                        this.DGselectedtransacties.CurrentPageIndex = 0;
                        this.DGselectedtransacties.DataSource = selectedtransacties;
                        this.DGselectedtransacties.DataBind();

                        // remove from nietgematchetransactions
                        nietgematchtetransacties.removeTransactieByInterneCollectieId(Convert.ToInt32(DGtransacties.SelectedItem.Cells[0].Text));
                        this.DGtransacties.CurrentPageIndex = 0;
                        this.DGtransacties.DataSource = nietgematchtetransacties;
                        this.DGtransacties.DataBind();

                        // Huidige Collecties bewaren in sessionvars
                        Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                        Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;
                        Session["NietGematchteTransacties"] = nietgematchtetransacties;
                        Session["SelectedFacturen"] = selectedfacturen;
                        Session["SelectedTransacties"] = selectedtransacties;
                        Session["VorderingDossiersOnbetaaldeFacturen"] = vorderingdossiers;
                        Session["DeletedTransacties"] = deletedtransacties;
                  }
            }
            private void BTunselectbanktransactie_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
// Button to move the selected banktransaction to collection nietgematchtetransacties
// this collection is bound to DGtransacties
// The opposite of above

                  if (this.DGselectedtransacties.SelectedItem != null)
                  {
                        Transactie trans = new Transactie();
                        trans = (Transactie) selectedtransacties.getTransactieByInterneCollectieId(Convert.ToInt32(DGselectedtransacties.SelectedItem.Cells[0].Text)).Clone();
                        
                        // Toevoegen aan DGTransacties
                        nietgematchtetransacties.Add(trans);
                        this.DGtransacties.CurrentPageIndex = 0;
                        this.DGtransacties.DataSource = nietgematchtetransacties;
                        this.DGtransacties.DataBind();

                        // Verwijderen uit DGselectedtransacties
                        selectedtransacties.removeTransactieByInterneCollectieId(Convert.ToInt32(DGselectedtransacties.SelectedItem.Cells[0].Text));
                        this.DGselectedtransacties.CurrentPageIndex = 0;
                        this.DGselectedtransacties.DataSource = selectedtransacties;
                        this.DGselectedtransacties.DataBind();

                        // Huidige Collecties bewaren in sessionvars
                        Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                        Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;
                        Session["NietGematchteTransacties"] = nietgematchtetransacties;
                        Session["SelectedFacturen"] = selectedfacturen;
                        Session["SelectedTransacties"] = selectedtransacties;
                        Session["VorderingDossiersOnbetaaldeFacturen"] = vorderingdossiers;
                        Session["DeletedTransacties"] = deletedtransacties;
                  }
            }

            private void BTselectfactuur_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  if (this.DGfacturen.SelectedItem != null)
                  {            
                        Factuur fac = new Factuur();
                        fac = (Factuur) subsetonbetaaldefacturen.getFactuurByInterneCollectieId(Convert.ToInt32(DGfacturen.SelectedItem.Cells[0].Text)).Clone();

                        // Toevoegen aan DGselectedfacturen
                        selectedfacturen.Add(fac);
                        this.DGselectedfacturen.CurrentPageIndex = 0;
                        this.DGselectedfacturen.DataSource = selectedfacturen;
                        this.DGselectedfacturen.DataBind();

                        // Verwijderen uit DGfacturen
                        subsetonbetaaldefacturen.removeFactuurByInterneCollectieId(Convert.ToInt32(DGfacturen.SelectedItem.Cells[0].Text));
                        this.DGfacturen.CurrentPageIndex = 0;
                        this.DGfacturen.DataSource = subsetonbetaaldefacturen;
                        this.DGfacturen.DataBind();

                        // Huidige Collecties bewaren in sessionvars
                        Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                        Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;
                        Session["NietGematchteTransacties"] = nietgematchtetransacties;
                        Session["SelectedFacturen"] = selectedfacturen;
                        Session["SelectedTransacties"] = selectedtransacties;
                        Session["VorderingDossiersOnbetaaldeFacturen"] = vorderingdossiers;
                        Session["DeletedTransacties"] = deletedtransacties;
                  }      
            }

            private void BTunselectfactuur_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  if (this.DGselectedfacturen.SelectedItem != null)
                  {
                        Factuur fac = new Factuur();
                        fac = (Factuur) selectedfacturen.getFactuurByInterneCollectieId(Convert.ToInt32(DGselectedfacturen.SelectedItem.Cells[0].Text)).Clone();

                        // Toevoegen aan DGFacturen
                        subsetonbetaaldefacturen.Add(fac);
                        this.DGfacturen.CurrentPageIndex = 0;
                        this.DGfacturen.DataSource = subsetonbetaaldefacturen;
                        this.DGfacturen.DataBind();

                        // Verwijderen uit DGselectedfacturen
                        selectedfacturen.removeFactuurByInterneCollectieId(Convert.ToInt32(DGselectedfacturen.SelectedItem.Cells[0].Text));
                        this.DGselectedfacturen.CurrentPageIndex = 0;
                        this.DGselectedfacturen.DataSource = selectedfacturen;
                        this.DGselectedfacturen.DataBind();

                        // Huidige Collecties bewaren in sessionvars
                        Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                        Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;
                        Session["NietGematchteTransacties"] = nietgematchtetransacties;
                        Session["SelectedFacturen"] = selectedfacturen;
                        Session["SelectedTransacties"] = selectedtransacties;
                        Session["VorderingDossiersOnbetaaldeFacturen"] = vorderingdossiers;
                        Session["DeletedTransacties"] = deletedtransacties;
                  }      
            }

            public void DGtransacties_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                  DGtransacties.CurrentPageIndex = e.NewPageIndex;
                  
                  // Rebind the data to refresh the DataGrid control.
                  DGtransacties.DataSource = (Transacties) Session["NietGematchteTransacties"];
                  DGtransacties.DataBind();      
            }

            public void DGfacturen_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                  DGfacturen.CurrentPageIndex = e.NewPageIndex;
                  
                  // Rebind the data to refresh the DataGrid control.
                  DGfacturen.DataSource = (Facturen) Session["SubsetOnbetaaldeFacturen"];
                  DGfacturen.DataBind();      
            }

            public void DGselectedtransacties_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                  DGselectedtransacties.CurrentPageIndex = e.NewPageIndex;
                  
                  // Rebind the data to refresh the DataGrid control.
                  DGselectedtransacties.DataSource = (Transacties) Session["SelectedTransacties"];
                  DGselectedtransacties.DataBind();      
            }

            public void DGselectedfacturen_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                  DGselectedfacturen.CurrentPageIndex = e.NewPageIndex;
                  
                  // Rebind the data to refresh the DataGrid control.
                  DGselectedfacturen.DataSource = (Facturen) Session["SelectedFacturen"];;
                  DGselectedfacturen.DataBind();      
            }


            private void DLfilter_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if (this.DLfilter.SelectedValue == "GeenFilter")
                  {
                        TBfilter.Text = "";

                        subsetonbetaaldefacturen.Clear();
                        subsetonbetaaldefacturen = (Facturen) onbetaaldefacturen.Clone();

                        DGfacturen.CurrentPageIndex = 0;
                        DGfacturen.DataSource = onbetaaldefacturen;
                        DGfacturen.DataBind();      
                  }
            }

            private void TBfilter_TextChanged(object sender, System.EventArgs e)
            {
                  bool fout = false;
                  bool gevonden = false;
                  double bedrag = 0;
                  string str1 = "", str2 = "";      

// Filter on collection onbetaaldefacturen (= unpaid invoices)      

                  if (this.DLfilter.SelectedValue == "GeenFilter")
                  {
                        TBfilter.Text = "";

                        subsetonbetaaldefacturen.Clear();
                        subsetonbetaaldefacturen = (Facturen) onbetaaldefacturen.Clone();

                        DGfacturen.CurrentPageIndex = 0;
                        DGfacturen.DataSource = onbetaaldefacturen;
                        DGfacturen.DataBind();      
                  }
                  else
                  {
                        if (this.DLfilter.SelectedValue == "Bedrag" || this.DLfilter.SelectedValue == "SchadedossierNr" || this.DLfilter.SelectedValue == "VorderingdossierNr")
                        {
                              try
                              {
                                    bedrag = Convert.ToDouble(this.TBfilter.Text.Trim());
                              }
                              catch
                              {
                                    fout = true;
                                    switch (this.DLfilter.SelectedValue)
                                    {
                                          case "Bedrag" :
                                                Routines.FoutMeldingmetFocus("Ongeldig Bedrag","TBfilter",this.Page);
                                                break;
                                          case "SchadedossierNr" :
                                                Routines.FoutMeldingmetFocus("Ongeldig DossierNummer","TBfilter",this.Page);
                                                break;
                                          case "VorderingdossierNr" :
                                                Routines.FoutMeldingmetFocus("Ongeldig VorderingsNummer","TBfilter",this.Page);
                                                break;
                                    }
                              }
                        }

                        if (fout == false)
                        {
                              // subset heraanmaken
                              subsetonbetaaldefacturen.Clear();

                              switch (this.DLfilter.SelectedValue)
                              {
                                    case "Bedrag" :
                                          // Rechstreeks op factuur - geeft alle facturen van dit bedrag en groter
                                          subsetonbetaaldefacturen = onbetaaldefacturen.GetFacturenopBedrag(bedrag);
                                          break;
                                    case "SchadedossierNr" :
                                          // Hier moet men naar het parent object gaan (vorderingdossier)
                                          foreach(VorderingDossier dos in (VorderingDossiers) Session["VorderingDossiersOnbetaaldeFacturen"])
                                          {
                                                if (dos.SchadeDossierNr != this.TBfilter.Text) continue;

                                                foreach(Factuur fac in dos.Facturen)
                                                {
                                                      if (fac.IsBetaald() != 1)
                                                            subsetonbetaaldefacturen.Add(fac);
                                                }
                                          }
                                          break;
                                    case "VorderingdossierNr" :
                                          // Rechstreeks op facturen
                                          subsetonbetaaldefacturen = onbetaaldefacturen.GetFacturenbyVorderingid(Convert.ToInt32(this.TBfilter.Text));
                                          break;
                                    case "Naam" :
                                          // Hier moet men naar het parent object gaan (vorderingdossier)
                                          gevonden = false;
                                          
                                          foreach(VorderingDossier dos in (VorderingDossiers) Session["VorderingDossiersOnbetaaldeFacturen"])
                                          {
                                                foreach (Relatie rel in dos.Relaties)
                                                {
                                                      str1 = rel.Naam.Trim();
                                                      str1 = str1.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.Naam.Length));
                                                      str2 = this.TBfilter.Text.Trim();
                                                      str2 = str2.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.Naam.Length)).Trim();
                                                      if (str1.ToLower() == str2.ToLower())
                                                      {
                                                            gevonden = true;
                                                      }
                                                }

                                                if (gevonden == true)
                                                {
                                                      // neem alle facturen van dit dossier
                                                      foreach(Factuur fac in dos.Facturen)
                                                      {
                                                            if (fac.IsBetaald() != 1)
                                                                  subsetonbetaaldefacturen.Add(fac);
                                                      }
                                                      gevonden = false;
                                                }                        
                                          }
                                          break;
                                    case "Postcode" :
                                          // Hier moet men naar het parent object gaan (vorderingdossier)
                                          gevonden = false;
                                          
                                          foreach(VorderingDossier dos in (VorderingDossiers) Session["VorderingDossiersOnbetaaldeFacturen"])
                                          {
                                                foreach (Relatie rel in dos.Relaties)
                                                {
                                                      str1 = rel.PostCode.Trim();
                                                      str1 = str1.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.PostCode.Length));
                                                      str2 = this.TBfilter.Text.Trim();
                                                      str2 = str2.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.PostCode.Length)).Trim();
                                                      if (str1.ToLower() == str2.ToLower())
                                                      {
                                                            gevonden = true;
                                                      }
                                                }

                                                if (gevonden == true)
                                                {
                                                      // neem alle facturen van dit dossier
                                                      foreach(Factuur fac in dos.Facturen)
                                                      {
                                                            if (fac.IsBetaald() != 1)
                                                                  subsetonbetaaldefacturen.Add(fac);
                                                      }
                                                      gevonden = false;
                                                }      
                                          }
                                          break;
                                    case "Gemeente" :
                                          // Hier moet men naar het parent object gaan (vorderingdossier)
                                          gevonden = false;
                                          
                                          foreach(VorderingDossier dos in (VorderingDossiers) Session["VorderingDossiersOnbetaaldeFacturen"])
                                          {
                                                foreach (Relatie rel in dos.Relaties)
                                                {
                                                      str1 = rel.Gemeente.Trim();
                                                      str1 = str1.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.Gemeente.Length));
                                                      str2 = this.TBfilter.Text.Trim();
                                                      str2 = str2.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.Gemeente.Length)).Trim();
                                                      if (str1.ToLower() == str2.ToLower())
                                                      {
                                                            gevonden = true;
                                                      }
                                                }

                                                if (gevonden == true)
                                                {
                                                      // neem alle facturen van dit dossier
                                                      foreach(Factuur fac in dos.Facturen)
                                                      {
                                                            if (fac.IsBetaald() != 1)
                                                                  subsetonbetaaldefacturen.Add(fac);
                                                      }
                                                      gevonden = false;
                                                }
                                                
                                          }
                                          break;
                                    case "Straat" :
                                          // Hier moet men naar het parent object gaan (vorderingdossier)
                                          gevonden = false;
                                          
                                          foreach(VorderingDossier dos in (VorderingDossiers) Session["VorderingDossiersOnbetaaldeFacturen"])
                                          {
                                                foreach (Relatie rel in dos.Relaties)
                                                {
                                                      str1 = rel.Straat.Trim();
                                                      str1 = str1.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.Straat.Length));
                                                      str2 = this.TBfilter.Text.Trim();
                                                      str2 = str2.Substring(0, Math.Min(this.TBfilter.Text.Length,rel.Straat.Length)).Trim();
                                                      if (str1.ToLower() == str2.ToLower())
                                                      {
                                                            gevonden = true;
                                                      }
                                                }

                                                if (gevonden == true)
                                                {
                                                      // neem alle facturen van dit dossier
                                                      foreach(Factuur fac in dos.Facturen)
                                                      {
                                                            if (fac.IsBetaald() != 1)
                                                                  subsetonbetaaldefacturen.Add(fac);
                                                      }
                                                      gevonden = false;
                                                }
                                                
                                          }
                                          break;
                                    case "OGM" :
                                          // Rechstreeks op facturen
                                          subsetonbetaaldefacturen = onbetaaldefacturen.GetFacturenopOGM(this.TBfilter.Text);
                                          break;
                                    case "Mededeling" :
                                          // Rechstreeks op factuur - geeft alle facturen van dit bedrag en groter
                                          break;
                              } // switch
                              DGfacturen.CurrentPageIndex = 0;
                              DGfacturen.DataSource = subsetonbetaaldefacturen;
                              DGfacturen.DataBind();            
                        } // if (fout == false)
                  } // else - if (this.DLfilter.SelectedValue == "GeenFilter")      
            }

            private void ToekenenButton_Click(object sender, System.EventArgs e)
            {
                  
// Matching of the selected transactions to an account number
                  Matchingen matchingen = new Matchingen();
                  Matching matching;

                  // Rechstreeks toekenen van een cubicrekening
                  // Dwz. dat de transactie niets met vorderingen te maken heeft

                  // Transactie matchen aan rekeningnummer
                  matching = new Matching();
                  matching.FactuurId = 0;
                  matching.IsManueel = true;
                  matching.MatchDt = DateTime.Today;
                  matching.RekeningNummerBkh = this.DLcubicrekening.SelectedValue;
                  matching.TransactieId = selectedtransacties.getItem(0).TransactieId;
                  matching.GematchteBedrag = selectedtransacties.getItem(0).Bedrag;
                  matching.ObjectState = ObjectState.Inserted;
                  matchingen.Add(matching);

                  // Naar Database
                  if (matchingen.Count > 0)
                  {
                        RecupOhraDB2 DB = new RecupOhraDB2();
                        DB.insertMatchingen(matchingen);
                  }

                  // Transactie verwijderen uit collectie
                  selectedtransacties.Clear();
                  this.DGselectedtransacties.CurrentPageIndex = 0;
                  this.DGselectedtransacties.DataSource = selectedtransacties;
                  this.DGselectedtransacties.DataBind();      
            }
            
            private void BTunselectbanktransactieAll_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {            
            }      

            private void MatchButton_Click(object sender, System.EventArgs e)
            {
                  
// Matching of the selected transactions to the selected unpaid invoices
// (that wil say that the selected line on my bakaccount was for the paiment of that invoice)
// Remove the transaction and the invoices (because they are matched)

                  RecupOhraDB2 DB = new RecupOhraDB2();

                  // Algemeen : Totaalbedrag banktransacties <= totaalbedrag facturen
                  if (selectedtransacties.GetTotaalBedragGeselecteerdeTransacties() > selectedfacturen.GetTotaalOpenBedragGeselecteerdeFacturen())
                        Routines.FoutMelding("Bedrag banktransacties > bedrag facturen", this.Page);
                  else
                  {
                        // Ofwel 1 - n of n - 1 NIET N-N
                        if (selectedtransacties.Count > 1 && selectedfacturen.Count > 1)
                              Routines.FoutMelding("Meerdere banktransacties aan meerdere facturen is niet toegelaten", this.Page);
                        else
                        {
                              if (selectedtransacties.GetTotaalBedragGeselecteerdeTransacties() <= 0)
                              {
                                    Routines.FoutMelding("Transactiebedrag <= 0 kan niet gematched worden", this.Page);
                              }
                              else
                              {                        
                                    Matchingen matchingen = new Matchingen();
                                    bool match = false;
                                    Matching matching;

                                    // Eerste transacties (gesplistste) naar de Db om een transId te krijgen
                                    foreach(Transactie tr in this.selectedtransacties)
                                    {
                                          if (tr.ObjectState == ObjectState.Inserted)
                                          {
                                                tr.TransactieId = DB.insertTransactieReturnId(tr);
                                          }
                                          if (tr.ObjectState == ObjectState.Deleted)
                                          {
                                                DB.deleteTransactie(tr);
                                          }
                                    }

                                    foreach(Transactie tr in this.nietgematchtetransacties )
                                    {
                                    
                                          if (tr.ObjectState == ObjectState.Inserted)
                                          {
                                                tr.TransactieId = DB.insertTransactieReturnId(tr);
                                          }
                                          if (tr.ObjectState == ObjectState.Deleted)
                                          {
                                                DB.deleteTransactie(tr);
                                          }
                                    }

                                    foreach(Transactie tr in this.deletedtransacties)
                                    {
                                          DB.deleteTransactie(tr);
                                    }
                  
                                    // 1-1
                                    if (selectedtransacties.Count == 1 && selectedfacturen.Count == 1)
                                    {
                                          // Volledige 1 op 1 macth of een gedeeltelijke betaling van een factuur
                                          // In beide objecten aanduiding matching
                                          selectedtransacties.getItem(0).WV_IsMatched = true;
                                          if (selectedfacturen.getItem(0).TeRecupererenBedrag - selectedfacturen.getItem(0).GetGevorderdBedrag() >  selectedtransacties.getItem(0).Bedrag)
                                                selectedfacturen.getItem(0).WV_IsMatched = false;  // gedeeltelijk
                                          else
                                                selectedfacturen.getItem(0).WV_IsMatched = true;

                                          // Maak matching object
                                          matching = new Matching();
                                          matching.FactuurId = selectedfacturen.getItem(0).FactuurId;
                                          matching.IsManueel = true;
                                          matching.MatchDt = DateTime.Today;
                                          if (selectedfacturen.getItem(0).GetVorderingDossierInfo() != null)
                                                matching.RekeningNummerBkh = selectedfacturen.getItem(0).GetVorderingDossierInfo().GetBkhRekening();
                                          else
                                                matching.RekeningNummerBkh = "ONBEKEND";

                                          matching.TransactieId = selectedtransacties.getItem(0).TransactieId;
                                          matching.GematchteBedrag = selectedtransacties.getItem(0).Bedrag;
                                          matching.ObjectState = ObjectState.Inserted ;
                                          // matching.BoekDt =
                                                
                                          matchingen.Add(matching);
                                          match = false;

                                          // Invoegen van deze betaling in de collectie betalingen aan de factuur
                                          Betaling betaling = new Betaling();
                                          betaling.BankId = selectedtransacties.getItem(0).BankId;
                                          betaling.Bedrag = selectedtransacties.getItem(0).Bedrag;
                                          betaling.BetaalDt =  DateTime.Today; // RU
                                          betaling.Jaar =  selectedtransacties.getItem(0).Jaar;
                                          betaling.OGM = selectedfacturen.getItem(0).OGM;
                                          betaling.TransactieId = selectedtransacties.getItem(0).TransactieId;
                                          betaling.TransactieVolgNr = selectedtransacties.getItem(0).TransactieVolgNr;
                                          betaling.VolgNr = selectedtransacties.getItem(0).VolgNr;
                              
                                          selectedfacturen.getItem(0).Betalingen.Add(betaling);
                                    
                                    }
                                    else
                                    {
                                          if (selectedtransacties.Count > 1 && selectedfacturen.Count == 1)
                                          {
                                                double totaaltoegekendbedrag = 0;
                                          
                                                // Een factuur wordt betaald door meerdere banktransacties
                                                selectedfacturen.getItem(0).WV_IsMatched = true;
                                          
                                                foreach (Transactie trans in selectedtransacties)
                                                {
                                                      if (trans.WV_IsMatched == true)
                                                            continue;

                                                      // In beide objecten aanduiding matching
                                                      trans.WV_IsMatched = true;
                                          
                                                      // Maak matching object
                                                      matching = new Matching();
                                                      matching.FactuurId = selectedfacturen.getItem(0).FactuurId;
                                                      matching.IsManueel = true;
                                                      matching.MatchDt = DateTime.Today;
                                                      if (selectedfacturen.getItem(0).GetVorderingDossierInfo() != null)
                                                            matching.RekeningNummerBkh = selectedfacturen.getItem(0).GetVorderingDossierInfo().GetBkhRekening();
                                                      else
                                                            matching.RekeningNummerBkh = "ONBEKEND";

                                                      matching.TransactieId = trans.TransactieId;
                                                      matching.GematchteBedrag = trans.Bedrag;
                                                      matching.ObjectState = ObjectState.Inserted ;
                                                      // matching.BoekDt =
                                                
                                                      matchingen.Add(matching);
                                                      match = false;
                                                
                                                      totaaltoegekendbedrag = totaaltoegekendbedrag + trans.Bedrag;

                                                      // Invoegen van deze betaling in de collectie betalingen aan de factuur
                                                      Betaling betaling = new Betaling();
                                                      betaling.BankId = trans.BankId;
                                                      betaling.Bedrag = trans.Bedrag;
                                                      betaling.BetaalDt =  DateTime.Today; // RU
                                                      betaling.Jaar =  trans.Jaar;
                                                      betaling.OGM = selectedfacturen.getItem(0).OGM;
                                                      betaling.TransactieId = trans.TransactieId;
                                                      betaling.TransactieVolgNr = trans.TransactieVolgNr;
                                                      betaling.VolgNr = trans.VolgNr;
                              
                                                      selectedfacturen.getItem(0).Betalingen.Add(betaling);

                                                } // foreach transactie

                                                if (selectedfacturen.getItem(0).TeRecupererenBedrag - (selectedfacturen.getItem(0).GetGevorderdBedrag() - totaaltoegekendbedrag)  >  totaaltoegekendbedrag)
                                                      selectedfacturen.getItem(0).WV_IsMatched = false;  // gedeeltelijk
                                                else
                                                      selectedfacturen.getItem(0).WV_IsMatched = true;

                                          } // aantal transacties > 1

                                          if (selectedtransacties.Count == 1 && selectedfacturen.Count > 1)
                                          {
                                                // Een Transactie wordt gesplitst over meerder facturen
                                                selectedtransacties.getItem(0).WV_IsMatched = true;
                                                double resterendbedrag = selectedtransacties.getItem(0).Bedrag;
                                          
                                                // Zou moeten in volgorde van vervaldag zijn
                                                selectedfacturen.SortByVervalDt("ASC");
                                                foreach (Factuur fac in selectedfacturen)
                                                {

                                                      if (fac.WV_IsMatched == true)
                                                            continue;

                                                      // Maak matching object
                                                      matching = new Matching();
                                                      matching.FactuurId = fac.FactuurId;
                                                      matching.IsManueel = true;
                                                      matching.MatchDt = DateTime.Today;
                                                      if (fac.GetVorderingDossierInfo() != null)
                                                            matching.RekeningNummerBkh = fac.GetVorderingDossierInfo().GetBkhRekening();
                                                      else
                                                            matching.RekeningNummerBkh = "ONBEKEND";

                                                      matching.TransactieId = selectedtransacties.getItem(0).TransactieId;
                                                      if (resterendbedrag >= fac.TeRecupererenBedrag)
                                                      {
                                                            matching.GematchteBedrag = fac.TeRecupererenBedrag;
                                                            fac.WV_IsMatched = true;
                                                      }
                                                      else
                                                      {
                                                            matching.GematchteBedrag = resterendbedrag;
                                                            // Niet volledig gematched
                                                            fac.WV_IsMatched = false;
                                                      }

                                                      // matching.BoekDt =
                                                      matching.ObjectState = ObjectState.Inserted ;
                                                
                                                      matchingen.Add(matching);
                                                      match = false;

                                                      // Invoegen van deze betaling in de collectie betalingen aan de factuur
                                                      Betaling betaling = new Betaling();
                                                      betaling.BankId = selectedtransacties.getItem(0).BankId;
                                                      betaling.Bedrag = matching.GematchteBedrag;
                                                      betaling.BetaalDt =  DateTime.Today; // RU
                                                      betaling.Jaar =  selectedtransacties.getItem(0).Jaar;
                                                      betaling.OGM = fac.OGM;
                                                      betaling.TransactieId = selectedtransacties.getItem(0).TransactieId;
                                                      betaling.TransactieVolgNr = selectedtransacties.getItem(0).TransactieVolgNr;
                                                      betaling.VolgNr = selectedtransacties.getItem(0).VolgNr;
                              
                                                      fac.Betalingen.Add(betaling);
                                                                                                
                                                      // resterendbedrag na macthing aan deze factuur
                                                      resterendbedrag = resterendbedrag - fac.TeRecupererenBedrag;

                                                } // Foreach factuur
                                          } // aantal facturen > 1
                                    } // Else aantal facturen = 1 AND aantal transacties = 1
                        
                                    // Naar database

                                    if (matchingen.Count > 0)
                                    {      
                                          DB.insertMatchingen(matchingen);
                                    }

                                    // na matching verwijderen uit grids (enkel de volledig gematchte), niet volledig gematchte eerst terug naar boven
                                    foreach(Factuur fac in selectedfacturen)
                                    {
                                          if (fac.WV_IsMatched != true)
                                          {
                                                // Toevoegen aan DGFacturen
                                                subsetonbetaaldefacturen.Add(fac);
                                                this.DGfacturen.CurrentPageIndex = 0;
                                                this.DGfacturen.DataSource = subsetonbetaaldefacturen;
                                                this.DGfacturen.DataBind();
                                          }
                                    }

                                    selectedtransacties.Clear();
                                    this.DGselectedtransacties.CurrentPageIndex = 0;
                                    this.DGselectedtransacties.DataSource = selectedtransacties;
                                    this.DGselectedtransacties.DataBind();

                                    selectedfacturen.Clear();
                                    this.DGselectedfacturen.CurrentPageIndex = 0;
                                    this.DGselectedfacturen.DataSource = selectedtransacties;
                                    this.DGselectedfacturen.DataBind();
                        
                              }  // Else aantal facturen = N AND aantal transacties = N
                        }  // else negatief bedrag
                  } // Else bedrag transacties > bedrag facturen

                  // Huidige Collecties bewaren in sessionvars
                  Session["OnbetaaldeFacturen"] = onbetaaldefacturen;
                  Session["NietGematchteTransacties"] = nietgematchtetransacties;
                  Session["SelectedFacturen"] = selectedfacturen;
                  Session["SelectedTransacties"] = selectedtransacties;
                  Session["SubsetOnbetaaldeFacturen"] = subsetonbetaaldefacturen;      
                  Session["DeletedTransacties"] = deletedtransacties;
            }

            private void BTSplit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
            
            }

            private void BTunsplit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  
            }      

            private void VorigeButton_Click(object sender, System.EventArgs e)
            {
                  Response.Redirect("Batch.aspx");      
            }      
            

            // ************************************
            // **********************   Menu Opties
            // *************************************
            private void MUhome_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  // Home
                  Session["Vorderingdossier"] = null;
                  Session["Mode"] = "";
                  Response.Redirect("Zoek.aspx");      
            }

            private void MUnieuwdossier_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {      
                  Session["Mode"] = "ADD";
                  Session["ZoekSchadeDossier"] = "Nee";
                  Session["VorderingDossier"] = null;
                  Response.Redirect("New_Dossier.aspx");
            }

            private void MUbatch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  Session["Mode"] = "";
                  Response.Redirect("Batch.aspx");
            }

            private void MUadministrator_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  Session["Mode"] = "";
                  Response.Redirect("Administrator.aspx");
            }                  
      }
}




0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.