Thursday, March 16, 2023

"Invalid private key file" exception while connecting SFTP server.

 I got a requirement to upload a file to SFTP. 

  • But while connecting SFTP they are using a private key file with a passphrase (password).
  • The same file I have used from the WinSCP application. It is connected and working.
  • But the same file I have triggered from C#. Then that time I got "Invalid private key file" exception error.
To resolve this we need to convert that private key from putty format to SSH Format.

Open WinSCP application. Under tools select "Run Puttygen".




Load the private key in WinSCP.
        



After loading the private key. In the conversions select the "Export OpenSSH key".





The newly generated key we can use in our code.
    var privateKey = new PrivateKeyFile(@"C:\some\path\key.pem", "passphrase");
    var client = new SftpClient("example.com", "username", new[] { privateKey });
    client.Connect();

If the private key is encrypted:
    var privateKey = new PrivateKeyFile(@"C:\some\path\key.pem", "passphrase");

Keep Daxing!!



Open table browser using x++

 Open table browser using x++.


If we want to open the table browser we need to open visual studio, right-click on the table and select the option like the below screenshot.




My functional required one form and in that he expected one field. That field lookup contains all table lists.


So I have created one form and added a field and button.



Lookup method:

    public void lookup()
    {
	Query                   query;
	QueryBuildDataSource    qbds;
	SysTableLookup          lookup;
	
	super();

	query 	= new Query();
	qbds 	= query.addDataSource(tableNum(SqlDictionary));
	lookup 	= SysTableLookup::newParameters(tableNum(SqlDictionary), this);
	
	qbds.clearRanges();
	qbds.addRange(fieldNum(SqlDictionary, fieldId)).value('0');
	
	lookup.addLookupfield(fieldNum(SqlDictionary, Name));
	lookup.addLookupfield(fieldNum(SqlDictionary, SqlName));
	lookup.parmQuery(query);
	lookup.performFormLookup();
    }


Clicked method:

    public void clicked()
    {           
	SysTableBrowser sysTableBrowser;            

	str tName;

	super();
	
	tName = TableName.valueStr();
	
	if (!tName)
	{
	    throw error(strfmt("@ApplicationFoundation:SysTableBrowser_MissingTableName"));
	}

	sysTableBrowser = new SysTableBrowser();
	sysTableBrowser.parmTableName(tName);
	sysTableBrowser.run(tablename2id(tName));
    }

Keep Daxing!!