Sunday, June 13, 2010

.Net Class for convert Numbers to Arabic Characters - تفقيط

I think that this task is common on all finance related softwares, we always need to print numbers in words at end of some report, so i am listing below a C# class for doing so, i t will take 3 parameters:

1- double theNo : Numbers to be converted to characters along with fraction if needed.
2- string mainCurr : The main currency name such as Dinar.
3- string subCurr : Sub currency name, used for fractions.

----------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Global
{
class NumericToChar
{
public static string NumberToWord_Ar(double theNo, string mainCurr, string subCurr)
{
string NoToTxt = String.Empty;
string[] MyArry1 = new string[9 + 1];
string[] MyArry2 = new string[9 + 1];
string[] MyArry3 = new string[9 + 1];
string Myno = String.Empty;
string GetNo = String.Empty;
string RdNo = String.Empty;
string My100 = String.Empty;
string My10 = String.Empty;
string My1 = String.Empty;
string My11 = String.Empty;
string My12 = String.Empty;
string GetTxt = String.Empty;
string Mybillion = String.Empty;
string MyMillion = String.Empty;
string MyThou = String.Empty;
string MyHun = String.Empty;
string MyFraction = String.Empty;
string MyAnd = String.Empty;
int I = 0;
string ReMark = String.Empty;

if (theNo > 999999999999.999)
{
return NoToTxt;
}

if (theNo <>
{
theNo = theNo * -1.0;
ReMark = "يتبقى لكم ";
}
else
{
ReMark = " ";
}

if (theNo == 0.0)
{
NoToTxt = "صفر";
return NoToTxt;
}

MyAnd = " و ";
MyArry1[0] = "";
MyArry1[1] = "مائة";
MyArry1[2] = "مائتان";
MyArry1[3] = "ثلاثمائة";
MyArry1[4] = "أربعمائة";
MyArry1[5] = "خمسمائة";
MyArry1[6] = "ستمائة";
MyArry1[7] = "سبعمائة";
MyArry1[8] = "ثمانمائة";
MyArry1[9] = "تسعمائة";

MyArry2[0] = "";
MyArry2[1] = " عشر";
MyArry2[2] = "عشرون";
MyArry2[3] = "ثلاثون";
MyArry2[4] = "أربعون";
MyArry2[5] = "خمسون";
MyArry2[6] = "ستون";
MyArry2[7] = "سبعون";
MyArry2[8] = "ثمانون";
MyArry2[9] = "تسعون";

MyArry3[0] = "";
MyArry3[1] = "واحد";
MyArry3[2] = "اثنان";
MyArry3[3] = "ثلاثة";
MyArry3[4] = "أربعة";
MyArry3[5] = "خمسة";
MyArry3[6] = "ستة";
MyArry3[7] = "سبعة";
MyArry3[8] = "ثمانية";
MyArry3[9] = "تسعة";
// ======================
GetNo = theNo.ToString("000000000000.000");

I = 0;
while (I <>
{

if (I <>
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(GetNo, I + 1, 3)
Myno = GetNo.Substring(I + 1 - 1, 3);
}
else
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(GetNo, I + 2, 2)
Myno = "0" + GetNo.Substring(I + 2 - 1, 2);
}

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1 - 1, 3)), "0") > 0)
{

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
RdNo = Myno.Substring(1 - 1, 1);
My100 = MyArry1[Convert.ToInt32(RdNo)];
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
RdNo = Myno.Substring(3 - 1, 1);
My1 = MyArry3[Convert.ToInt32(RdNo)];
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
RdNo = Myno.Substring(2 - 1, 1);
My10 = MyArry2[Convert.ToInt32(RdNo)];

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 11)
{
My11 = "إحدى عشر";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 12)
{
My12 = "إثنى عشر";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 10)
{
My10 = "عشرة";
}

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if ((String.Compare((Myno.Substring(1 - 1, 1)), "00") > 0) && (String.Compare((Myno.Substring(2 - 1, 2)), "00") > 0))
{
My100 = My100 + MyAnd;
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
if ((String.Compare((Myno.Substring(3 - 1, 1)), "0") > 0) && (String.Compare((Myno.Substring(2 - 1, 1)), "1") > 0))
{
My1 = My1 + MyAnd;
}

GetTxt = My100 + My1 + My10;

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
if (((Myno.Substring(3 - 1, 1)) == "1") && ((Myno.Substring(2 - 1, 1)) == "1"))
{
GetTxt = My100 + My11;
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
if (((Myno.Substring(1 - 1, 1)) == "0"))
{
GetTxt = My11;
}
}

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
if (((Myno.Substring(3 - 1, 1)) == "2") && ((Myno.Substring(2 - 1, 1)) == "1"))
{
GetTxt = My100 + My12;
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
if (((Myno.Substring(1 - 1, 1)) == "0"))
{
GetTxt = My12;
}
}

if ((I == 0) && (GetTxt != ""))
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1 - 1, 3)), "10") > 0)
{
Mybillion = GetTxt + " مليار";
}
else
{
Mybillion = GetTxt + " مليارات";
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "2"))
{
Mybillion = " مليار";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "2"))
{
Mybillion = " ملياران";
}
}
}

