Friday, September 18, 2020

AX - How to sort datasource

Simple add into datasource Init() method this code:
public void init()
{
  super();    
           
  smmEncyclopediaItems_ds.query().dataSourceNo( 1 ).
    addSortField( fieldNum( smmEncyclopediaItems, ParentId ), SortOrder::Ascending );
  smmEncyclopediaItems_ds.query().dataSourceNo( 1 ).
    addSortField( fieldNum( smmEncyclopediaItems, order ), SortOrder::Ascending );
  smmEncyclopediaItems_ds.query().dataSourceNo( 1 ).
    addSortField( fieldNum( smmEncyclopediaItems, ItemName ), SortOrder::Ascending );        
}

Tuesday, August 25, 2020

C# - How to make question into active directory (AD, LDAP)

This example shows all Petr names contained in defined AD:
/* -- question */
DirectoryEntry de = new DirectoryEntry("LDAP://OU=Comp Users,DC=EU,DC=company,DC=local");

/* -- define searcher */

DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectCategory=User)(objectClass=person)(givenname=*petr*))";
ds.Sort = new SortOption( "name", SortDirection.Ascending );
      
/* -- enumerate results */

SearchResultCollection results = ds.FindAll();
    
foreach ( SearchResult sr in results )
{
  string output = "";
  if ( sr.Properties["name"].Count > 0 ) 
  	output+= sr.Properties["name"][0].ToString();
  if ( sr.Properties["mail"].Count > 0 ) 
  	output+= " - " + sr.Properties["mail"][0].ToString();
  if ( sr.Properties["givenname"].Count > 0 ) 
  	output+= " - " + sr.Properties["givenname"][0].ToString();
  if ( sr.Properties["sn"].Count > 0 ) 
  	output+= " - " + sr.Properties["sn"][0].ToString();
  if ( sr.Properties["userPrincipalName"].Count > 0 ) 
  	output+= " - " + sr.Properties["userPrincipalName"][0].ToString();
  if ( sr.Properties["distinguishedName"].Count > 0 ) 
  	output+= " - " + sr.Properties["distinguishedName"][0].ToString();
        
  Console.WriteLine( output );
}
Output:
Bednaro, Petr - Petr.Bednar@company.com - Petr - Bednaro - bednarop@EU.company.local - 
CN=Bednaro\, Petr,OU=Czech Republic,OU=MKPE,OU=Comp Users,DC=EU,DC=company,DC=local

Cepka, Petr - Petr.Cepka@company.com - Petr - Cepka - cepkap@EU.company.local - 
CN=Cepka\, Petr,OU=Czech Republic,OU=BEIC,OU=Comp Users,DC=EU,DC=company,DC=local

Hartigo, Petr - Petr.Hartigo@company.com - Petr - Hartigo - hartigop@EU.company.local - 
CN=Hartigo\, Petr,OU=Czech Republic,OU=MKPE,OU=Comp Users,DC=EU,DC=company,DC=local
For DirectoryEntry is possible use Domain class:
DirectoryEntry de = domain.GetDirectoryEntry();      
If you want filter only for company BEIC, extent the filter in LDAP string:
DirectoryEntry de = new DirectoryEntry("LDAP://OU=BEIC,OU=Comp Users,DC=EU,DC=company,DC=local");

WIN OS - How to get important info about machine

systeminfo
Output:
For another information you can use command:
msinfo

Thursday, August 20, 2020

C# - How to get active domain name

Domain domain;
try {
  domain = Domain.GetCurrentDomain();
  Console.WriteLine( domain.Name );
}
catch ( ActiveDirectoryOperationException e ) {
  Console.WriteLine( "No domain" );
  return;
}
Output:
EU.company.local

Monday, July 13, 2020

C# - How to compute hash value for the file

using( var md5 = MD5.Create() )
using( var sha256 = SHA256.Create() )
{
  using ( FileStream stream = File.OpenRead( @"c:\_ax\3of9.ttf" ) )
  {          
    byte[] hashMD5 = md5.ComputeHash( stream );
    Console.WriteLine( BitConverter.ToString( hashMD5 ) );  

    byte[] hashSHA256 = sha256.ComputeHash( stream );
    Console.WriteLine( BitConverter.ToString( hashSHA256 ) );  
  }
}
Output:
DB-E9-C0-88-6B-BB-A1-84-5D-4C-D8-95-60-DD-91-4E
E3-B0-C4-42-98-FC-1C-14-9A-FB-F4-C8-99-6F-B9-24-27-AE-41-E4-64-9B-93-4C-A4-95-99-1B-78-52-B8-55

Tuesday, June 23, 2020

SQL SERVER - How to load data from text file to table ?

If I have this table:
column_name     datatype   length
--------------- ---------- ------
id              varchar    7
description     nvarchar   60
last_index      int        NULL
..and this text file (first three rows only here):
-94544;ovládání;74
-00000;přídavné podstavce;15
-01881;hřídel, nástavec-spojovací(keiper);145
For import file data to this table use this statement:
delete dbo.drawing_index;

bulk insert dbo.drawing_index
from 'c:\cieb\import\drawing_index.txt'
with
(
	CODEPAGE = '65001',	-- UTF-8
	fieldterminator = ';',
	ROWTERMINATOR = '\n'
)
Output:

Tuesday, June 2, 2020

C# - How to hide TabPage ?

TabPage.Hide() doesn`t work correctly. But you can use this trick:
TabCommon.Parent = null;
TabLabels.Parent = null;
Output:

Wednesday, May 6, 2020

C# - How get Max() on FindAll() - LINQ

This example finds between MapAreas items max(Page) - but only in same Row as mapArea.Row.
int page = this.MapAreas.FindAll( area => area.Row == mapArea.Row ).Max( x => x.Page );

Thursday, April 2, 2020

SQL SERVER - How to align string length to fixed size

With left() function is problem because ending spaces are lost. The solution is little trick with space() function.
select top 1 left( itemid, 25 ) + 'x' from inventitemprice
where
itemid = 'S-144-700-1006'

select top 1 left( itemid + space(100), 25 ) + 'x' from inventitemprice
where
itemid = 'S-144-700-1006'
Output:
--------------------------
S-144-700-1006x

(1 row(s) affected)


--------------------------
S-144-700-1006           x

(1 row(s) affected)

Monday, March 9, 2020

C# - How call stored proc with output/return parameter ?

public static string GetStatistics() {
  string value = "";

  SqlCommand command = DB.SPPrepare( "GetStatisticsAsString" );
            
  command.Parameters.Add( new SqlParameter( "@catalog", "HYG" ) );
  command.Parameters.Add( new SqlParameter( "@catalogDSO", "MYDSO" ) );
  SqlParameter par = new SqlParameter( "@output", System.Data.SqlDbType.NVarChar, 2048 );
  par.Direction = System.Data.ParameterDirection.Output;
  command.Parameters.Add( par );

  command.ExecuteNonQuery();

  value = (string)command.Parameters[ "@output" ].Value;

  return value;
}

Monday, March 2, 2020

C# - How to use generic Min(), Max()

shape.Saved_Width =
 Math.Abs( shape.Points.Max( item => item.Draw_X )-shape.Points.Min( item => item.Draw_X ) );

Friday, February 28, 2020

WIN OS - How to flush your machine DNS cache ?

ipconfig /flushdns

AX - How to make sorting in grid column

Override datasource Init() method:
public void init()
{
    super();

    this.query().dataSourceNo( 1 ).addSortField( fieldNum( CIEB_Torque, cieb_datetime ), 
      SortOrder::Ascending );
}
Output:

Friday, January 31, 2020

SQL SERVER - How to make cumulative sum column

If you need cumulative sum if SQL select, you can use analytical function:
select  cast( mag as int ) as mag, count(*) as count,
sum ( count(*) ) over 
( order by cast( mag as int ) rows between unbounded preceding and current row ) 
as count_cumul
from dso
where
catalog = 'MYDSO'
group by cast( mag as int )
order by cast( mag as int )
Output:
mag         count       count_cumul
----------- ----------- -----------
0           5           5
1           5           10
2           7           17
3           13          30
4           26          56
5           45          101
6           88          189
...

Tuesday, January 14, 2020

C# - How to sort generic list by item value

private List<ConstellationIndex> ConstellationsIndex = new List<ConstellationIndex>();
...
this.ConstellationsIndex.Sort( (x, y) => x.Constellation.CompareTo( y.Constellation ) );

Thursday, January 9, 2020

C# - How to go through files in directory with files copy

DirectoryInfo dir = new DirectoryInfo( this.OverviewMapCopyFromDir );

foreach( FileInfo file in dir.GetFiles( "overview_*.tif" ) ) {
  File.Copy( file.FullName, Path.Combine( m_FullOutputDirectory, file.Name ) );    
}

Friday, January 3, 2020

C# - How to convert first character of string to upper letter

The problem is little complicated, because access to string chars (through indexer) is only read-only.
One way is this:

row.Constellation = arr[2].Trim().ToLower();
char[] ch = row.Constellation.ToCharArray();
ch[0] = char.ToUpper( row.Constellation[0] );
row.Constellation = new string( ch );