How to do a search in and sort a multidimensional List of objects in C#?

ruud00000
ruud00000 used Ask the Experts™
on
(C#, Compact Framework 3.5, Visual Studio 2008 Professional)

I want to save the values in a form into an array until I'm finished adding values and then commit the values in the array to the database at once. I don't want to make use of a table adapter, gridview etc due to limited resources (pda), speed, need for customisation of dialogs etc.

Like this, where I first execute opslaanButton on adding each new item to the array and then execute commitButton to write it to the database.
 
private void centrumOpslaanButtton_Click(object sender, EventArgs e)
        {
            centra[itemTeller] = new Centrum_oud();
            centra[itemTeller].centrumId = int.Parse(centrumIdTextBox.Text);
            centra[itemTeller].naam = centrumNaamTextBox.Text;
            centra[itemTeller].plaats = centrumPlaatsTextBox.Text;
            centra[itemTeller].banen = int.Parse(centrumBanenTextBox.Text);
            itemTeller += 1;
            positie = itemTeller - 1;
        }

        private void centrumCommitButton_Click(object sender, EventArgs e)
        {
            using (SqlCeConnection connection = new SqlCeConnection(connectString))
            {
                connection.Open();

                // Start a local transaction.
                SqlCeTransaction sqlTran = connection.BeginTransaction();

                // Enlist the command in the current transaction.
                SqlCeCommand command = connection.CreateCommand();
                command.Transaction = sqlTran;

                try
                {
                    int rowsaffected = 0;
                    for (int i = 0; i < centra.Length; i += 1)
                    {
                        if (centra[i] != null)
                        {
                            command.CommandText = "INSERT INTO Centrum (centrumId, naam, plaats, banen) VALUES ";
                            command.CommandText += "(" + centra[i].centrumId + ", N'" + centra[i].naam + "', N'" + centra[i].plaats + "', " + centra[i].banen + ");\n";
                            rowsaffected += command.ExecuteNonQuery();
                        }
                    }

                    sqlTran.Commit();
                    centrumToonTextBox.Text = rowsaffected + " records zijn in de database opgeslagen\r\n";
                    for (int i = 0; i < centra.Length; i += 1)
                    {
                        centra[i] = null;
                    }
                    itemTeller = 0;
                    positie = - 1;
                    centrumIdTextBox.Text = "";
                    centrumNaamTextBox.Text = "";
                    centrumPlaatsTextBox.Text = "";
                    centrumBanenTextBox.Text = "";
                }
                catch (Exception ex)
                {
                    centrumToonTextBox.Text = ex.Message + "\r\nDe records zijn niet in de database opgeslagen\r\n";
                    sqlTran.Rollback();
                }
            }
        }

Open in new window


Now I want to generalize this piece of code, so I have to read the contents (.Text property) of all controls of type textBox in the form and match them to the appropriate fields in the database table. My idea was to create a List of Fields where Field is a class that contains the properties Tabindex, Name, Text (and others) like this, since the values contained are of different types (int and string) so cannot be put in an array.
 
public class Field
    {
        string naam;
        string type;
        int length;
        int tabIndex;
        string text;

        public Field(string naam, string type, int length, int tabIndex, string text) 
        {
            this.naam = naam;
            this.type = type;
            this.length = length;
            this.tabIndex = tabIndex;
            this.text = text;
        }

        public Field(string naam, string text, int tabIndex)
        {
            this.naam = naam;
            this.tabIndex = tabIndex;
            this.text = text;
        }

        public string Naam
        {
            get { return naam; }
            set { naam = value; }
        }

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        public int Length
        {
            get { return length; }
            set { length = value; }
        }

        public int TabIndex
        {
            get { return length; }
            set { length = value; }
        }

        public string Text
        {
            get { return type; }
            set { type = value; }
        }
    }

Open in new window


Thereby I would like to sort that list on property TabIndex, which must be possible using a class that implements the IComparer interface. And I want to search the list for a Field object who's name property value matches the name of the databasetablefield who's value i want to update (record by record) and this search should return the value of the Text property...

So now I have a list of FormControl attributes, filled as follows (in a loop):
 
panel2Fields.Add(new Field(this.panel2.Controls[i].Name, this.panel2.Controls[i].Text, this.panel2.Controls[i].TabIndex));

Open in new window


And need to write a comparer class and call that, to sort on TabIndex and need a way to do a search for i.e. an abject who's Name property value is centrumPlaatsTextBoxDb when I need the Text property's value of field Plaats (so I need to do a search for a Name that contains the substring 'plaats').

How is this done?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Why don't you do something like this:

private void GetData () {
  entra[itemTeller] = new Centrum_oud();
  foreach (Control ctrl in thisForm.Controls) {
    switch (ctrl.Name) {
       case "centrumId" :
           centra[itemTeller].centrumId = int.parse(ctrl.Text);
           break;
       case "naam" :
           centra[itemTeller].naam = ctrl.Text;
           break;
       case "plaats" :
           centra[itemTeller].plaats = ctrl.Text;
           break;
       case "banen" :
           centra[itemTeller].banen = int.parse(ctrl.Text);
           break;
    }
     itemTeller++;
     positie = itemTeller - 1;
  }

This function will cycle through all your controls on your form and populate your structure.

Or have I misunderstood your question?

Author

Commented:
Thanks for your reply!

Well the idea is that there are about 25 tables and I would prefer to have one set of methods to deal with all the tables rather than having to copy that set of methods 25 times (program size and maintenance...).

Taking it further from your code, would you have a suggestion how to substitute the following 'constants' for variables:
- array name centra refers to table name 'centrum' ,
- centrumId, naam, plaats, banen are the names of the fields of table centrum,
- centrumIdTextBoxDb, centrumNaamTextBoxDb, centrumPlaatsTextBoxDb, centrumBanenTextBoxDb are the names of the textBox controls who's .Text property contains the values to store (I could adjust the naming conventions if that would make things easier).

Commented:
well... there's a way to convert a string to a corresponding variable name and this post http://ubuntuforums.org/showpost.php?s=674a800da1045dc1702ff443791033ed&p=3497371&postcount=2" does a good job of describing this technique.

Does this help?

So, how does your form look? do you have 25 sets of text boxes?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
I adapted your code to fit in, like this:
 
private void GetData()
        {
            string field01Text;
            bool field01;
            foreach (Control ctrl1 in this.centrumPanelTop.Controls)
            {
                field01Text = "centrumIdTextBox";
                field01 = ctrl1.Name == field01Text;
                switch (field01)
                {
                    case true:
                        centra[itemTeller].centrumId = int.Parse(ctrl1.Text);
                        break;
                }
            }

            string[] field02Text = {"centrumNaamTextBox", "centrumPlaatsTextBox", "centrumBanenTextBox"};
            foreach (Control ctrl2 in this.centrumPanelMidden.Controls)
            {
                if (ctrl2.Name == field02Text[0])
                { centra[itemTeller].naam = ctrl2.Text; }
                if (ctrl2.Name == field02Text[1])
                { centra[itemTeller].plaats = ctrl2.Text; }
                if (ctrl2.Name == field02Text[2])
                { centra[itemTeller].banen = int.Parse(ctrl2.Text); }
            }
        }

Open in new window


The controls are inside panels so I evaluate each panel instead of one form.

Furthermore I put the texboxnames in an array (field02Text). Since a case statement can only evaluate a constant a substituted the switch for if statements.

Now I still have centra[itemTeller].naam etc where 'naam' would need to be replaced by something else; I would think of an (enum or an)  indexer so i can reference to centrum[itemTeller].naam by using: centra[itemTeller].[0].

Here's the code, I haven't tested yet...
 
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace KegelTelApp04
{
    class Centrum_oud
    {
        private int p_banen;
        private string p_naam;
        private string p_plaats;
        private int p_centrumId;
        Centrum_oud[] items;

        // constructors
        public Centrum_oud() 
        {
        }

        // indexers
        public Field this[int index]
        {
            get { return items[index]; }
            set { items[index] = value; OnChanged(); }
        }

        protected virtual void OnChanged()
        {
            if (Changed != null) Changed(this, EventArgs.Empty);
        }

        public int banen
        {
            get
            {
                return p_banen;
            }
            set
            {
                p_banen = value;
            }
        }

        public string naam
        {
            get
            {
                return p_naam;
            }
            set
            {
                p_naam = value;
            }
        }

        public string plaats
        {
            get
            {
                return p_plaats;
            }
            set
            {
                p_plaats = value;
            }
        }

        public int centrumId
        {
            get
            {
                return p_centrumId;
            }
            set
            {
                p_centrumId = value;
            }
        }

        public void addCentrum()
        {
            throw new System.NotImplementedException();
        }

        public void deleteCentrum()
        {
            throw new System.NotImplementedException();
        }

        public void changeCentrum()
        {
            throw new System.NotImplementedException();
        }

        // event
        public event EventHandler Changed;

    }

}

Open in new window

Commented:
OK, let me ask you this question... is the number of fields and types of your fields the same for every case (out of your 25 cases)? If so, you can create one class that would house all of your data. It will have an extra field though to specify the form type. Then, you'll have a list of type 'A' and you can differentiate who is who based on that type field.
i.e.
if (MyClass.ClassType == "centrum_oud") { // do something }

Does that make sense?

Author

Commented:
Here are the table definitions:
 
public void createTables(object sender, EventArgs e, string connectString)
        {
            using (SqlCeConnection connection = new SqlCeConnection(connectString))
            {
                connection.Open();

                // Start a local transaction.
                SqlCeTransaction sqlTran = connection.BeginTransaction();

                // Enlist the command in the current transaction.
                SqlCeCommand command = connection.CreateCommand();
                command.Transaction = sqlTran;
                messageList.Clear();
                int rowsaffected = 0;
                int FK_created = 0;
                try
                {
                    // eerst tabellen maken met pk's, daarna fk's roevoegen
                    command.CommandText = "CREATE TABLE centrum (centrumId INTEGER NOT NULL, naam NVARCHAR(25), plaats NVARCHAR(25), banen INTEGER, CONSTRAINT PK_CENTRUM PRIMARY KEY (CENTRUMID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE speler_speler (koppelId INTEGER NOT NULL, speler INTEGER, speeltmet INTEGER, voorkeur INTEGER, CONSTRAINT PK_SPELER_SPELER PRIMARY KEY (KOPPELID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE club (clubId INTEGER NOT NULL, centrum INTEGER, naam NVARCHAR(41), plaats NVARCHAR(49), CONSTRAINT PK_CLUB PRIMARY KEY (CLUBID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE bal (balId INTEGER NOT NULL, eigenaar INTEGER, naam NVARCHAR(15), CONSTRAINT PK_BAL PRIMARY KEY (BALID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE eigenaar (eigenaarId INTEGER NOT NULL, speler INTEGER, club INTEGER, CONSTRAINT PK_EIGENAAR PRIMARY KEY (EIGENAARID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spel_club (spel INTEGER NOT NULL, club INTEGER NOT NULL, CONSTRAINT PK_SPEL_CLUB PRIMARY KEY (SPEL, CLUB))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE beurt (beurtId INTEGER NOT NULL, beurt INTEGER, speler INTEGER, spel INTEGER, baan INTEGER, centrum INTEGER, bal INTEGER, CONSTRAINT PK_BEURT PRIMARY KEY (BEURTID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE sessie (sessieId INTEGER NOT NULL, dag INTEGER, maand INTEGER, jaar INTEGER, CONSTRAINT PK_SESSIE PRIMARY KEY (SESSIEID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE sessie_centrum (sessie INTEGER NOT NULL, centrum INTEGER NOT NULL, CONSTRAINT PK_SESSIE_CENTRUM PRIMARY KEY (SESSIE, CENTRUM))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spel (spelId INTEGER NOT NULL, spelvariant INTEGER, sessie INTEGER, spelers INTEGER, teams INTEGER, dag INTEGER, maand INTEGER, jaar INTEGER, CONSTRAINT PK_SPEL PRIMARY KEY (SPELID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE speler (spelerId INTEGER NOT NULL, club INTEGER, bal INTEGER, team INTEGER, volgorde INTEGER, voornaam NVARCHAR(39), achternaam NVARCHAR(39), tussenvoegsel NVARCHAR(7), gddag INTEGER, gdmaand INTEGER, gdjaar INTEGER, straat NVARCHAR(39), huisnr NVARCHAR(6), telefoonvast NVARCHAR(11), telefoonmobiel NVARCHAR(11), partnervoornaam NVARCHAR(39), partnerachternaam NVARCHAR(39), partnertv NVARCHAR(15), partnergddag INTEGER, partnergdmaand INTEGER, partnergdjaar INTEGER, CONSTRAINT PK_SPELER PRIMARY KEY (SPELERID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spelerrang (rang INTEGER NOT NULL, klasse NVARCHAR(1) NOT NULL, spel INTEGER NOT NULL, speler INTEGER NOT NULL, CONSTRAINT PK_SPELERRANG PRIMARY KEY (RANG, KLASSE, SPEL))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE speler_spel_bal (speler INTEGER NOT NULL, spel INTEGER NOT NULL, bal INTEGER NOT NULL, CONSTRAINT PK_SPELER_SPEL_BAL PRIMARY KEY (SPELER, SPEL))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spelvariant (spelvariantId INTEGER NOT NULL, beurten INTEGER, worpen INTEGER, naam NVARCHAR(25), spellen INTEGER, thuis BIT, CONSTRAINT PK_SPELVARIANT PRIMARY KEY (SPELVARIANTID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE baan_centrum (baanId INTEGER NOT NULL, baan INTEGER, centrum INTEGER, CONSTRAINT PK_BAAN_CENTRUM PRIMARY KEY (BAANID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spel_baan (spel INTEGER NOT NULL, baan INTEGER NOT NULL, CONSTRAINT PK_SPEL_BAAN PRIMARY KEY (SPEL, BAAN))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spel_centrum (spel INTEGER NOT NULL, centrum INTEGER NOT NULL, CONSTRAINT PK_SPEL_CENTRUM PRIMARY KEY (SPEL, CENTRUM))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE team (teamId INTEGER NOT NULL, naam NVARCHAR(14), club INTEGER, CONSTRAINT PK_TEAM PRIMARY KEY (TEAMID))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE teamrang (rang INTEGER NOT NULL, klasse NVARCHAR(1) NOT NULL, spel INTEGER NOT NULL, team INTEGER NOT NULL, CONSTRAINT PK_TEAMRANG PRIMARY KEY (RANG, KLASSE, SPEL))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE team_spel (team INTEGER NOT NULL, spel INTEGER NOT NULL, CONSTRAINT PK_TEAM_SPEL PRIMARY KEY (TEAM, SPEL))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE worp (worp INTEGER NOT NULL, beurt INTEGER NOT NULL, score INTEGER, CONSTRAINT PK_WORP_BEURT PRIMARY KEY (WORP, BEURT))";
                    rowsaffected += command.ExecuteNonQuery();
                    command.CommandText = "CREATE TABLE spelkoppelkanmet (koppelId INTEGER NOT NULL, spel INTEGER, spelspeler INTEGER, spelbal INTEGER, s_aantalspelers INTEGER, b_aantalspelers INTEGER, aantalvoorkeuren INTEGER, wilkanmet INTEGER, wilkanmetbal INTEGER, voorkeur INTEGER, stap1 BIT, stap2 BIT, stap3 BIT, CONSTRAINT PK_SPELKOPPELKANMET PRIMARY KEY (KOPPELID))";
                    rowsaffected += command.ExecuteNonQuery();

                    command.CommandText = "ALTER TABLE speler_speler ADD CONSTRAINT FK1_SPELER_SPELER FOREIGN KEY (SPELER) REFERENCES SPELER (SPELERID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler_speler ADD CONSTRAINT FK2_SPELER_SPELER FOREIGN KEY (SPEELTMET) REFERENCES SPELER (SPELERID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE club ADD CONSTRAINT FK3_CLUB FOREIGN KEY (CENTRUM) REFERENCES CENTRUM (CENTRUMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE bal ADD CONSTRAINT FK4_BAL FOREIGN KEY (EIGENAAR) REFERENCES EIGENAAR (EIGENAARID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE eigenaar ADD CONSTRAINT FK5_EIGENAAR FOREIGN KEY (SPELER) REFERENCES SPELER (SPELERID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE eigenaar ADD CONSTRAINT FK6_EIGENAAR FOREIGN KEY (CLUB) REFERENCES CLUB (CLUBID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel_club ADD CONSTRAINT FK7_SPEL_CLUB FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel_club ADD CONSTRAINT FK8_SPEL_CLUB FOREIGN KEY (CLUB) REFERENCES CLUB (CLUBID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE beurt ADD CONSTRAINT FK9_BEURT FOREIGN KEY (SPELER) REFERENCES SPELER (SPELERID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE beurt ADD CONSTRAINT FK10_BEURT FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE beurt ADD CONSTRAINT FK11_BEURT FOREIGN KEY (CENTRUM) REFERENCES CENTRUM (CENTRUMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE beurt ADD CONSTRAINT FK12_BEURT FOREIGN KEY (BAL) REFERENCES BAL (BALID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE sessie_centrum ADD CONSTRAINT FK13_SESSIE_CENTRUM FOREIGN KEY (SESSIE) REFERENCES SESSIE (SESSIEID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE sessie_centrum ADD CONSTRAINT FK14_SESSIE_CENTRUM FOREIGN KEY (CENTRUM) REFERENCES CENTRUM (CENTRUMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel ADD CONSTRAINT FK15_SPEL FOREIGN KEY (SPELVARIANT) REFERENCES SPELVARIANT (SPELVARIANTID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel ADD CONSTRAINT FK16_SPEL FOREIGN KEY (SESSIE) REFERENCES SESSIE (SESSIEID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler ADD CONSTRAINT FK17_SPELER FOREIGN KEY (CLUB) REFERENCES CLUB (CLUBID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler ADD CONSTRAINT FK18_SPELER FOREIGN KEY (BAL) REFERENCES BAL (BALID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler ADD CONSTRAINT FK19_SPELER FOREIGN KEY (TEAM) REFERENCES TEAM (TEAMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spelerrang ADD CONSTRAINT FK20_SPELERRANG FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spelerrang ADD CONSTRAINT FK21_SPELERRANG FOREIGN KEY (SPELER) REFERENCES SPELER (SPELERID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler_spel_bal ADD CONSTRAINT FK22_SPELER_SPEL_BAL FOREIGN KEY (SPELER) REFERENCES SPELER (SPELERID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler_spel_bal ADD CONSTRAINT FK23_SPELER_SPEL_BAL FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE speler_spel_bal ADD CONSTRAINT FK24_SPELER_SPEL_BAL FOREIGN KEY (BAL) REFERENCES BAL (BALID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE baan_centrum ADD CONSTRAINT FK25_BAAN_CENTRUM FOREIGN KEY (CENTRUM) REFERENCES CENTRUM (CENTRUMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel_baan ADD CONSTRAINT FK26_SPEL_BAAN FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel_baan ADD CONSTRAINT FK27_SPEL_BAAN FOREIGN KEY (BAAN) REFERENCES BAAN_CENTRUM (BAANID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel_centrum ADD CONSTRAINT FK28_SPEL_CENTRUM FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE spel_centrum ADD CONSTRAINT FK29_SPEL_CENTRUM FOREIGN KEY (CENTRUM) REFERENCES CENTRUM (CENTRUMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE team ADD CONSTRAINT FK30_TEAM FOREIGN KEY (CLUB) REFERENCES CLUB (CLUBID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE teamrang ADD CONSTRAINT FK31_TEAMRANG FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE teamrang ADD CONSTRAINT FK32_TEAMRANG FOREIGN KEY (TEAM) REFERENCES TEAM (TEAMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE team_spel ADD CONSTRAINT FK33_TEAM_SPEL FOREIGN KEY (TEAM) REFERENCES TEAM (TEAMID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE team_spel ADD CONSTRAINT FK34_TEAM_SPEL FOREIGN KEY (SPEL) REFERENCES SPEL (SPELID)";
                    FK_created += command.ExecuteNonQuery();
                    command.CommandText = "ALTER TABLE worp ADD CONSTRAINT FK35_WORP FOREIGN KEY (BEURT) REFERENCES BEURT (BEURTID)";

                    sqlTran.Commit();
                    messageList.Add(rowsaffected + " tabellen zijn toegevoegd aan de database\r\n");
                    messageList.Add(FK_created + " foreign keys zijn toegevoegd aan de database\r\n");
                }
                catch (Exception ex)
                {
                    messageList.Add(ex.Message + "\r\nToevoegen van tabellen is mislukt\r\n");
                    messageList.Add(rowsaffected + " tabellen waren toegevoegd aan de database\r\n");
                    messageList.Add(FK_created + " foreign keys waren toegevoegd aan de database\r\n");
                    messageList.Add("Toevoegen is ongedaan gemaakt\r\n");
                    sqlTran.Rollback();
                }
                return;
            }
        }

Open in new window


The tables consist of varying number of fields and types.

Since all tables contain values that need to be filled this will result in a number of forms that is more or less equal to the number of tables...

So the solution you suggested in your last post would not work I think?

Commented:
I'm sorry then. I'm all out of ideas.

I would recommend you do one more thing though. Implement it 25 times (once for each table) and then refactor all common code.

Sorry I couldn't be of more help here.

Author

Commented:
OK thanks anyway, maybe there are others with good ideas. If I find a way in the meantime I'll report back.
Here's a way for the getData() method:
 
private void GetData()
        {
            string[] field01Text = objects[itemTeller].Panel1fields;
            foreach (Control ctrl1 in this.PanelTop.Controls)
            {
                if (field01Text.Length > 0 && ctrl1.Name == field01Text[0])
                { objects[itemTeller].F0 = ctrl1.Text; }
            }

            string[] field02Text = objects[itemTeller].Panel2fields;
            foreach (Control ctrl2 in this.PanelMidden.Controls)
            {
                if (field02Text.Length > 0 && ctrl2.Name == field02Text[0])
                { objects[itemTeller].F1 = ctrl2.Text; }
                if (field02Text.Length > 1 && ctrl2.Name == field02Text[1])
                { objects[itemTeller].F2 = ctrl2.Text; }
                if (field02Text.Length > 2 && ctrl2.Name == field02Text[2])
                { objects[itemTeller].F3 = ctrl2.Text; }
                if (field02Text.Length > 3 && ctrl2.Name == field02Text[3])
                { objects[itemTeller].F4 = ctrl2.Text; }
                if (field02Text.Length > 4 && ctrl2.Name == field02Text[4])
                { objects[itemTeller].F5 = ctrl2.Text; }
                if (field02Text.Length > 5 && ctrl2.Name == field02Text[5])
                { objects[itemTeller].F6 = ctrl2.Text; }
                if (field02Text.Length > 6 && ctrl2.Name == field02Text[6])
                { objects[itemTeller].F7 = ctrl2.Text; }
                if (field02Text.Length > 7 && ctrl2.Name == field02Text[7])
                { objects[itemTeller].F8 = ctrl2.Text; }
                if (field02Text.Length > 8 && ctrl2.Name == field02Text[8])
                { objects[itemTeller].F9 = ctrl2.Text; }
                if (field02Text.Length > 9 && ctrl2.Name == field02Text[9])
                { objects[itemTeller].F10 = ctrl2.Text; }
            }
        }

Open in new window


with properties F0 ... F10 added to the Centrum_oud Class, setting the values for the appropriate fields and doing the type conversion (I'm not actually using the indexer now by the way...)
 
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace KegelTelApp04
{
    class Centrum_oud
    {
        private int banen;
        private string naam;
        private string plaats;
        private int centrumId;
        Centrum_oud[] items;
        string[] panel1fields = { "centrumIdTextBox" };
        string[] panel2fields = { "centrumNaamTextBox", "centrumPlaatsTextBox", "centrumBanenTextBox" };


        // constructors
        public Centrum_oud() 
        {

        }

        // indexers
        public Centrum_oud this[int index]
        {
            get { return items[index]; }
            set { items[index] = value; OnChanged(); }
        }

        protected virtual void OnChanged()
        {
            if (Changed != null) Changed(this, EventArgs.Empty);
        }

        public int Banen
        {
            get { return banen; }
            set { banen = value; }
        }

        public string Naam
        {
            get { return naam; }
            set { naam = value; }
        }

        public string Plaats
        {
            get { return plaats; }
            set { plaats = value; }
        }

        public int CentrumId
        { 
            get { return centrumId; }
            set { centrumId = value; }
        }

        public string F0
        {
            get { return Convert.ToString(centrumId); }
            set { centrumId = int.Parse(value); }
        }

        public string F1
        {
            get { return naam; }
            set { naam = value; }
        }

        public string F2
        {
            get { return plaats; }
            set { plaats = value; }
        }

        public string F3
        {
            get { return Convert.ToString(banen); }
            set { banen = int.Parse(value); }
        }

        public string F4 { set { } }
        public string F5 { set { } }
        public string F6 { set { } }
        public string F7 { set { } }
        public string F8 { set { } }
        public string F9 { set { } }
        public string F10 { set { } }

        public string[] Panel1fields
        {
            get { return panel1fields; }
        }

        public string[] Panel2fields
        {
            get { return panel2fields; }
        }
        

        public void addCentrum()
        {
            throw new System.NotImplementedException();
        }

        public void deleteCentrum()
        {
            throw new System.NotImplementedException();
        }

        public void changeCentrum()
        {
            throw new System.NotImplementedException();
        }

        // event
        public event EventHandler Changed;

    }

}

Open in new window


It works and I'll settle for this now although it is not really the solution I was looking for (the more elegant multidimensional List solution I was looking for). I may open another forum thread for that later and close this one.

Thanks.

Author

Commented:
(partially answered by myself)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial