Tuesday, September 4, 2012

Hosting WCF inside asp Virtual Directory

Step 1)

 VirtualDirectoryName->Properties->Virtual Directory Tab->Confirguration Button->

Insert new WildCard Mapping C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll 

Uncheck Verify that file exists

Step 2)

VirtualDirectoryName->Properties->Directory Security Tab->Authentication and access control->Edit Button->

Uncheck Integrated Windodws Authentication

Step3) 


Reset IIS

Thursday, December 22, 2011

crossbrowser InnerText

function getInnerText(o)
{
return o.textContent ? o.textContent : o.innerText
}

Wednesday, November 30, 2011

process to create database mirroring when servers are not under domain

As the severs are not part of domain, we cannot use ntlm or kerberos to authenticate the user,
so we need to create certificates, then create login and user to use that certificate, create end point using the above user such that both sql servers can trust each other


Principal server steps
----------------------
--CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'pa55w0rd!' ;

USE master;
SELECT * FROM sys.certificates;


USE master;
CREATE CERTIFICATE principal_mirror_cert
WITH SUBJECT = '172.23.5.22 certificate for database mirroring',
EXPIRY_DATE = '1/1/2100';
GO



SELECT name, role_desc, state_desc, connection_auth_desc, encryption_algorithm_desc
FROM sys.database_mirroring_endpoints;

drop ENDPOINT Endpoint_Mirroring


CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS TCP (
LISTENER_PORT=5023
, LISTENER_IP = ALL
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE principal_mirror_cert
, ENCRYPTION = REQUIRED ALGORITHM AES
, ROLE = ALL
);
GO


BACKUP CERTIFICATE principal_mirror_cert TO FILE = 'D:\Mirroring_Cert\principal_mirror_cert.cer';
GO

xp_fixeddrives


USE master;
CREATE LOGIN secondary_mirror_login WITH PASSWORD = 'pa55w0rd!';
GO
--Create a user for that login.

CREATE USER secondary_mirror_user FOR LOGIN secondary_mirror_login;
GO
--Associate the certificate with the user.

CREATE CERTIFICATE secondary_mirror_cert
AUTHORIZATION secondary_mirror_user
FROM FILE = 'D:\Mirroring_Cert\secondary_mirror_cert.cer'
GO

GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO secondary_mirror_login;
GO

--At HOST_A, set server instance on HOST_B as partner (mirror server).
ALTER DATABASE school
SET PARTNER = 'TCP://172.23.5.44:5023';
GO

Mirror Server Steps
-----------------------
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'pa55w0rd!' ;

SELECT * FROM sys.certificates;

USE master;
CREATE CERTIFICATE secondary_mirror_cert
WITH SUBJECT = '172.23.5.44 certificate for database mirroring',
EXPIRY_DATE = '1/1/2100';
GO

drop endpoint Endpoint_Mirroring

CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS TCP (
LISTENER_PORT=5023
, LISTENER_IP = ALL
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE secondary_mirror_cert
, ENCRYPTION = REQUIRED ALGORITHM AES
, ROLE = ALL
);
GO


SELECT name, role_desc, state_desc, connection_auth_desc, encryption_algorithm_desc
FROM sys.database_mirroring_endpoints;

xp_fixeddrives

BACKUP CERTIFICATE secondary_mirror_cert TO FILE = 'D:\Mirroring_Cert\secondary_mirror_cert.cer';
GO


USE master;
CREATE LOGIN principal_mirror_login WITH PASSWORD = 'pa55w0rd!';
GO
--Create a user for that login.

CREATE USER principal_mirror_user FOR LOGIN principal_mirror_login;
GO
--Associate the certificate with the user.

CREATE CERTIFICATE principal_mirror_cert
AUTHORIZATION principal_mirror_user
FROM FILE = 'D:\Mirroring_Cert\principal_mirror_cert.cer'
GO

GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO principal_mirror_login;
GO

--At HOST_B, set server instance on HOST_A as partner (principal server):
ALTER DATABASE school
SET PARTNER = 'TCP://172.23.5.22:5023';
GO

ALTER DATABASE school
SET PARTNER off;
GO

Tuesday, November 8, 2011

