[C#] Wert der Tebelle ist DBNull

Nicoo

Grünschnabel
Guten Morgen zusammen,

ich komme zur Zeit an einem Fehler nicht vorbei: The value for column 'ProfessionID' in table 'Apprentice' is DBNull.

Endprodukt:
Eine Anwendung zum Berechnen des Leistungslohnes für meine Firma.
Die Personen sollen einzeln erfasst und in ein DataSet gespeichert werden können.
Erst wenn der Benutzer auf "Speichern" klickt, sollen die Daten in die Access-Datenbank geschrieben werden.
Beim Laden der Anwendung sollen automatisch die Daten aus der Access-DB in das DataSet geladen und auf dem Windows-Form angezeigt werden.
Zudem sollen die Daten im DataSet bearbeitet werden können.

Problem:
Nachdem ich die Daten für einen neuen Benutzer eingegeben habe, erscheint der oben genannte Fehler.

Die DB-Tabelle Apprentice:
- ID
- ApprenticeName
- ApprenticeForename
- ApprenticeshipStart
- ApprenticeshipEnd
- ProfessionID

So speichere ich die Daten in die Access-DB:
Code:
private void SaveData()
		{
			try
			{
				apprenticeTableAdapter.Connection.Open();
				var rows = _dataset.Apprentice;
				var rowCount = 0;

				foreach (var row in rows)
				{
					apprenticeTableAdapter.Insert(rows[rowCount].ApprenticeName, rows[rowCount].ApprenticeForename, rows[rowCount].ApprenticeshipStart, rows[rowCount].ApprenticeshipEnd, rows[rowCount].ProfessionID);

					rowCount++;
				}
				apprenticeTableAdapter.Connection.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

So fülle ich die Daten in ein ListView (später wird diese ersetzt durch ein spez. DataGridView..):
Code:
private void FillListView()
		{
			var rowCount = 0;
			var rows = _dataset.Apprentice;

			_lsvPersonMng.Items.Clear();

			foreach (var row in rows)
			{
				var listViewItem = new ListViewItem();

				listViewItem.Text = rows[rowCount].ApprenticeName;
				listViewItem.SubItems.Add(rows[rowCount].ApprenticeForename);
				listViewItem.SubItems.Add(rows[rowCount].ApprenticeshipStart.ToString());
				listViewItem.SubItems.Add(rows[rowCount].ApprenticeshipEnd.ToString());
				listViewItem.SubItems.Add(Professions.ToList()[rows[rowCount].ProfessionID].Name);

				_lsvPersonMng.Items.Add(listViewItem);

				rowCount++;
			}
		}

Wenn ich diese Zeile auskommentier, funktioniert es: listViewItem.SubItems.Add(Professions.ToList()[rows[rowCount].ProfessionID].Name);

Hier noch mein Model Professions:
Code:
public class Professions
	{
		#region Fields

		public static Profession AnlagenUndApparateBauer = new Profession("aab", "Anl. & Apparatebauer/-in", 1);
		public static Profession Automatiker = new Profession("aum", "Automatiker/-in", 2);
		public static Profession AutomatikMonteur = new Profession("aumm", "Automatikmonteur/-in", 3);
		public static Profession Betriebsunterhalt = new Profession("buh", "Betriebsunterhalt", 4);
		public static Profession Elektroniker = new Profession("elk", "Elektroniker/-in", 5);
		public static Profession Informatiker = new Profession("inf", "Informatiker/-in", 6);
		public static Profession Kaufmann = new Profession("kv", "Kaufmann/-frau", 7);
		public static Profession Konstrukteur = new Profession("kon", "Konstrukteur/-in", 8);
		public static Profession Logistiker = new Profession("log", "Logistiker-/in", 9);
		public static Profession Polymechaniker = new Profession("poly", "Polymechaniker/-in", 10);
		public static Profession ProdMechaniker = new Profession("prodm", "Prod.Mechaniker-/in", 11);

		#endregion

		#region Public Methods

		public static List<Profession> ToList()
		{
			var list = new List<Profession>();

			list.Add(AnlagenUndApparateBauer);
			list.Add(Automatiker);
			list.Add(AutomatikMonteur);
			list.Add(Betriebsunterhalt);
			list.Add(Elektroniker);
			list.Add(Informatiker);
			list.Add(Kaufmann);
			list.Add(Konstrukteur);
			list.Add(Logistiker);
			list.Add(Polymechaniker);
			list.Add(ProdMechaniker);

			return list;
		}

		#endregion
	}

So wird ein neuer Benutzer hinzugefügt(Lehrberuf wird über eine ComboBox ausgewählt):
Code:
var lname = _txbName.Text;
				var fname = _txbForename.Text;
				var apsStart = _dtpApprenticeshipStart.Value;
				var apsEnd = _dtpApprenticeshipEnd.Value;
				var prof = (Profession)_cbbProfession.SelectedItem;

				if (lname != string.Empty && fname != string.Empty &&
					apsStart != null && apsEnd != null && prof != null)
				{
					Person = new Apprentice(lname, fname, apsStart, apsEnd, prof);
				}

Und das Model Apprentice:
Code:
#region Fields

		public string LastName { get; private set; }
		public string ForeName { get; private set; }
		public DateTime ApprenticeshipStart { get; private set; }
		public DateTime ApprenticeshipEnd { get; private set; }
		public Profession AppreniceshipProfession { get; private set; }

		#endregion

		#region Constructors

		public Apprentice(string lname, string fname, DateTime apsStart, DateTime apsEnd, Profession apsProf)
		{
			LastName = lname;
			ForeName = fname;
			ApprenticeshipStart = apsStart;
			ApprenticeshipEnd = apsEnd;
			AppreniceshipProfession = apsProf;
		}

		#endregion


Kann mir jmd helfen? (Bitte melden, wenn ihr noch mehr Codeteile oder Infos braucht..)



Gruss
Nico


-----------------------------------------------
Edit:
Der Fehler tritt bei dieser Zeile auf:

Code:
listViewItem.SubItems.Add(Professions.ToList()[rows[rowCount].ProfessionID].Name);

Im VS wird der Fehler dann im Designer angezeigt (im catch):
Code:
public int ProfessionID {
                get {
                    try {
                        return ((int)(this[this.tableApprentice.ProfessionIDColumn]));
                    }
                    catch (global::System.InvalidCastException e) {
                        throw new global::System.Data.StrongTypingException("The value for column \'ProfessionID\' in table \'Apprentice\' is DBNull.", e);
                    }
                }
                set {
                    this[this.tableApprentice.ProfessionIDColumn] = value;
                }
            }
 
Zuletzt bearbeitet:
Zurück