Tuesday, September 24, 2019

CRM remove email warning "The email below might contain script or content that is potentially harmful and has been blocked."




  1. Download and install the solution
    • You can get the latest version under this link: https://github.com/seanmcne/OrgDbOrgSettings/releases
  2. Open the solution and locate the “SecuritySettingforEmail” entry
    • Open the Solution and activate the tracking through pressing of the “Add” button.
  3. Change the “SecuritySettingforEmail” value from 1 to 3

Loop through sys columns and change data in tables to proper case

DECLARE @SQL varchar(max)
DECLARE @MyCursor CURSOR
DECLARE @TableName char(50)
DECLARE @FieldName char(50)

--Get a list of all tables in DB
if object_id('tempdb..#temp') is not null
drop table #temp
--45
select o.name TableName,c.name FieldName, t.name datatype , c.max_length , 2 keep
into #temp
from sys.objects o inner join sys.columns c on o.object_id =c.object_id inner join sys.types t on c.user_type_id =t.user_type_id
where type = 'U' and   t.precision =0 and t.max_length >0
and o.name not like 'meta%' and o.name not like 'sysd%' 
and c.name in  ('Address1', 'Address2', 'City', 'Country', 'County', 'Description','FirstName', 'LastName', 'Name',  'JobTitle', 'MiddleName')

--Display the top 3 records of each table         
SET @MyCursor = CURSOR FOR
 select rtrim(ltrim(TableName)),rtrim(ltrim(FieldName))  from #temp order by fieldname

OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @TableName, @FieldName
WHILE @@FETCH_STATUS = 0
BEGIN

IF @fieldname = 'Country'
BEGIN
--SET @SQL = ' SELECT TOP 3 '''+ @fieldname +''', dbo.topropercase(' + rtrim(ltrim(@fieldname)) + ') FROM  ' + rtrim(ltrim(@TableName) + ' where ' + @fieldname + ' not in (''USA'', ''US'') and len(rtrim(ltrim(' + @fieldname + ')))>2') 
SET @SQL = 'UPDATE ' + rtrim(ltrim(@TableName) + 'SET '+ @fieldname +'= dbo.topropercase(' + rtrim(ltrim(@fieldname)) + ')  where ' + @fieldname + ' not in (''USA'', ''US'') and len(rtrim(ltrim(' + @fieldname + ')))>2') 
END
ELSE
BEGIN
--SET @SQL = ' SELECT TOP 3 '''+ @fieldname +''', dbo.topropercase(' + rtrim(ltrim(@fieldname)) + ') FROM  ' + rtrim(ltrim(@TableName) + ' where ' + @fieldname + ' is not null') 
SET @SQL = 'UPDATE ' + rtrim(ltrim(@TableName) + 'SET '+ @fieldname +'= dbo.topropercase(' + rtrim(ltrim(@fieldname)) + ')  where ' + @fieldname + ' is not null') 
END

 --set @sql = 'select '+ @fieldname +' from ' + @TableName
--select @SQL
EXEC(@SQL)

FETCH NEXT FROM @MyCursor
INTO @TableName, @FieldName
END
CLOSE @MyCursor
DEALLOCATE @MyCursor

SQL funtion to change text to proper case

CREATE FUNCTION ToProperCase(@string VARCHAR(255)) RETURNS VARCHAR(255)
AS
BEGIN
  DECLARE @i INT           -- index
  DECLARE @l INT           -- input length
  DECLARE @c NCHAR(1)      -- current char
  DECLARE @f INT           -- first letter flag (1/0)
  DECLARE @o VARCHAR(255)  -- output string
  DECLARE @w VARCHAR(10)   -- characters considered as white space

  SET @w = '[' + CHAR(13) + CHAR(10) + CHAR(9) + CHAR(160) + ' ' + ']'
  SET @i = 1
  SET @l = LEN(@string)
  SET @f = 1
  SET @o = ''

  WHILE @i <= @l
  BEGIN
    SET @c = SUBSTRING(@string, @i, 1)
    IF @f = 1 
    BEGIN
     SET @o = @o + @c
     SET @f = 0
    END
    ELSE
    BEGIN
     SET @o = @o + LOWER(@c)
    END

    IF @c LIKE @w SET @f = 1

    SET @i = @i + 1
  END

  RETURN @o
END

Thursday, July 25, 2019

Load image to SQL


INSERT INTO [Images]
           ([ImageId]
           ,[FileName]
           ,[FileSize]
           ,[ImageExt]
           ,[CreatedDateTime]
           ,[ModifiedDateTime]
           ,[CreatedBy]
           ,[ModifiedBy]
           ,[CompanyId]
           ,[ImageData])
     select
           'Test3'
           ,'Test3'
           ,'Test3'
           ,'jpg'
           ,'1/1/2019'
           ,'1/1/2019'
           ,'Test'
           ,'Test'
           ,'Test'
   ,bulkColumn FROM OPENROWSET(BULK N'e:\temp\monica\flowers.jpg', SINGLE_BLOB) image

Thursday, July 18, 2019

SQL Cursor

DECLARE @SQL varchar(max)
DECLARE @MyCursor CURSOR
DECLARE @TableName char(50)

--Get a list of all tables in DB
if object_id('tempdb..#tableList') is not null
drop table #tableList
select name, object_id into #tableList from sys.objects where type='u' and name not like 'sys%'

--Display the top 3 records of each table
SET @MyCursor = CURSOR FOR
 select name  from #tableList

OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN

SET @SQL = ' SELECT TOP 3 * FROM  ' + rtrim(ltrim(@TableName))

EXEC(@SQL)

FETCH NEXT FROM @MyCursor
INTO @TableName
END
CLOSE @MyCursor
DEALLOCATE @MyCursor

Thursday, June 27, 2019

c# Convert a comma separate string into astring array and loop

string fruit = "Apple,Banana,Orange,Strawberry";
string[] split = fruit.Split(',');

foreach (string item in split)
{
    Console.WriteLine(item);
}

Friday, June 21, 2019

c# Create Datatable



 public static DataTable CreateDataTable()
        {

            DataTable dt = new DataTable();

            DataColumn col1 = new DataColumn("FirstName");
            DataColumn col2 = new DataColumn("LastName");
            DataColumn col3 = new DataColumn("Age");

            col1.DataType = System.Type.GetType("System.String");
            col2.DataType = System.Type.GetType("System.String");
            col3.DataType = System.Type.GetType("System.String");

            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);

            DataRow row1 = dt.NewRow();
            row1[col1] = "Bob";
            row1[col2] = "Smith";
            row1[col3] = "47";
            dt.Rows.Add(row1);

            DataRow row2 = dt.NewRow();
            row2[col1] = "Tina";
            row2[col2] = "Smith";
            row2[col3] = "34";
            dt.Rows.Add(row2);

            DataRow row3 = dt.NewRow();
            row3[col1] = "Maria";
            row3[col2] = "Smith";
            row3[col3] = "27";
            dt.Rows.Add(row3);

            return dt;

        }