Code Snippet Details

<- Back to List


Language
C#.Net
Description
DBUtility
Code
using System; using System.Data; namespace Alphabet.Framework.DAL { public class DBUtility { /// <summary> /// Check a value in a datarow is populated. /// </summary> /// <param name="dataRow">The datarow containing the value to check.</param> /// <param name="columnName">The name of the item to check.</param> /// <param name="errorMessage">Referenced error message used to raise an error with details of what fields is not populated.</param> /// <remarks></remarks> public static void CheckMandatoryValue(DataRow dataRow, string columnName, ref string errorMessage) { try { if (dataRow[columnName].ToString() == string.Empty) { errorMessage += "Column " + columnName + " must be populated."; } } catch (Exception ex) { throw; } } /// <summary> /// Converts null values to string. /// </summary> /// <param name="rowValue">An individual data row value.</param> /// <returns>A string.</returns> public static string DBNullToString(object rowValue) { if (System.Convert.IsDBNull(rowValue)) { return string.Empty; } else { return rowValue.ToString(); } } /// <summary> /// Check that a data column has ther expected name and type. /// </summary> /// <remarks>Throws an ArgumentException if either the column name, type or both are invalid.</remarks> public static void CheckColumn(System.Data.DataColumn columnToCheck, string expectedName, System.Type expectedType) { string exceptionMessage = string.Empty; if (columnToCheck.ColumnName != expectedName) { exceptionMessage += "Invalid column name in column " + columnToCheck.Ordinal + ", expected='" + expectedName + "' actual='" + columnToCheck.ColumnName + "'. "; } if (columnToCheck.DataType.Name != expectedType.Name) { exceptionMessage += "Invalid column type in column " + columnToCheck.Ordinal + ", expected='" + expectedType.Name + "' actual='" + columnToCheck.DataType.Name + "'. "; } if (exceptionMessage.Length > 0) { throw new ArgumentException("Invalid data column - " + exceptionMessage); } } } }