if ((I == 3) && (GetTxt != ""))
{

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1 - 1, 3)), "10") > 0)
{
MyMillion = GetTxt + " مليون";
}
else
{
MyMillion = GetTxt + " مليون";
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "1"))
{
MyMillion = " مليون";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "2"))
{
MyMillion = " مليونان";
}
}
}

if ((I == 6) && (GetTxt != ""))
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1, 2)), "02") <= 0)
{
MyThou = GetTxt + " ألف";
}
else
{
MyThou = GetTxt + " ألف";
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
if (((Myno.Substring(3 - 1, 1)) == "1") && (Convert.ToInt32(Myno.Substring(2 - 1, 2)) != 11))
{
MyThou = " ألف";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
else if (((Myno.Substring(3 - 1, 1)) == "2") && (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 12))
{
MyThou = " ألفان";
}
else if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) <= 10)
{
MyThou = GetTxt + " آلاف";
}
}
}

if ((I == 9) && (GetTxt != ""))
{
MyHun = GetTxt;
}
if ((I == 12) && (GetTxt != ""))
{
MyFraction = Myno;
}
}

I = I + 3;
}

if ((Mybillion != ""))
{
if ((MyMillion != "") || (MyThou != "") || (MyHun != ""))
{
Mybillion = Mybillion + MyAnd;
}
}

if ((MyMillion != ""))
{
if ((MyThou != "") || (MyHun != ""))
{
MyMillion = MyMillion + MyAnd;
}
}

if ((MyThou != ""))
{
if ((MyHun != ""))
{
MyThou = MyThou + MyAnd;
}
}
if (theNo.ToString().IndexOf(".") != -1)
MyFraction = theNo.ToString("0.000").Substring(theNo.ToString("0.000").IndexOf(".") + 1);

if (MyFraction != "")
{
if ((Mybillion != "") || (MyMillion != "") || (MyThou != "") || (MyHun != ""))
{
NoToTxt = ReMark + Mybillion + MyMillion + MyThou + MyHun + " " + mainCurr + MyAnd + MyFraction + " " + subCurr;
}
else
{
NoToTxt = ReMark + MyFraction + " " + subCurr;
}
}
else
{
NoToTxt = ReMark + Mybillion + MyMillion + MyThou + MyHun + " " + mainCurr;
}

return NoToTxt;
}

}
}

-----------------------------------------------------------------------------------------------

Monday, July 6, 2009

Oracle Simple DBA Tasks

I recently started working heavily on Oracle database DBA stuff, export, import convert to arabic characterset ..etc.

So this blog will list all information regarding these tasks and how to accomplish inorder to be reference in any future similar dba tasks.

First of all whenver you try to make any dba you have to connect as sysdba user privilages for example

Open SQLPlus, and login as sys/sys as ssydba

- Export database

exp userid=dbuser/dbuserpassword file=c:\anyfile.dmp

- Import dump

If you need to import this dump file into another user you have to do the following:

1- If user not created, create new user

create user user_name identified by password

2- grant him dba privilages.

grant dba to user_name;

3- Import database:

imp file=c:\anyfile.dmp fromuser=dbuser touser=user_name userid=dbuser/dbuserpassword

Also if this user needed to be removed run the following:

DROP USER ahmaddb CASCADE;


Another needed task was converting the characterset to be arabic enabled.

1- First full backup should be taken from database.
2- You should shutdown database and make restricted session as follows:


SHUTDOWN IMMEDIATE;
STARTUP MOUNT;

ALTER SYSTEM ENABLE RESTRICTED SESSION;

ALTER DATABASE CHARACTER SET AR8MSWIN1256;

and by these steps your database engine will be arabic enabled.

One note is that when arabic is needed to be enabled on client machines having oracle client only, you should just change regsitry value on NLS_LANG as follows:

go to:

HKEY_LOCAL_MACHINE-->SOFTWARE-->ORACLE_HOME

Open NLS_LANG parameter and put AR8MSWIN1256 value instead of current one and this should be same as server value.

Wednesday, May 13, 2009

