Thursday 21 October 2010

C# .net Insert a Column into DataTable Source and Refresh DataGridView

Inserting rows at a particular point in a DataTable is easy, inserting columns was a bit trickier as the DataGridView didn't update or refresh its view of the DataSource;

if (dataGridView.SelectedCells.Count > 0)
{
  int selectedCellCol = dataGridView.SelectedCells[0].ColumnIndex;
  DataColumn newColumn = new DataColumn();
 
  // Set properties of the new column
  workingDataSet.Tables[0].Columns.Add(newColumn);
  newColumn.SetOrdinal(selectedCellCol);
  this.dataGridView.DataSource = null;
  this.dataGridView.DataSource = workingDataSet.Tables[0];
               
  this.dataGridView.Refresh();
 }

It seems setting the DataSource to null, followed by pointing it back at the DataTable source forced the data in the DataGridView to refresh.

Monday 11 October 2010

Microsoft Dynamics AX Call a Static Method from C#.Net X++

Install the .Net Business Connector is installed. If not, it can be using the AX Client Setup.

Add a reference to the Business Connector DLL "C:\Program Files\Microsoft Dynamics AX\50\Client\Bin\Microsoft.Dynamics.BusinessConnectorNet.dll"

Access the method as follows;

using Connector = Microsoft.Dynamics.BusinessConnectorNet;

Connector.Axapta axInstance;
axInstance = new Connector.Axapta();axInstance.LogonAs("Username", "Domain", null, null, null, null, "AXShortcut.axc");
returnVal = (string)ax.CallStaticClassMethod("MyAXClass", "MyAXMethod", "MyArgs");

The (string)
cast of the return value could change.

Map a drive in a Windows BAT file

The following can be used to map a drive in a windows bat file without causing an error when the drive already exists;

REM SETUP MAPPED DRIVE TO Y: IF IT Y: NOT EXIST
if not exist "y:" (net use y: \\myServerName\rootFolderOfMappedDrive)

Creating an FTP Windows bat Script to Get/Put Automatically

First create a windows bat file conatining something like the following;

@echo off
REM THIS IS A COMMENT AND WILL NOT RUN
REM AUTO FTP SCRIPT
REM ANTHONY BLAKE 11/09/2010


REM myftpurl.com COULD ALSO BE THE IP OF THE FTP SITE
ftp -s:"c:\SCRIPTS\ftpAuto.txt" myftpurl.com

REM DONE!
exit

Then create your ftpAuto.txt;

myUserName
myPassword
bin
cd /DestFolder

put c:\OutboundFiles\UploadFile.csv
bye


The bat file calls the Windows FTP command and tells it to use the ftpAuto.txt script. The script changes the remote folder on the ftp site to "DestFolder" and puts the local file UploadFile.csv onto the server.

Setup a Windows Scheduled task to run at a desired interval which runs the bat file.

Friday 8 October 2010

C#.net Read & Write XML Files

Code to Read an XML File into a DataSet;

this.workingDataSet = new DataSet();
FileStream xmlStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
workingDataSet.ReadXml(xmlStream);
xmlStream.Close();

Code to Write a DataSet to an XML file (UTF8);

FileStream xmlStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
XmlTextWriter xmlWriter = new XmlTextWriter(xmlStream, Encoding.UTF8);
workingDataSet.WriteXml(xmlWriter);
xmlStream.Close();

Microsoft Dynamics AX 2009 WinAPIServer & WinAPI Class

There are lots of uses for the WinAPI & WinAPIServer classes in Dynamics AX. Two static methods I have found useful recently are;

WinAPIServer::copyFile(...);
WinAPIServer::deleteFile(...);


There isnt a moveFile method however just using the copy then delete method works well. The advantage of these WinAPIServer class methods over the WinAPI class methods are that file permissions are handled by the methods themselves.

Thursday 7 October 2010

Writing to a CSV in Microsoft Dynamics AX 2009 X++

This post has moved to my new website, if not redirected automaticaly please click here: https://anthonyblake.github.io/ax2009/2010/07/10/ax2009-write-csv.html 

To write to a csv in Microsoft Dynamics AX 2009 X++

//Macros
#File

//File Access Vars
FileIOPermission permission;
CommaTextIO commaio;
FilePath filePath = @'C:\MyFolder\MyOutput.csv';
;

//Set Write Permission For New File
permission = new FileIOPermission(filePath, #io_write);
permission.assert();

//Create CommaIO Object
commaio = new CommaTextIO(filePath, #io_write);

//You Might Want The Next Line To Go In A Loop
commaio.write('Field1', 'Field2', 'Field3', 'etc...etc');

//After Your Loop...
//This Line Isn't Always Needed But Releases
// The Lock On The Output File If It Is To Be Moved
commaio = null;

//Reset Write Access
CodeAccessPermission::revertAssert();

Useful Windows Batch File Commands

This post has moved to my new website, if not redirected automaticaly please click here: https://anthonyblake.github.io/windows/cli/2010/07/10/useful-win-batch-commands.html 

The following code checks if a directory exists, if not it is created. It then moves a file into that directory.

This is ideal code to run before an ftp script as it will prevent a half written file being transmitted via FTP.

@echo off
if not exist "C:\Folder\Destination"
(

md "C:\Folder\Destination"
)
move "C:\Folder\Source\*.*"
"C:\Folder\Destination"

pause

The pause command lets you see the results of the script. Remove it if automated on a Windows Scheduled Task.