Monday 3 November 2014

Common Functions




This function will get File Upload Control as input and save the file with adding time stamp before the file name and returns the file name.

public string UploadFile(FileUpload fup)
    {
        string FileName,TimeStamp;
        TimeStamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        fup.SaveAs(HttpContext.Current.Server.MapPath("~/image/" + TimeStamp + "_" + fup.PostedFile.FileName));
        FileName = TimeStamp + "_" + fup.PostedFile.FileName;
        return FileName;
    }

This function will convert list to datatable.

public static DataTable ListToDataTable<T>(IEnumerable<T> list)
        {
            DataTable table = new DataTable();
            if (list != null)
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
                foreach (PropertyDescriptor prop in properties)
                    table.Columns.Add(
                        prop.Name,
                        (prop.PropertyType.IsGenericType &&
                         prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            ? Nullable.GetUnderlyingType(prop.PropertyType)
                            : prop.PropertyType
                        );
                foreach (T item in list)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                    table.Rows.Add(row);
                }
            }
            return table;
        }


To Change Validators to Image

private void SetValidatorDisplay()
        {
            foreach (var validator in Page.Validators)
            {
                var reqvalidator = (validator as BaseValidator);
                if (reqvalidator != null)
                {
                    reqvalidator.Display = ValidatorDisplay.Dynamic;
                    if (!string.IsNullOrEmpty(reqvalidator.ErrorMessage))
                    {
                        if (!reqvalidator.ErrorMessage.Contains("<img"))
                        {
                            reqvalidator.ToolTip = reqvalidator.ErrorMessage;
                            string imagePath = General.VALIDATIONICON; // ADD VALIDATION IMAGE ICON TO DISPLAY VALIDATIONS
                           
                            reqvalidator.Text = @"<img src='" + imagePath + "' alt='" + reqvalidator.ToolTip + "'></img>";
                        }
                    }
                }
            }
        }


No comments:

Post a Comment