Excel 2007 automation on top of a Windows Server 2008 x64

This solution is ...

・Windows 2008 Server x64

Please make this folder.

C:\Windows\SysWOW64\config\systemprofile\Desktop

・Windows 2008 Server x86

Please make this folder.

C:\Windows\System32\config\systemprofile\Desktop

Tuesday, October 11, 2011

Compatiablity Error while using Merge Statement in the database in SQL Server

ALTER DATABASE [RestoredDatabase] SET COMPATIBILITY_LEVEL = 100
OR
SP_DBCMPTLEVEL 'RestoredDatabase',100

Wednesday, September 28, 2011

C# – Create Excel file from DataTable without using Excel Object

DataTable dtCustomer = new DataTable("Customer");
dtCustomer.Columns.Add(new DataColumn("Name"));
dtCustomer.Columns.Add(new DataColumn("Gender"));
DataRow drItem = dtCustomer.NewRow();
drItem["Name"] = "rongchaua";
drItem["Gender"] = "Male";
dtCustomer.Rows.Add(drItem);
drItem = dtCustomer.NewRow();
drItem["Name"] = "4nh7i3m";
drItem["Gender"] = "Male";
dtCustomer.Rows.Add(drItem);
ExcelCreator.Create(dtCustomer,@"E:\Export.xls");
MessageBox.Show("Export done");




class ExcelCreator
{
    public static void Create(DataTable dtSource, string strFileName)
    {
        XmlTextWriter xtwWriter = new XmlTextWriter(strFileName, Encoding.UTF8);
        xtwWriter.Formatting = Formatting.Indented;
        xtwWriter.WriteStartDocument();
        xtwWriter.WriteProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"");
        xtwWriter.WriteStartElement("Workbook", "urn:schemas-microsoft-com:office:spreadsheet");
        xtwWriter.WriteAttributeString("xmlns", "o", null, "urn:schemas-microsoft-com:office:office");
        xtwWriter.WriteAttributeString("xmlns", "x", null, "urn:schemas-microsoft-com:office:excel");
        xtwWriter.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");
        xtwWriter.WriteAttributeString("xmlns", "html", null, "http://www.w3.org/TR/REC-html40");
        xtwWriter.WriteStartElement("DocumentProperties", "urn:schemas-microsoft-com:office:office");
        xtwWriter.WriteElementString("Author", Environment.UserName);
        xtwWriter.WriteElementString("LastAuthor", Environment.UserName);
        xtwWriter.WriteElementString("Created", DateTime.Now.ToString("u") + "Z");
        xtwWriter.WriteElementString("Company", "Unknown");
        xtwWriter.WriteElementString("Version", "11.8122");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteStartElement("ExcelWorkbook", "urn:schemas-microsoft-com:office:excel");
        xtwWriter.WriteElementString("WindowHeight", "13170");
        xtwWriter.WriteElementString("WindowWidth", "17580");
        xtwWriter.WriteElementString("WindowTopX", "120");
        xtwWriter.WriteElementString("WindowTopY", "60");
        xtwWriter.WriteElementString("ProtectStructure", "False");
        xtwWriter.WriteElementString("ProtectWindows", "False");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteStartElement("Styles");
        xtwWriter.WriteStartElement("Style");
        xtwWriter.WriteAttributeString("ss", "ID", null, "Default");
        xtwWriter.WriteAttributeString("ss", "Name", null, "Normal");
        xtwWriter.WriteStartElement("Alignment");
        xtwWriter.WriteAttributeString("ss", "Vertical", null, "Bottom");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteElementString("Borders", null);
        xtwWriter.WriteElementString("Font", null);
        xtwWriter.WriteElementString("Interior", null);
        xtwWriter.WriteElementString("NumberFormat", null);
        xtwWriter.WriteElementString("Protection", null);
        xtwWriter.WriteEndElement();
        xtwWriter.WriteEndElement();
        xtwWriter.WriteStartElement("Worksheet");
        xtwWriter.WriteAttributeString("ss", "Name", null, dtSource.TableName);
        xtwWriter.WriteStartElement("Table");
        xtwWriter.WriteAttributeString("ss", "ExpandedColumnCount", null, dtSource.Columns.Count.ToString());
        xtwWriter.WriteAttributeString("ss", "ExpandedRowCount", null, dtSource.Rows.Count.ToString());
        xtwWriter.WriteAttributeString("x", "FullColumns", null, "1");
        xtwWriter.WriteAttributeString("x", "FullRows", null, "1");
        xtwWriter.WriteAttributeString("ss", "DefaultColumnWidth", null, "60");
        foreach (DataRow row in dtSource.Rows)
        {
            xtwWriter.WriteStartElement("Row");
            foreach (object cellValue in row.ItemArray)
            {
                xtwWriter.WriteStartElement("Cell");
                xtwWriter.WriteStartElement("Data");
                xtwWriter.WriteAttributeString("ss", "Type", null, "String");
                xtwWriter.WriteValue(cellValue);
                xtwWriter.WriteEndElement();
                xtwWriter.WriteEndElement();
            }
            xtwWriter.WriteEndElement();
        }
        xtwWriter.WriteEndElement();
        xtwWriter.WriteStartElement("WorksheetOptions", "urn:schemas-microsoft-com:office:excel");
        xtwWriter.WriteStartElement("PageSetup");
        xtwWriter.WriteStartElement("Header");
        xtwWriter.WriteAttributeString("x", "Margin", null, "0.4921259845");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteStartElement("Footer");
        xtwWriter.WriteAttributeString("x", "Margin", null, "0.4921259845");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteStartElement("PageMargins");
        xtwWriter.WriteAttributeString("x", "Bottom", null, "0.984251969");
        xtwWriter.WriteAttributeString("x", "Left", null, "0.78740157499999996");
        xtwWriter.WriteAttributeString("x", "Right", null, "0.78740157499999996");
        xtwWriter.WriteAttributeString("x", "Top", null, "0.984251969");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteEndElement();
        xtwWriter.WriteElementString("Selected", null);
        xtwWriter.WriteStartElement("Panes");
        xtwWriter.WriteStartElement("Pane");
        xtwWriter.WriteElementString("Number", "1");
        xtwWriter.WriteElementString("ActiveRow", "1");
        xtwWriter.WriteElementString("ActiveCol", "1");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteEndElement();
        xtwWriter.WriteElementString("ProtectObjects", "False");
        xtwWriter.WriteElementString("ProtectScenarios", "False");
        xtwWriter.WriteEndElement();
        xtwWriter.WriteEndElement();
        xtwWriter.WriteEndElement();
        xtwWriter.Flush();
        xtwWriter.Close();
    }
}

