ColumnNameByDataType() public method

Returns the name of the first column with a given column type (valuable for Track Columns)
public ColumnNameByDataType ( DataSet ds, string dataType ) : string
ds System.Data.DataSet DataSet with column definitions
dataType string type to be searched
return string
        /// -----------------------------------------------------------------------------
        /// <summary>
        ///   Implements the search interface for DotNetNuke
        /// </summary>
        /// -----------------------------------------------------------------------------
        public SearchItemInfoCollection GetSearchItems(ModuleInfo modInfo)
        {
            var searchItemCollection = new SearchItemInfoCollection();
            var udtController        = new UserDefinedTableController(modInfo);

            try
            {
                var dsUserDefinedRows = udtController.GetDataSet(withPreRenderedValues: false);

                //Get names of ChangedBy and ChangedAt columns
                var colnameChangedBy = udtController.ColumnNameByDataType(dsUserDefinedRows,
                                                                          DataTypeNames.UDT_DataType_ChangedBy);
                var colnameChangedAt = udtController.ColumnNameByDataType(dsUserDefinedRows,
                                                                          DataTypeNames.UDT_DataType_ChangedAt);

                var moduleController = new ModuleController();
                var settings         = moduleController.GetModuleSettings(modInfo.ModuleID);
                var includeInSearch  = !(settings[SettingName.ExcludeFromSearch].AsBoolean());

                if (includeInSearch)
                {
                    foreach (DataRow row in dsUserDefinedRows.Tables[DataSetTableName.Data].Rows)
                    {
                        var changedDate     = DateTime.Today;
                        var changedByUserId = 0;

                        if (colnameChangedAt != string.Empty && !Information.IsDBNull(row[colnameChangedAt]))
                        {
                            changedDate = Convert.ToDateTime(row[colnameChangedAt]);
                        }
                        if (colnameChangedBy != string.Empty && !Information.IsDBNull(row[colnameChangedBy]))
                        {
                            changedByUserId = ModuleSecurity.UserId(row[colnameChangedBy].ToString(), modInfo.PortalID);
                        }

                        var desc = string.Empty;
                        foreach (DataRow col in dsUserDefinedRows.Tables[DataSetTableName.Fields].Rows)
                        {
                            var fieldType  = col[FieldsTableColumn.Type].ToString();
                            var fieldTitle = col[FieldsTableColumn.Title].ToString();
                            var visible    = Convert.ToBoolean(col[FieldsTableColumn.Visible]);
                            if (visible &&
                                (fieldType.StartsWith("Text") || fieldType == DataTypeNames.UDT_DataType_String))
                            {
                                desc += string.Format("{0} &bull; ", Convert.ToString(row[fieldTitle]));
                            }
                        }
                        if (desc.EndsWith("<br/>"))
                        {
                            desc = desc.Substring(0, Convert.ToInt32(desc.Length - 5));
                        }
                        var searchItem = new SearchItemInfo(modInfo.ModuleTitle, desc, changedByUserId, changedDate,
                                                            modInfo.ModuleID, row[DataTableColumn.RowId].ToString(),
                                                            desc);
                        searchItemCollection.Add(searchItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
            }

            return(searchItemCollection);
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        ///   Implements the search interface for DotNetNuke
        /// </summary>
        /// -----------------------------------------------------------------------------
        public SearchItemInfoCollection GetSearchItems(ModuleInfo modInfo)
        {
            var searchItemCollection = new SearchItemInfoCollection();
            var udtController = new UserDefinedTableController(modInfo);

            try
            {
                var dsUserDefinedRows = udtController.GetDataSet(withPreRenderedValues: false);

                //Get names of ChangedBy and ChangedAt columns
                var colnameChangedBy = udtController.ColumnNameByDataType(dsUserDefinedRows,
                                                                          DataTypeNames.UDT_DataType_ChangedBy);
                var colnameChangedAt = udtController.ColumnNameByDataType(dsUserDefinedRows,
                                                                          DataTypeNames.UDT_DataType_ChangedAt);

                var moduleController = new ModuleController();
                var settings = moduleController.GetModuleSettings(modInfo.ModuleID);
                var includeInSearch = !(settings[SettingName.ExcludeFromSearch].AsBoolean());

                if (includeInSearch)
                {
                    foreach (DataRow row in dsUserDefinedRows.Tables[DataSetTableName.Data].Rows)
                    {
                        var changedDate = DateTime.Today;
                        var changedByUserId = 0;

                        if (colnameChangedAt != string.Empty && ! Information.IsDBNull(row[colnameChangedAt]))
                        {
                            changedDate = Convert.ToDateTime(row[colnameChangedAt]);
                        }
                        if (colnameChangedBy != string.Empty && ! Information.IsDBNull(row[colnameChangedBy]))
                        {
                            changedByUserId = ModuleSecurity.UserId(row[colnameChangedBy].ToString(), modInfo.PortalID);
                        }

                        var desc = string.Empty;
                        foreach (DataRow col in dsUserDefinedRows.Tables[DataSetTableName.Fields].Rows)
                        {
                            var fieldType = col[FieldsTableColumn.Type].ToString();
                            var fieldTitle = col[FieldsTableColumn.Title].ToString();
                            var visible = Convert.ToBoolean(col[FieldsTableColumn.Visible]);
                            if (visible &&
                                (fieldType.StartsWith("Text") || fieldType == DataTypeNames.UDT_DataType_String))
                            {
                                desc += string.Format("{0} &bull; ", Convert.ToString(row[fieldTitle]));
                            }
                        }
                        if (desc.EndsWith("<br/>"))
                        {
                            desc = desc.Substring(0, Convert.ToInt32(desc.Length - 5));
                        }
                        var searchItem = new SearchItemInfo(modInfo.ModuleTitle, desc, changedByUserId, changedDate,
                                                            modInfo.ModuleID, row[DataTableColumn.RowId].ToString(),
                                                            desc);
                        searchItemCollection.Add(searchItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
            }

            return searchItemCollection;
        }
        string ImportModule(int moduleId, string fileName, string folder, string delimiter)
        {
            var strMessage = "";

            var mc         = new ModuleController();
            var moduleInfo = mc.GetModule(moduleId, TabId, false);

            if (Path.GetExtension(fileName).ToUpper() == ".CSV" && moduleInfo != null)
            {
                var udtController = new UserDefinedTableController(ModuleContext);

                var file = FileManager.Instance.GetFile(PortalId, Path.Combine(folder, fileName));
                using (var stream = FileManager.Instance.GetFileContent(file))
                {
                    // Open the stream and read it back.
                    using (var reader = new TextFieldParser(stream))
                    {
                        reader.TextFieldType = FieldType.Delimited;
                        reader.SetDelimiters(delimiter);

                        DataSet   ds                     = null;
                        DataTable dataTable              = null;
                        string    colChangedBy           = null;
                        string    colChangedAt           = null;
                        string    colCreatedBy           = null;
                        string    colCreatedAt           = null;
                        var       obligateDataTypesCount = DataType.SystemDataTypes().Count;

                        try
                        {
                            while (!reader.EndOfData)
                            {
                                var linecsv = reader.ReadFields();
                                if (dataTable == null)
                                {
                                    ds           = CreateFields(linecsv, udtController);
                                    dataTable    = ds.Tables[DataSetTableName.Data];
                                    colChangedBy = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_ChangedBy);
                                    colChangedAt = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_ChangedAt);
                                    colCreatedBy = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_CreatedBy);
                                    colCreatedAt = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_CreatedAt);
                                }
                                else
                                {
                                    var insertTime = DateTime.Now.ToString("s");
                                    var newRow     = dataTable.NewRow();
                                    newRow[DataTableColumn.RowId] = -1;
                                    newRow[colChangedBy]          = UserInfo.Username;
                                    newRow[colCreatedBy]          = UserInfo.Username;
                                    newRow[colChangedAt]          = insertTime;
                                    newRow[colCreatedAt]          = insertTime;
                                    dataTable.Rows.Add(FillRow(linecsv, newRow, obligateDataTypesCount));
                                }
                            }

                            if (ds != null)
                            {
                                for (var rowNr = 0; rowNr <= dataTable.Rows.Count - 1; rowNr++)
                                {
                                    udtController.UpdateRow(ds, rowNr, isDataToImport: true);
                                }
                                mc.UpdateModuleSetting(moduleId, SettingName.ListOrForm, "List");
                            }
                        }
                        catch (Exception ex)
                        {
                            strMessage = string.Format(Localization.GetString("importError", LocalResourceFile), ex.Message);
                        }
                    }
                }
            }

            return(strMessage);
        }
        string ImportModule(int moduleId, string fileName, string folder, string delimiter)
        {
            var strMessage = "";

            var mc = new ModuleController();
            var moduleInfo = mc.GetModule(moduleId, TabId, false);

            if (Path.GetExtension(fileName).ToUpper() == ".CSV" && moduleInfo != null)
            {
                var udtController = new UserDefinedTableController(ModuleContext );
              
                var file = FileManager.Instance.GetFile(PortalId, Path.Combine(folder, fileName));
                using (var stream = FileManager.Instance.GetFileContent(file))
                {
                    // Open the stream and read it back.
                    using (var reader = new TextFieldParser(stream))
                    {
                        reader.TextFieldType = FieldType.Delimited;
                        reader.SetDelimiters(delimiter);

                        DataSet ds = null;
                        DataTable dataTable = null;
                        string colChangedBy = null;
                        string colChangedAt = null;
                        string colCreatedBy = null;
                        string colCreatedAt = null;
                        var obligateDataTypesCount = DataType.SystemDataTypes().Count;

                        try
                        {
                            while (!reader.EndOfData)
                            {
                                var linecsv = reader.ReadFields();
                                if (dataTable == null)
                                {
                                    ds = CreateFields(linecsv, udtController);
                                    dataTable = ds.Tables[DataSetTableName.Data];
                                    colChangedBy = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_ChangedBy);
                                    colChangedAt = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_ChangedAt);
                                    colCreatedBy = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_CreatedBy);
                                    colCreatedAt = udtController.ColumnNameByDataType(ds, DataTypeNames.UDT_DataType_CreatedAt);
                                }
                                else
                                {
                                    var insertTime = DateTime.Now.ToString("s");
                                    var newRow = dataTable.NewRow();
                                    newRow[DataTableColumn.RowId] = -1;
                                    newRow[colChangedBy] = UserInfo.Username;
                                    newRow[colCreatedBy] = UserInfo.Username;
                                    newRow[colChangedAt] = insertTime;
                                    newRow[colCreatedAt] = insertTime;
                                    dataTable.Rows.Add(FillRow(linecsv, newRow, obligateDataTypesCount));
                                }
                            }

                            if (ds != null)
                            {
                                for (var rowNr = 0; rowNr <= dataTable.Rows.Count - 1; rowNr++)
                                {
                                    udtController.UpdateRow(ds, rowNr, isDataToImport: true);
                                }
                                mc.UpdateModuleSetting(moduleId, SettingName.ListOrForm, "List");
                            }
                        }
                        catch (Exception ex)
                        {
                            strMessage = string.Format(Localization.GetString("importError", LocalResourceFile), ex.Message);
                        }
                    }
                }

                
            }

            return strMessage;
        }