Code Snippets


Database_CommandOLEParameterAdd

public static void Database_CommandOLEParameterAdd(
System.Data.OleDb.OleDbCommand objCommand,
enOleDbType sType,
string sParameterName,
string sSourceColumn = "",
int iSize = -1)
{
try
{
if (gbEND_GENERAL == true)
return;

var objParameter = new System.Data.OleDb.OleDbParameter();

objParameter.ParameterName = sParameterName;

switch (sType)
{
case enOleDbType.Bool:
objParameter.OleDbType = System.Data.OleDb.OleDbType.Boolean;
break;

case enOleDbType.Text:
objParameter.OleDbType = System.Data.OleDb.OleDbType.Char;
break;

case enOleDbType.Memo:
objParameter.OleDbType = System.Data.OleDb.OleDbType.LongVarWChar;
break;
}

if (sSourceColumn.Length > 0)
objParameter.SourceColumn = sSourceColumn;

if (iSize > -1)
objParameter.Size = iSize;

objCommand.Parameters.Add(objParameter);
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Database_CommandOLEParameterAdd",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

Database_ConnectionClose

public static void Database_ConnectionClose(
string sDatabaseName)
{
try
{
if (gbEND_GENERAL == true)
return;

// VB logic:
// If (gDataConnection.State And ConnectionState.Open) <> ConnectionState.Open Then
// gDataConnection.Close()
// End If
//
// This condition is inverted compared to typical usage,
// but we preserve it EXACTLY as written.

if ( (gDataConnection.State & ConnectionState.Open) != ConnectionState.Open )
{
gDataConnection.Close();
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Database_ConnectionClose",
msCLASSNAME,
"close the connection to the Database '" + sDatabaseName + "'.",
mobjCOMException,
mobjException);
}
}
}

Database_ConnectionOpen

public static object Database_ConnectionOpen(
string sConnectionString,
string sDatabaseName,
bool bInformUser = true)
{
object returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null; // VB Exit Function ? returns Nothing

gDataConnection = new System.Data.OleDb.OleDbConnection();
gDataConnection.ConnectionString = sConnectionString;
gDataConnection.Open();

returnValue = true;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
// VB assigns the return value *inside the Finally block*
returnValue = false;

if (bInformUser == true)
{
clsError.Handle(
"Database_ConnectionOpen",
msCLASSNAME,
"connect to the Database '" + sDatabaseName + "'.",
mobjCOMException,
mobjException);
}
}
}

return returnValue;
}

Database_SQLRunCodeConn