Thursday, August 18, 2011

Load Browser with URL from VB

Private Const SW_SHOW = 5 ' Displays Window in its current size and position
Private Const SW_SHOWNORMAL = 1 ' Restores Window if Minimized or Maximized
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function FindExecutable Lib "shell32.dll" Alias _
"FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As _
String, ByVal lpResult As String) As Long

Private Sub Command1_Click()

Dim FileName As String, Dummy As String
Dim BrowserExec As String * 255
Dim RetVal As Long
Dim FileNumber As Integer
' First, create a known, temporary HTML file
BrowserExec = Space(255)
FileName = "C:\temphtm.HTM"
FileNumber = FreeFile ' Get unused file number
Open FileName For Output As #FileNumber ' Create temp HTML file
Write #FileNumber, " <\HTML>" ' Output text
Close #FileNumber ' Close file
' Then find the application associated with it
RetVal = FindExecutable(FileName, Dummy, BrowserExec)
BrowserExec = Trim(BrowserExec)
' If an application is found, launch it!
If RetVal <= 32 Or IsEmpty(BrowserExec) Then ' Error
MsgBox "Could not find associated Browser", vbExclamation, _
"Browser Not Found"
Else
RetVal = ShellExecute(Me.hwnd, "open", BrowserExec, _
"http://66.134.46.26/elroiuat/aspfiles/general/login.asp", Dummy, SW_SHOWNORMAL)
If RetVal <= 32 Then ' Error
MsgBox "Web Page not Opened", vbExclamation, "URL Failed"
End If
End If
Kill FileName ' delete temp HTML file
Unload Me
End Sub