ruud00000
asked on
How to do a search in and sort a multidimensional List of objects in C#?
(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.
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.
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):
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?
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();
}
}
}
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; }
}
}
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));
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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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?
Does this help?
So, how does your form look? do you have 25 sets of text boxes?
ASKER
I adapted your code to fit in, like this:
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...
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); }
}
}
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;
}
}
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?
i.e.
if (MyClass.ClassType == "centrum_oud") { // do something }
Does that make sense?
ASKER
Here are the table definitions:
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?
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;
}
}
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?
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.
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.
ASKER
OK thanks anyway, maybe there are others with good ideas. If I find a way in the meantime I'll report back.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
(partially answered by myself)
ASKER
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).