Monday, January 16, 2012

Access Microsoft Dynamics NAV 5.0 Database from web through C/Front dll .


Access Microsoft Dynamics NAV 5.0 Database from web through C/Front dll .

Three years back I got a requirement to create approval module to Customer/Vendor Payments from
Inside as well as outside. Challenge was access from the outside. I used to cfront dll to do this. In this article I have mentioned how to change the Native DB Password from outside the NAV.
01 ) I designed below layout.

02) Write the code (C#.Net) inside the btnChangePassword_Click Event using Microsoft.Navision.CFront


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Navision.CFront;













public partial class ChangePassword : System.Web.UI.Page
{

    protected void btnChangePassword_Click(object sender, EventArgs e)
    {
        int CT;
        int CR;
        string NewConPassword;
        string NewPassword1;
        if (txtNewPassword.Text.Trim() == txtConfirmPassword.Text.Trim())
        {

            NewConPassword = txtConfirmPassword.Text.Trim();
            NewPassword1 = txtNewPassword.Text.Trim();


            try
            {
                NavisionNetType NetType;

                NavisionDriverType DriverType;

                NavisionCode Data;
                NavisionText Password;

                DriverType = NavisionDriverType.Native;
                NetType = NavisionNetType.NativeTcp;

                CFrontDotNet.DriverType = DriverType;



                CFrontDotNet.NavisionPath = System.Configuration.ConfigurationManager.AppSettings.Get("NavisionPath");
                CFrontDotNet.Instance.ConnectServerAndOpenDatabase(System.Configuration.ConfigurationManager.AppSettings.Get("Database"), NetType, null, 2000, true, false, textUsername.Text.Trim(), textPassword.Text.Trim());
                CFrontDotNet.Instance.OpenCompany("CompanyName");
                CT = CFrontDotNet.Instance.OpenTable(2000000002);
                CR = CFrontDotNet.Instance.AllocRecord(CT);
                CFrontDotNet.Instance.SetFilter(CT, 1, textUsername.Text.Trim());

                try
                {
                    if (CFrontDotNet.Instance.FindRecord(CT, CR, "-"))
                    {
                        Data = CFrontDotNet.Instance.GetFieldData(CT, CR, 1);
                        Password = CFrontDotNet.Instance.GetFieldData(CT, CR, 2);

                        CFrontDotNet.Instance.CryptPassword(Data.ToString(), NewConPassword.ToString());
                        CFrontDotNet.Instance.BeginWriteTransaction();
                        CFrontDotNet.Instance.SetFieldData(CT, CR, 2, NavisionFieldType.Text, GetBytesByType(NavisionFieldType.Text, NewConPassword.ToString(), 2000000002, 2));
                        CFrontDotNet.Instance.ModifyRecord(CT, CR);
                        CFrontDotNet.Instance.EndWriteTransaction();

                        lblErrorMassage.Text = "Password has been changed";

                    }
                    else
                    {
                        lblErrorMassage.Text = "Invalid User Id/Password";
                    }
                }
                finally
                {
                    CFrontDotNet.Instance.FreeRecord(CR);
                }
            }
            catch(Exception ex)
            {
                lblErrorMassage.Text = "Invalid User Id/Password";
            }

        }
        else
        {
            lblErrorMassage.Text = "New Password and Confirm password are miss match";
        }
    }

03) New function to convert data types.


    private byte[] GetBytesByType(NavisionFieldType type, String value, int tableHandle,   int fieldNo)
    {
        switch (type)
        {
            case NavisionFieldType.Text:
                {
                    NavisionText n = NavisionText.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Option:
                {
                    NavisionOption n = new NavisionOption(Convert.ToInt32(value));
                    return n.GetBytes();
                }
            case NavisionFieldType.BigInteger:
                {
                    NavisionBigInteger n = NavisionBigInteger.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Binary:
                {
                    NavisionBinary n = NavisionBinary.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Blob:
                {
                    NavisionBlob n = NavisionBlob.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Boolean:
                {
                    NavisionBoolean n = NavisionBoolean.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Code:
                {
                    NavisionCode n = NavisionCode.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Date:
                {
                    NavisionDate n = NavisionDate.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.DateFormula:
                {
                    NavisionDateFormula n = NavisionDateFormula.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.DateTime:
                {
                    NavisionDateTime n = NavisionDateTime.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Decimal:
                {
                    NavisionDecimal n = NavisionDecimal.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Duration:
                {
                    NavisionDuration n = NavisionDuration.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Guid:
                {
                    NavisionGuid n = NavisionGuid.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Integer:
                {
                    NavisionInteger n = new NavisionInteger(int.Parse(value));
                    return n.GetBytes();
                }
            case NavisionFieldType.RecordId:
                {
                    NavisionRecordId n = NavisionRecordId.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.TableFilter:
                {
                    NavisionTableFilter n = NavisionTableFilter.Parse(value);
                    return n.GetBytes();
                }
            case NavisionFieldType.Time:
                {
                    NavisionTime n = NavisionTime.Parse(value);
                    return n.GetBytes();
                }
        }
        return new byte[] { };
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_unload(object sender, EventArgs e)
    {
        CFrontDotNet.Instance.CloseDatabase();
        CFrontDotNet.Instance.DisconnectServer();
    }
}





2 comments:

  1. i try this but know i am confused put cfront.dll all over the place client folder Server Folder Windows Folder System32 Folder SDK Folder but i always get error CFRONT could not be found or loaded ;-(

    Please where to put cfont dll thx

    ReplyDelete
  2. You need to register it under system32.

    ReplyDelete