Easy way to convert data table to hash table or sqldatareader to hashtable Easy way to convert data table to hash table or sqldatareader to hashtable sql sql

Easy way to convert data table to hash table or sqldatareader to hashtable


You can use the following function to convert DataTable to HashTable,

public static Hashtable convertDataTableToHashTable(DataTable dtIn,string keyField,string valueField)   {       Hashtable htOut = new Hashtable();       foreach(DataRow drIn in dtIn.Rows)       {          htOut.Add(drIn[keyField].ToString(),drIn[valueField].ToString());       }      return htOut;    }

Then in your code just use,

Hashtable sendData = new Hashtable();//You need to pass datatable, key field and value fieldsendData = convertDataTableToHashTable(dt, "orderNumber", "customerName");


public static Hashtable Fn_ConvertDataTableToHashTable(DataTable dtTable, int iRow){        Hashtable hshTable = new Hashtable();        if (CommonUtil.Fn_CheckDatatableHasValue(dtTable))        {            foreach (DataColumn column in dtTable.Columns)            {                hshTable.Add(column.ColumnName, dtTable.Rows[iRow][column.ColumnName].ToString());            }        }        return hshTable;}