Debugging a Windows Service

When creating windows services using .Net we always face problems while debugging it, since the attach process approach of debugging is on my opinion useless. The following is code snippet i found on internet that will enable windows service debugging and will make it running like any other desktop application.


1- Create a windows service.



2- Put this code at Program.cs on Main method:


Service1 obj= new Service1();
obj.start();
do
{
System.Threading.Thread.Sleep(1000);
} while (true);


The new main method will look like this:

static void Main()
{
Service1 email = new Service1();
email.AnyMethod();
do
{
System.Threading.Thread.Sleep(1000);
} while (true);
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}



Where Service1 that you create intance from it your service name, and AnyMethod is any method that you create inside the service class.


When you run the service you will notice that the debugging is breaking at the AnyMethod that you just created at your service.

Wednesday, February 25, 2009

Database Log file Shrink

Some times i come across this need (shrinking database log file) and always do some research on internet to find the solution, but the problem is in most cases the info is not complete, so i am listing the steps below to be refrence for me and others in future :)

Step1: Taking backup from log file

BACKUP LOG myDBName WITH TRUNCATE_ONLY

Step2: Getting files logical name

EXEC sp_helpfile

it will be the first column on the list

Step3: Shrinking the database

DBCC
SHRINKFILE
(, 2000)
WITH NO_INFOMSGS

Notes:

1- Using the logical name of logfile not physical when shrinking.
2- Taking backup of log file before proceed (second step).

Hope this helps,

Salam.,

Monday, January 7, 2008

Custom Grid View

I think GridView is the most thing that will be used in any standard .Net project, but when we are using grid in our applications, we remember (after doing so many pages) that we want this gridView to have certain functionality like Google paging style, mouse hover coloring ..etc. So we end up by copying the same functionality in every page that contains same control !

To avoid this we have to create an inherited control class from gridview, and extend our functionality on it.

I have created a sample gridview that have the following functionality:

1- Google Style paging
2- Row Mouse Hover coloring.
3- Row Mouse Click Coloring.

And the main feature is that the idea of adding any new functionality once and not replicating it.

I will attach link to sample project to my created gridview as custom control. You can just download this project and understand the whole idea.


Sample Screen Shot:


Saturday, November 24, 2007

Create un-install setup shortcut and project

In order to create professional setup project using .Net setup and deployment projects, the you have to create your own way for un-installing the products using shortcut from start menu.

And you can accomplish that by:
1- creating a new project (console application) to your solution.
2- In program.cs class place the following code:
//**** Begin here
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace UninstallMyProject
{
///
/// Used to handle project custom uninstall from start menu or target directory
///

class Program
{
static void Main(string[] args)
{
string[] arguments = Environment.GetCommandLineArgs();

Process proc = new Process();
string path = Environment.GetFolderPath(Environment.SpecialFolder.System);

// Default product code must be changed if the product changed in setup project
string productCode = "{BBA4SSBD2-20B5-410C-8F23-C23E18806C7B}";

// if no arguments send then the uninstall called from exe itself ,, and
// for this we cant send the product code so assume the default one

if (arguments.Length == 1)
{
proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
proc.Start();
}
else
{
foreach (string argument in arguments)
{
string[] parameters = argument.Split('=');
if (parameters[0].ToLower() == "/u")
{
productCode = parameters[1];

proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
proc.Start();
}
}
}







}
}
}

//** Ends Here


3- Then you can just create Shortcut on your setup project, that point to this exe with the required product code as parameter. parameter in the shortcuts placed in argument property as: /u=[ProductCode]

Hope this information will be helpful, and if any help needed, just leave your comments.

Cheers,
Nasser

Friday, August 24, 2007

SharePoint Custom Web Parts

What is Web Part?

ASP.NET Web Parts controls are an integrated set of controls for creating Web sites that enable end users to modify the content, appearance, and behavior of Web pages directly in a browser. The topics in this section provide information on what Web Parts are, how they work, and how to use them to create user-customizable ASP.NET Web pages.

How to create custom VS 2005 web parts, Using Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions:

This is a new addons to VS 2005 IDE that will enable developers create web parts, by downloading this setup from here.

After installation, open VS 2005 IDE, click on New Project at File Menu, then go Share Point node at the left menu, then here you go, you got new project types, for example Web Parts, see below:



After that, select web part, then the projects open, and you can place any Html contents you wanted on following line sub that will appear:

protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
writer.write("Hello World web part.");
}

Then by Just clicking on "F5" & Run the VS will compile & deploy it at SharePoint, and by adding new web parts from the Share Point, you will notice that your web part will be available on the web parts galley !

Cheers,