private static string GetFieldName(InvalidConstraintException e, DataTable erroredtable) { if (erroredtable == null || e == null) { return(""); } var t01 = "ForeignKeyConstraint "; var t02 = " requires the"; int pos1 = e.Message.IndexOf(t01); int pos2 = -1; if (pos1 >= 0) { pos1 += t01.Length; pos2 = e.Message.IndexOf(t02); } string s = ""; // constraints are enforced on relation FK_OPS_AC11_ACP21_AC, and deleting this row will var t11 = "constraints are enforced on relation "; var t12 = ", and deleting this row will"; if (pos1 >= 0 && pos2 >= 0) { s = e.Message.Substring(pos1, pos2 - pos1); } else { pos1 = e.Message.IndexOf(t11); if (pos1 >= 0) { pos1 += t11.Length; pos2 = e.Message.IndexOf(t12); s = e.Message.Substring(pos1, pos2 - pos1); } } if (!erroredtable.Constraints.Contains(s)) { return(""); } ForeignKeyConstraint fk = erroredtable.Constraints[s] as ForeignKeyConstraint; return(fk.Columns[0].ColumnName); }
public static MyException TranslateException(Exception e, DataGridView dgv) { string s, msg = ""; if (e == null) { throw new ArgumentException(); } if (dgv == null) { return(new MyException(e.Message, e)); } DataTable dt = GetDataTableFromDataGridView(dgv); if (e is SqlException && e.Message.StartsWith(_sqlExceoptionStr1)) { //The UPDATE statement conflicted with the REFERENCE constraint \"fk_OPS_AC11_Acp21_AC\". The conflict occurred in database \"C:\\A1-DOCS\\C_NET\\KLONS1\\DB\\KLONS.MDF\", table \"dbo.OPS\", column 'AC11' SqlException esq = e as SqlException; //s = GetFieldName(esq, table); //s = table.Columns[s].Caption; msg = string.Format("Lauka vērtību nevar mainīt, " + "ja tā ir izmantota sistītā tabulā"); } else if (e is FbException && e.Message.StartsWith("violation of FOREIGN KEY constraint ")) { //violation of FOREIGN KEY constraint "FK_OPSD_CLID_PERSONS_CLID" on table "OPSD" Foreign key references are present for the record Problematic key value is ("CLID" = 'zapte') msg = string.Format("Darbību ar ierakstu nevar izpildīt, " + "ja tas ir izmantots sistītā tabulā"); } else if (e is DBConcurrencyException) { msg = "Izmaiņas neizdevās saglabāt,\n" + "jo ierakstu ir labojis cits lietotājs.\n" + "Mēģiniet pārlasīt tabulas datus."; } else if (e is NoNullAllowedException) { NoNullAllowedException enn = e as NoNullAllowedException; s = GetFieldName(enn); s = GetColumnDescr(dgv, s); msg = string.Format("Lauks [{0}] nevar būt tukšs", s); } else if (e is ConstraintException && e.Message.StartsWith("Column '") && e.Message.Contains("is constrained to be unique")) { //Column 'AC' is constrained to be unique. Value '1210' is already present. ConstraintException ec = e as ConstraintException; s = GetFieldName(ec); s = GetColumnDescr(dgv, s); msg = string.Format("Nekorekta lauka [{0}] vērtība" + " (šāda vērtība tabulā jau ir).", s); } else if (e is ConstraintException && e.Message.StartsWith("Failed to enable constraints. One or more rows contain")) { //Failed to enable constraints. One or more rows contain DetailedConstraintException de = new DetailedConstraintException(e.Message, GetDataTableFromDataGridView(dgv), e); msg = de.Message; } else if (e is InvalidConstraintException && e.Message.IndexOf("deleting") >= 0) { msg = "Ierakstu nevar dzēst, jo tam ir saistīti ieraksti citā tabulā."; } else if (e is InvalidConstraintException) { InvalidConstraintException eic = e as InvalidConstraintException; s = GetFieldName(eic, dt); s = GetColumnDescr(dgv, s); msg = string.Format("Nekorekta lauka [{0}] vērtība", s); } else if (e is ArgumentException && e.Message.Contains("The value violates the MaxLength limit")) { //ArgumentException: Cannot set column 'ClId'. The value violates the MaxLength limit of this column s = GetFieldName(e as ArgumentException); s = GetColumnDescr(dgv, s); msg = string.Format("Lauka [{0}] teksts ir par garu.", s); } else if (e is FormatException) { //Input string was not in a correct format. msg = string.Format("Ievadīta nekorekta lauka vērtība\n" + "(nekorekts ievadītā teksta formāts, ...)."); } if (msg == "") { msg = e.Message; } return(new MyException(msg, e)); }