public static string Database_SQLRunCodeConn(
string sSQLQuery,
bool bReturnRecordSet = false,
bool bReturnValue = false)
{
string returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null; // VB Exit Function ? returns Nothing

System.Data.OleDb.OleDbCommand objdatacommand;

objdatacommand = new System.Data.OleDb.OleDbCommand();
objdatacommand.Connection = gDataConnection;
objdatacommand.CommandText = sSQLQuery;
objdatacommand.CommandType = CommandType.Text;

gDataReader = objdatacommand.ExecuteReader();

if (bReturnRecordSet == true || bReturnValue == true)
{
if (bReturnValue == true)
{
if (gDataReader.RecordsAffected > 0)
{
returnValue = "Empty";
}
else
{
returnValue = gDataReader.FieldCount.ToString();
}

gDataReader.Close();
}
else
{
if (gDataReader.RecordsAffected == 0)
returnValue = "Empty";
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
returnValue = "Error";

clsError.Handle(
"Database_SQLRunCodeConn",
msCLASSNAME,
"execute the following SQL code and obtain the recordset" +
gsCRLF + gsCRLF + sSQLQuery,
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataRow_ToDataGridView

public static void DataRow_ToDataGridView(
string sTableName,
System.Data.DataRow objDataRow,
ref System.Windows.Forms.DataGridView ctlDataGridView,
string[] saTableColumns)
{
System.Data.DataTable objDataTable;
System.Windows.Forms.DataGridViewRow objdatagridviewrow;

string stablecolumn;
int itablecolumnno;
int idatatablecolumn;

try
{
if (gbEND_GENERAL == true)
return;

objDataTable = clsDatabase.mobjDataSet.Tables[sTableName];

ctlDataGridView.Rows.Add();
objdatagridviewrow = ctlDataGridView.Rows[ctlDataGridView.Rows.Count - 1];

for (itablecolumnno = 0; itablecolumnno <= saTableColumns.Length - 1; itablecolumnno++)
{
stablecolumn = saTableColumns[itablecolumnno];

for (idatatablecolumn = 0; idatatablecolumn <= objDataTable.Columns.Count - 1; idatatablecolumn++)
{
if (objDataTable.Columns[idatatablecolumn].ColumnName == stablecolumn)
{
if (objDataRow[idatatablecolumn] == System.DBNull.Value)
{
clszMessagesGeneral.Message(
"An entry in the '" + stablecolumn + "' column contains a Null value." +
gsCRLF +
"Values for the first 2 columns in this table are: " +
objDataRow[0].ToString() + gsCRLF +
objDataRow[1].ToString());

objdatagridviewrow.Cells[itablecolumnno].Value = "";
}
else
{
objdatagridviewrow.Cells[itablecolumnno].Value =
Convert.ToString(objDataRow[idatatablecolumn]);
}

break; // Exit For
}
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataRow_ToDataGridView",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataRow_ToListView

public static void DataRow_ToListView(
string sTableName,
System.Data.DataRow objDataRow,
ref System.Windows.Forms.ListView ctlListView,
string[] saTableColumns)
{
System.Data.DataTable objDataTable;
System.Windows.Forms.ListViewItem objlistviewitem = null;

string stablecolumn;
int itablecolumnno;
int idatatablecolumn;

objDataTable = clsDatabase.mobjDataSet.Tables[sTableName];

for (itablecolumnno = 0; itablecolumnno <= saTableColumns.Length - 1; itablecolumnno++)
{
stablecolumn = saTableColumns[itablecolumnno];

for (idatatablecolumn = 0; idatatablecolumn <= objDataTable.Columns.Count - 1; idatatablecolumn++)
{
if (objDataTable.Columns[idatatablecolumn].ColumnName == stablecolumn)
{
if (itablecolumnno == 0)
{
// First column becomes the ListViewItem itself
objlistviewitem = ctlListView.Items.Add(
Convert.ToString(objDataRow[idatatablecolumn]));
}
else
{
if (objDataRow[idatatablecolumn] != System.DBNull.Value)
{
objlistviewitem.SubItems.Add(
Convert.ToString(objDataRow[idatatablecolumn]));
}
else
{
objlistviewitem.SubItems.Add("");
}
}

break; // Exit For
}
}
}
}

DataRow_UpdateColumns

public static void DataRow_UpdateColumns(
System.Data.DataRow DataRow,
params string[] vColumnsValues)
{
try
{
if (gbEND_GENERAL == true)
return;

int icount;
string scolumnname;
string scolumnvalue;

DataRow.BeginEdit();

for (icount = 1; icount <= vColumnsValues.Length - 1; icount += 2)
{
scolumnname = vColumnsValues[2 * (icount - 1)];
scolumnvalue = vColumnsValues[2 * (icount - 1) + 1];

DataRow[scolumnname] = scolumnvalue;
}

DataRow.EndEdit();
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"UpdateColumns",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataRow_ValueToBoolean

public static bool DataRow_ValueToBoolean(
System.Data.DataRow objDataRow,
string sColumnName)
{
bool returnValue = false;

try
{
if (gbEND_GENERAL == true)
return false; // VB Exit Function ? returns default(Boolean) = False

if (objDataRow[sColumnName] != System.DBNull.Value)
{
returnValue = Convert.ToBoolean(objDataRow[sColumnName]);
}
else
{
returnValue = false;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ValueToBoolean",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataRow_ValueToString

public static string DataRow_ValueToString(
System.Data.DataRow objDataRow,
string sColumnName,
string sEmptyReturn = "")
{
string returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null; // VB Exit Function ? returns Nothing

if (objDataRow[sColumnName] != System.DBNull.Value)
{
returnValue = Convert.ToString(objDataRow[sColumnName]);
}
else
{
returnValue = "";
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ValueToString",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataSet_UpdateRow

public void DataSet_UpdateRow(
System.Data.DataRow DataRow,
params string[] vCols)
{
// Simulate VB's implicit return point for On Error GoTo
AnError:

try
{
if (gbEND_GENERAL == true)
return;

int icount;
string scolumnname;
string scolumnvalue;

DataRow.BeginEdit();

// VB: For icount = 1 To UBound(vCols) Step 2
for (icount = 1; icount <= vCols.Length - 1; icount += 2)
{
scolumnname = vCols[2 * (icount - 1)];
scolumnvalue = vCols[2 * (icount - 1) + 1];

DataRow[scolumnname] = scolumnvalue;
}

DataRow.EndEdit();

if (gbDEBUG == false)
return;
}
catch
{
// Jump to VB label
goto AnErrorHandler;
}

return;

AnErrorHandler:
Error_Handle("DataSet_UpdateRow", msMODULENAME, "");
}

DataTable_ColumnNamesToArray

public static string[] DataTable_ColumnNamesToArray(
string sTableName,
string sIgnoreColumnName = "")
{
string[] asTempArray = null;

try
{
int icolumnnumber = 0;
int iarraycount = 0;

System.Data.DataTable objDataTable;

objDataTable = clsDatabase.mobjDataSet.Tables[sTableName];

// VB: ReDim asTempArray(objDataTable.Columns.Count - 1)
asTempArray = new string[objDataTable.Columns.Count];

for (icolumnnumber = 0; icolumnnumber <= objDataTable.Columns.Count - 1; icolumnnumber++)
{
if (objDataTable.Columns[icolumnnumber].ColumnName != sIgnoreColumnName)
{
asTempArray[iarraycount] = objDataTable.Columns[icolumnnumber].ColumnName;
iarraycount++;
}
}

// VB: ReDim asTempArray(iarraycount - 1)
Array.Resize(ref asTempArray, iarraycount);

return asTempArray;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnNamesToArray",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return asTempArray;
}

DataTable_ColumnNamesToCombo

public static void DataTable_ColumnNamesToCombo(
string sTableName,
ref System.Windows.Forms.ComboBox ctlComboBox,
string sPrefix = "",
bool bClearList = true,
string sIgnoreColumnName = "")
{
try
{
if (gbEND_GENERAL == true)
return;

System.Data.DataTable objDataTable;
int icolumnnumber;

if (bClearList == true)
ctlComboBox.Items.Clear();

if (sPrefix.Length > 0)
sPrefix = sPrefix + ".";

objDataTable = clsDatabase.mobjDataSet.Tables[sTableName];

for (icolumnnumber = 0; icolumnnumber <= objDataTable.Columns.Count - 1; icolumnnumber++)
{
if (objDataTable.Columns[icolumnnumber].ColumnName != sIgnoreColumnName)
{
ctlComboBox.Items.Add(
sPrefix + objDataTable.Columns[icolumnnumber].ColumnName);
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnNamesToCombo",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataTable_ColumnsDefine

public static System.Data.DataTable DataTable_ColumnsDefine(
ref BET_DataGridView dlgBETDataGrid,
string sDataTableName,
string[,] asColumnsArray)
{
System.Data.DataTable returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null;

System.Data.DataTable objDataTable;
System.Data.DataColumn objDataColumn;

int inoofcolumns;
int icolumnno;
int icolumnsvisibleno = 0;

objDataTable = clsDatabase.mobjDataSet.Tables[sDataTableName];

inoofcolumns = asColumnsArray.GetUpperBound(0);

for (icolumnno = 0; icolumnno <= inoofcolumns; icolumnno++)
{
objDataColumn = new System.Data.DataColumn(asColumnsArray[icolumnno, 1]);

// Count visible columns
if (Convert.ToInt32(asColumnsArray[icolumnno, 3]) > 0)
{
icolumnsvisibleno = icolumnsvisibleno + 1;
}

if (asColumnsArray[icolumnno, 0] == "TextBox")
{
objDataColumn.DataType = System.Type.GetType("System.String");
objDataColumn.DefaultValue = string.Empty;
}

if (asColumnsArray[icolumnno, 0] == "CheckBox")
{
objDataColumn.DataType = System.Type.GetType("System.Boolean");
objDataColumn.DefaultValue = Boolean.FalseString;
}

objDataTable.Columns.Add(objDataColumn);
}

// VB had this but commented out:
// dlgBETDataGrid.zBETiColumnsVisibleTotal = icolumnsvisibleno

returnValue = objDataTable;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnsDefine",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_ColumnToArrayString

public static string[] DataTable_ColumnToArrayString(
System.Data.DataTable objDataTable,
string sColumnName)
{
string[] returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null;

int iarraycount;
string[] asTempArray;

// VB: ReDim asTempArray(objDataTable.Rows.Count - 1)
asTempArray = new string[objDataTable.Rows.Count];

iarraycount = 0;

foreach (System.Data.DataRow objDataRow in objDataTable.Rows)
{
if (objDataRow.RowState != System.Data.DataRowState.Deleted)
{
if (!objDataRow.IsNull(sColumnName))
{
asTempArray[iarraycount] =
Convert.ToString(objDataRow[sColumnName]);
}
else
{
asTempArray[iarraycount] = "";
}

iarraycount++;
}
else
{
// VB comment preserved:
// ' Call MsgBox("found a deleted row")
}
}

returnValue = asTempArray;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnToArrayString",
msCLASSNAME,
"transfer the datatable column to the array.",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_ColumnToArrayStringSorted

public static string[] DataTable_ColumnToArrayStringSorted(
string sTableName,
string sColumnName,
string sSQLSort = "")
{
string[] returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null;

int iarraycount;
System.Data.DataRow[] objDataRows;
string[] asTempArray;

objDataRows = clsDatabase.mobjDataSet.Tables[sTableName]
.Select(sColumnName, sSQLSort, System.Data.DataViewRowState.CurrentRows);

// VB: ReDim asTempArray(objDataRows.Length - 1)
asTempArray = new string[objDataRows.Length];

for (iarraycount = 0; iarraycount <= objDataRows.Length - 1; iarraycount++)
{
asTempArray[iarraycount] =
objDataRows[iarraycount][sColumnName].ToString();
}

returnValue = asTempArray;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnToArrayStringSorted",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_ColumnToArrayStringUnique

public static string[] DataTable_ColumnToArrayStringUnique(
System.Data.DataTable objDataTable,
string sColumnName,
string sSQLFilter = "",
string sSQLSort = "",
System.Data.DataViewRowState objRowState = System.Data.DataViewRowState.CurrentRows)
{
string[] returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null;

System.Data.DataRow[] objDataRows;
string[] asTempArray1;
string[] asTempArray2;

int iarraycount;
int iuniqueentries = 0;

objDataRows = objDataTable.Select(
sSQLFilter,
sSQLSort,
objRowState);

// VB: ReDim asTempArray1(objDataRows.Length - 1)
asTempArray1 = new string[objDataRows.Length];
asTempArray2 = new string[objDataRows.Length];

// Fill array 1
for (iarraycount = 0; iarraycount <= objDataRows.Length - 1; iarraycount++)
{
asTempArray1[iarraycount] =
objDataRows[iarraycount][sColumnName].ToString();
}

// VB: For iarraycount = (asTempArray1.Length - 1) To 0 Step -1
for (iarraycount = asTempArray1.Length - 1; iarraycount >= 0; iarraycount--)
{
// VB compares element i with element i-1
// This will throw for i = 0 in VB? No — VB allows negative index? No.
// But VB never executes the body for i = 0 because:
// If CStr(asTempArray1(iarraycount)) = CStr(asTempArray1(iarraycount - 1)) Then
// For i=0, this becomes asTempArray1(-1) ? VB throws.
// But your code *never* checks i=0 because the comparison fails before assignment.
// C# must preserve this behaviour by skipping i=0 manually.

if (iarraycount == 0)
continue;

if (Convert.ToString(asTempArray1[iarraycount]) ==
Convert.ToString(asTempArray1[iarraycount - 1]))
{
asTempArray2[iuniqueentries] = asTempArray1[iarraycount];
iuniqueentries++;
}
}

// VB: ReDim Preserve asTempArray2(iuniqueentries - 1)
Array.Resize(ref asTempArray2, iuniqueentries);

returnValue = asTempArray2;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnToArrayStringUnique",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_ColumnToComboUnique

public static void DataTable_ColumnToComboUnique(
System.Windows.Forms.ComboBox cboComboBox,
string sTableName,
string sColumnName,
string sSQLFilter = "",
string sSQLSort = "",
System.Data.DataViewRowState objRowState = System.Data.DataViewRowState.CurrentRows,
bool bClearList = true,
string sExtraItemsBefore = "",
bool bIncludeBlank = false,
string sExtraItemsAfter = "")
{
try
{
if (gbEND_GENERAL == true)
return;

System.Data.DataRow[] objDataRows;
string[] asTempArray1;
int iarraycount;
int iuniqueentries = 0;

if (bClearList == true)
cboComboBox.Items.Clear();

if (sExtraItemsBefore.Length > 0)
{
clsStr.ToComboBox(sExtraItemsBefore, cboComboBox, ";", null, null, bClearList);
}

if (bIncludeBlank == true)
cboComboBox.Items.Add("");

if (sSQLSort.Length == 0)
sSQLSort = sColumnName;

objDataRows = clsDatabase.mobjDataSet.Tables[sTableName]
.Select(sSQLFilter, sSQLSort, objRowState);

if (objDataRows.Length == 0)
{
System.Windows.Forms.MessageBox.Show("No records");
}

// VB: ReDim asTempArray1(objDataRows.Length - 1)
asTempArray1 = new string[objDataRows.Length];

for (iarraycount = 0; iarraycount <= objDataRows.Length - 1; iarraycount++)
{
asTempArray1[iarraycount] =
objDataRows[iarraycount][sColumnName].ToString();
}

// Add first item separately (VB logic)
if (asTempArray1.Length > 0 &&
Convert.ToString(asTempArray1[0]).Length > 0)
{
cboComboBox.Items.Add(asTempArray1[0]);
}

// Unique consecutive values
for (iarraycount = 1; iarraycount <= asTempArray1.Length - 1; iarraycount++)
{
if (Convert.ToString(asTempArray1[iarraycount]) !=
Convert.ToString(asTempArray1[iarraycount - 1]))
{
if (Convert.ToString(asTempArray1[iarraycount]).Length > 0)
{
cboComboBox.Items.Add(asTempArray1[iarraycount]);
}

iuniqueentries++;
}
}

// VB BUG PRESERVED:
// It should pass sExtraItemsAfter, but VB passes sExtraItemsBefore again.
if (sExtraItemsAfter.Length > 0)
{
clsStr.ToComboBox(sExtraItemsBefore, cboComboBox, ";", null, null, bClearList);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnToComboUnique",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataTable_ColumnValueToString

public static string DataTable_ColumnValueToString(
string sTableName,
string sSQLSelect,
string sColumnName)
{
string returnValue = null;

try
{
if (gbEND_GENERAL == true)
return null; // VB Exit Function ? returns Nothing

System.Data.DataRow objdatarow;

foreach (System.Data.DataRow objdatarowLoop in
clsDatabase.mobjDataSet.Tables[sTableName].Select(sSQLSelect))
{
objdatarow = objdatarowLoop;

if (objdatarow[sColumnName] != System.DBNull.Value)
{
returnValue = Convert.ToString(objdatarow[sColumnName]);
}
else
{
returnValue = "empty";
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ColumnValueToString",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_Populate

public static int DataTable_Populate(
string sTableName,
DataTable_SQL_Create SQL_CreateSelect)
{
int returnValue = 0;

try
{
if (gbEND_GENERAL == true)
return 0; // VB Exit Function ? returns default(Int32)

clsDatabase.gDataAdapter = new System.Data.OleDb.OleDbDataAdapter();

SQL_CreateSelect(); // VB: Call SQL_CreateSelect()

// VB:
// Populate = clsDatabase.gDataAdapter.Fill(clsDatabase.mobjDataSet, sTableName)
returnValue = clsDatabase.gDataAdapter.Fill(
clsDatabase.mobjDataSet,
sTableName);
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_Populate",
msCLASSNAME,
"populate the datatable '" + sTableName +
"' using the corresponding SELECT statement.",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_PrimaryKeyDefine

public static void DataTable_PrimaryKeyDefine(
string sTableName,
string sColumnName)
{
try
{
if (gbEND_GENERAL == true)
return;

System.Data.DataTable objDataTable;
System.Data.DataColumn[] aobjDataColumns;

objDataTable = clsDatabase.mobjDataSet.Tables[sTableName];

// VB: ReDim aobjDataColumns(0)
aobjDataColumns = new System.Data.DataColumn[1];

aobjDataColumns[0] = objDataTable.Columns[sColumnName];

objDataTable.PrimaryKey = aobjDataColumns;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_PrimaryKeyDefine",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataTable_RowFind

public static System.Data.DataRow DataTable_RowFind(
string sTableName,
string sSQLFilter,
string sSQLSort)
{
System.Data.DataRow returnValue = null;

System.Data.DataRow[] objDataRows;
System.Data.DataRow objDataRow;

try
{
if (gbEND_GENERAL == true)
return null;

objDataRows = clsDatabase.mobjDataSet.Tables[sTableName]
.Select(sSQLFilter, sSQLSort);

if (objDataRows.Length == 0)
{
clszMessagesGeneral.SQLNoRowsInformation(sSQLFilter, sTableName);
}

// VB always attempts to return objDataRows(0),
// even if Length = 0 (VB would throw). Behaviour preserved.
returnValue = objDataRows[0];
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_RowFind",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_RowsCount

public static int DataTable_RowsCount(
string sTableName,
string sSQLFilter,
bool bInformUser = false)
{
int returnValue = 0;
System.Data.DataRow[] objDataRows;

try
{
if (gbEND_GENERAL == true)
return 0; // VB Exit Function ? returns default(Int32)

// VB: objDataRows = RowsFilter(sTableName, sSQLFilter, , bInformUser)
// The missing argument becomes Type.Missing in C#
objDataRows = RowsFilter(sTableName, sSQLFilter, null, bInformUser);

returnValue = objDataRows.Length;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_RowsCount",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_RowsFilter

public static System.Data.DataRow[] DataTable_RowsFilter(
string sTableName,
string sSQLFilter,
string sSQLSort = "",
bool bInformUser = true)
{
System.Data.DataRow[] returnValue = null;
System.Data.DataRow[] objDataRows;

try
{
if (gbEND_GENERAL == true)
return null;

if (sSQLSort.Length == 0)
{
objDataRows = clsDatabase.mobjDataSet.Tables[sTableName]
.Select(sSQLFilter);
}
else
{
objDataRows = clsDatabase.mobjDataSet.Tables[sTableName]
.Select(sSQLFilter, sSQLSort);
}

if (bInformUser == true && objDataRows.Length == 0)
{
clszMessagesGeneral.SQLNoRowsInformation(sSQLFilter, sTableName);
}

returnValue = objDataRows;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_RowsFilter",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_RowsInsertFromArray

public static void DataTable_RowsInsertFromArray(
string sTableName,
string[,] saArrayName)
{
try
{
if (gbEND_GENERAL == true)
return;

System.Data.DataRow objDataRow;

int iarraycount;
int icolcount;

for (iarraycount = 0; iarraycount <= saArrayName.GetUpperBound(0); iarraycount++)
{
objDataRow = clsDatabase.mobjDataSet.Tables[sTableName].NewRow();

for (icolcount = 0; icolcount <= saArrayName.GetUpperBound(1); icolcount++)
{
objDataRow[icolcount] = saArrayName[iarraycount, icolcount];
}

clsDatabase.mobjDataSet.Tables[sTableName].Rows.Add(objDataRow);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_RowsInsertFromArray",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataTable_SaveChanges

public static int DataTable_SaveChanges(
string sTableName,
DataTable_SQL_Create SQL_CreateInsert,
DataTable_SQL_Create SQL_CreateUpdate,
DataTable_SQL_Create SQL_CreateDelete)
{
int returnValue = 0;

try
{
if (clsError.ErrorFlag() == true)
return 0; // VB Exit Function ? returns default(Int32)

clsDatabase.gDataAdapter = new System.Data.OleDb.OleDbDataAdapter();

SQL_CreateInsert();
SQL_CreateUpdate();
SQL_CreateDelete();

// VB: SaveChanges = gDataAdapter.Update(DataSet, sTableName)
returnValue = clsDatabase.gDataAdapter.Update(
clsDatabase.mobjDataSet,
sTableName);

clsDatabase.mobjDataSet.AcceptChanges();
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_SaveChanges",
msCLASSNAME,
"save all the changes to the datatable '" + sTableName + "'.",
mobjCOMException,
mobjException);
}
}

return returnValue;
}

DataTable_ToArray

public string[] DataTable_ToArray(
System.Data.DataTable objDataTable,
int iColumn)
{
string[] sdataarray = null;
int irowcount;

try
{
// VB: ReDim sdataarray(objDataTable.Rows.Count - 1)
sdataarray = new string[objDataTable.Rows.Count];

for (irowcount = 0; irowcount <= objDataTable.Rows.Count - 1; irowcount++)
{
sdataarray[irowcount] =
Convert.ToString(objDataTable.Rows[irowcount][iColumn]);
}

return sdataarray;
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);

return null;
}
}

DataTable_ToArrayMulti

public static string[,] DataTable_ToArrayMulti(
string sTableName)
{
string[,] sdataarray = null;
System.Data.DataTable objDataTable;
int irowcount;
int icolcount;

try
{
if (gbEND_GENERAL == true)
return null;

objDataTable = clsDataSet.mobjDataSet.Tables[sTableName];

// VB: ReDim sdataarray(rows-1, cols-1)
sdataarray = new string[
objDataTable.Rows.Count,
objDataTable.Columns.Count];

for (irowcount = 0; irowcount <= objDataTable.Rows.Count - 1; irowcount++)
{
for (icolcount = 0; icolcount <= objDataTable.Columns.Count - 1; icolcount++)
{
sdataarray[irowcount, icolcount] =
Convert.ToString(objDataTable.Rows[irowcount][icolcount]);
}
}

return sdataarray;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ToArrayMulti",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}

return null;
}

DataTable_ToDataGridView

public static void DataTable_ToDataGridView(
string sTableName,
System.Windows.Forms.DataGridView ctlDataGridView,
string[] saTableColumns)
{
try
{
if (gbEND_GENERAL == true)
return;

System.Data.DataRow objDataRow;

foreach (System.Data.DataRow objDataRowLoop in
clsDatabase.mobjDataSet.Tables[sTableName].Rows)
{
objDataRow = objDataRowLoop;

clsDataRow.ToDataGridView(
sTableName,
objDataRow,
ctlDataGridView,
saTableColumns);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL_ERRMSG == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ToDataGridView",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataTable_ToListView

public static void DataTable_ToListView(
string sTableName,
System.Windows.Forms.ListView ctlListView,
string[] saTableColumns)
{
try
{
if (gbEND_GENERAL == true)
return;

System.Data.DataRow objDataRow;

ctlListView.BeginUpdate();

foreach (System.Data.DataRow objDataRowLoop in
clsDatabase.mobjDataSet.Tables[sTableName].Rows)
{
objDataRow = objDataRowLoop;

clsDataRow.ToListView(
sTableName,
objDataRow,
ctlListView,
saTableColumns);
}

ctlListView.EndUpdate();
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ToListView",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

DataTable_ToTreeView

public static void DataTable_ToTreeView(
string sTableName,
string sColumnName,
System.Windows.Forms.TreeView ctlTreeView,
int iNodeNumber)
{
System.Data.DataRow objdatarow;
System.Windows.Forms.TreeNode objtreenode;
int ipos = 0;

try
{
if (gbEND_GENERAL == true)
return;

objtreenode = ctlTreeView.Nodes[iNodeNumber];

ctlTreeView.BeginUpdate();

foreach (System.Data.DataRow objdatarowLoop in
clsDataSet.mobjDataSet.Tables[sTableName].Rows)
{
objdatarow = objdatarowLoop;

objtreenode.Nodes.Insert(
ipos,
new System.Windows.Forms.TreeNode(
Convert.ToString(objdatarow[sColumnName])));

ipos++;
}

ctlTreeView.EndUpdate();
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"DataTable_ToTreeView",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}

Message_DataSetIsNothing

public static void Message_DataSetIsNothing()
{
if (clsError.ErrorFlag() == true)
return;

System.Windows.Forms.MessageBox.Show(
"The object 'clsDatabase.mobjDataSet' is nothing and has not been initialised.",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);

// sets the flag to an error
clsError.ErrorFlagChange(true);
}

Message_SQLNoRows

public static void Message_SQLNoRows(
string sSqlFilter,
string sTableName,
bool bFlagError = false)
{
// VB comment preserved:
// If clsError.ErrorFlag() = True Then Exit Sub

System.Windows.Forms.MessageBox.Show(
"The following SQL does not return any rows:" +
System.Environment.NewLine +
"SELECT all the rows WHERE " +
System.Environment.NewLine +
sSqlFilter.Trim() +
System.Environment.NewLine +
"FROM " + sTableName,
"SQL - No Rows Returned",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);

// VB comment preserved:
// clsError.ErrorFlagChange(bFlagError)
}

Question_RowDelete

public static bool Question_RowDelete(
int iDefaultButton = 1)
{
bool breturn = false;
System.Windows.Forms.DialogResult objreturn;
System.Windows.Forms.MessageBoxDefaultButton objdefaultbutton;

switch (iDefaultButton)
{
case 1:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
case 2:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2;
break;
default:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
}

objreturn = System.Windows.Forms.MessageBox.Show(
"Are you sure you want to delete this row ?",
gsDIALOG_PREFIX_GENERAL,
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question,
objdefaultbutton);

if (objreturn == System.Windows.Forms.DialogResult.Yes)
breturn = true;
if (objreturn == System.Windows.Forms.DialogResult.No)
breturn = false;

return breturn;
}

Question_SaveTheChanges

public static bool Question_SaveTheChanges(
int iDefaultButton = 1)
{
bool breturn = false;
System.Windows.Forms.DialogResult objreturn;
System.Windows.Forms.MessageBoxDefaultButton objdefaultbutton;

switch (iDefaultButton)
{
case 1:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
case 2:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2;
break;
default:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
}

objreturn = System.Windows.Forms.MessageBox.Show(
"Are you sure you want to save these changes ?",
gsDIALOG_PREFIX_GENERAL,
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question,
objdefaultbutton);

if (objreturn == System.Windows.Forms.DialogResult.Yes)
breturn = true;
if (objreturn == System.Windows.Forms.DialogResult.No)
breturn = false;

return breturn;
}

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top