Thursday, February 11, 2016

Javascript Object Oriented

<html>
<head>
<script>
function Say() {
//private
var nameA = "ha";
//public
this.nameB = "he";
//private
function sayBye() {
return "Bye!" + nameA;
}
//Privileged -> cannot be shared by all instances
this.sayHello = function() {
return "Hello!" + nameA;
}
}
//static
Say.nameC = "wa";
//static
Say.sayBye = function() {
return "Bye!" + this.nameC;
}
//public -> can be shared by all instances
Say.prototype.sayHi = function() {
return "Hi!" + this.nameB;
}
var a = new Say();
a.sayWa = function ()
{
return "Wa!" + a.nameB;
}
alert(a.sayHello());
alert(a.sayHi());
alert(Say.sayBye());
alert(a.sayWa());
var b = new Say();
alert(b.sayHi());
alert(b.sayWa());  //throw exception
</script>
</head>
<body>
</body>
</html>

Friday, June 13, 2014

Get all value from Divs using Jquery

I would like to get all values under a div list into an array.

var list = new Array();
$('.className').each(function() {list.push(this.value);});

Tuesday, January 28, 2014

SSRS: System.Security.Permissions.SecurityPermission

I install the assembly in GAC and reference the dll in a report template. When I use a custom dll to generate barcode in SSRS, an error message is shown.
"Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

I know that it's something about the permission issue. But I don't have any idea on it. After that, I find out that the configuration of reporting server and report designer are two different thing. And my target is reporting server only.

(1). Register dll in GAC

(2). Grant full trust in rssrvpolicy.config
<CodeGroup class="UnionCodeGroup"
   version="1"
   PermissionSetName="FullTrust"
   Name="ThisDataProviderCodeGroup"
   Description="Code group for the .NET data provider">
      <IMembershipCondition class="UrlMembershipCondition"
         version="1"
         Url=
"C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\bin\DataProviderAssembly.dll"
       />
</CodeGroup>

After restarting the reporting services, I upload the report to my reporting server and the report works well.


Monday, November 25, 2013

Configuration of Class Library

I have developed a ws client in a class library and the code of the ws client is generated by adding a web references.  When I want to move the ws client from uat environment to production environment, the client does not work at all. The problem is raised since I only update the configuration of app.config  inside the class library. As the ws client is a class library, the configuration in app.config will be ignored. Afterwards, I update the  Reference.cs which is generated by vs.

.Net: This application needs to use a cryptographic key

My application is required to integrated with a HTTPS web services. A popup windows is shown when I execute the console application under debug mode. It  requests some permission for key access. After I grant the access, the application works fine.



 Then, I publish the application to IIS and windows security popup is shown when I browse the site. Actually, the site doesn't work. 


The absence of windows security popup gives me some idea on the permission on existing private key. 

There are two ways to solve above issue.
1. Give permission to this folder C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys directly
2. Go to cmd-> type "mmc"-> add snapin certificate->click folder personal->right click->All Tasks->import your private key->right click your private key->All Tasks->Manage Private Key...->grant the permission.






Friday, September 27, 2013

Soap WS-Security Header : One of "SOAP Header" elements required

I run a C# web service client to connect to a Java web service. Then, it throws a exception: [com.ibm.wsspi.wssecurity.SoapSecurityException:WSEC5048EL One of "SOAP Header" elements required]. As no header is defined in the wsdl, I have no idea about the exception and don't know what should be added to the header. Further, the generated web service client does not have a header property. That means, I cannot add the header to the client directly.

After searching, I found out that WS-Security is a standard. 


Then, I try to find out where I can insert the header to the requested XML. Below it's a one of the solutions that I found out on ASP.NET forum.

WSHeader .cs
 public class WSHeader : SoapExtension
    {
        public bool outgoing = true;
        public bool incoming = false;
        private Stream outputStream;
        public Stream oldStream;
        public Stream newStream;

        // The ChainStream, GetInitializers, Initialize, and ProcessMessage are required
        // for a Soap override.  ChainStream and ProcessMessage are what we need.
        /// <summary>
        /// Override.  Save old stream, create new one.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public override Stream ChainStream(Stream stream)
        {
            // save a copy of the stream, create a new one for manipulating.
            this.outputStream = stream;
            oldStream = stream;
            newStream = new MemoryStream();
            return newStream;
        }
        #region "overrides of no interest"
        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override object GetInitializer(Type serviceType)
        {
            return null;
        }
        public override void Initialize(object initializer)
        {
            return;
        }
        #endregion
        /// <summary>
        /// Return a string version of the XML
        /// </summary>
        /// <returns></returns>
        public string getXMLFromCache()
        {
            newStream.Position = 0; // start at the beginning!
            string strSOAPresponse = ExtractFromStream(newStream);
            return strSOAPresponse;
        }
        /// <summary>
        /// Transfer the text from the target stream from the 
        /// current position.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        private String ExtractFromStream(Stream target)
        {
            if (target != null)
                return (new StreamReader(target)).ReadToEnd();
            return "";
        }
        /// <summary>
        /// Override.  Process .AfterSerialize and .BeforeDeserialize
        /// to insert a non-standard SOAP header with username and 
        /// password (currently hard-coded).
        /// </summary>
        /// <param name="message"></param>
        public override void ProcessMessage(SoapMessage message)
        {
            StreamReader readStr;
            StreamWriter writeStr;
            string soapMsg1;
            XmlDocument xDoc = new XmlDocument();
            // a SOAP message has 4 stages.  We're interested in .AfterSerialize
            switch (message.Stage)
            {
                case SoapMessageStage.BeforeSerialize:
                    break;

                case SoapMessageStage.AfterSerialize:
                    {
                        // Get the SOAP body as a string, so we can manipulate...
                        String soapBodyString = getXMLFromCache();

                        // Strip off the old header stuff before the message body
                        // I'm not completely sure, but the soap:encodingStyle might be
                        // unique to the WebSphere environment.  Dunno.
                        String BodString = "<soap:Body>";
                        int pos1 = soapBodyString.IndexOf(BodString) + BodString.Length;
                        int pos2 = soapBodyString.Length - pos1;
                        soapBodyString = soapBodyString.Substring(pos1, pos2);
                        soapBodyString = "<soap:Body>" + soapBodyString;

                        // Create the SOAP Message 
                        // It's comprised of a <soap:Element> that's enclosed in <soap:Body>. 
                        // Pack the XML document inside the <soap:Body> element 
                        //String xmlVersionString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                        String soapEnvelopeBeginString = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://tempuri.org/\" xmlns:types=\"http://tempuri.org/encodedTypes\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
                        String soapEnvHeaderString = "<soap:Header><Security><UsernameToken><Username>";
                        String soapEnvHeaderString2 = "</Username><Password>";
                        String soapEnvHeaderString3 = "</Password></UsernameToken></Security></soap:Header>";

                        Stream appOutputStream = new MemoryStream();
                        StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
                        //soapMessageWriter.Write(xmlVersionString);
                        soapMessageWriter.Write(soapEnvelopeBeginString);
                        //// The heavy-handed part - forcing the right headers AND the uname/pw :)
                        soapMessageWriter.Write(soapEnvHeaderString);
                        soapMessageWriter.Write(ConfigurationManager.AppSettings["WSUserName"]);
                        soapMessageWriter.Write(soapEnvHeaderString2);
                        soapMessageWriter.Write(ConfigurationManager.AppSettings["WSPassword"]);
                        soapMessageWriter.Write(soapEnvHeaderString3);
                        // End clubbing of baby seals
                        // Add the soapBodyString back in - it's got all the closing XML we need.
                        //soapBodyString = dammit;
                        soapMessageWriter.Write(soapBodyString);
                        // write it all out.
                        soapMessageWriter.Flush();
                        appOutputStream.Flush();
                        appOutputStream.Position = 0;
                        StreamReader reader = new StreamReader(appOutputStream);
                        StreamWriter writer = new StreamWriter(this.outputStream);
                        writer.Write(reader.ReadToEnd());
                        writer.Flush();
                        appOutputStream.Close();
                        this.outgoing = false;
                        this.incoming = true;
                        break;
                    }
                case SoapMessageStage.BeforeDeserialize:
                    {
                        //Make the output available for the client to parse...
                        readStr = new StreamReader(oldStream);
                        writeStr = new StreamWriter(newStream);
                        soapMsg1 = readStr.ReadToEnd();
                        xDoc.LoadXml(soapMsg1);
                        soapMsg1 = xDoc.InnerXml;
                        writeStr.Write(soapMsg1);
                        writeStr.Flush();
                        newStream.Position = 0;
                        break;
                    }
                case SoapMessageStage.AfterDeserialize:
                    break;
                default:
                    throw new Exception("invalid stage!");
            }
        }
    }

Add following to web.config 
<system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="TDCWS.WSHeader,TDCWS" priority="1" group="Low"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>


Hope that can help!

References:

Monday, August 12, 2013

TFS: Access to the path is denied

I want to download latest version from TFS 2012 to test the project is uploaded to source control successfully or not. When I run the project, an error is occurred.


After a while, I discover the file property of the project is read-only. Hence, access is denied.

Then, I just wonder why the project become read-only. It is because I haven't check out the project through TFS before the download. So, please remember to check out the project via TFS or just uncheck the read-only properties of the project.




Thursday, June 20, 2013

href vs onclick

I would like to generate a link dynamically. Then, a javascript function is used to redirect to another page. original: It can really redirect the page, but IE doesn't indicate that the page is loading. update: It works fine under IE which can show the page is loading.

Friday, April 26, 2013

RegisterArrayDeclaration

I would like to use ClientScript.RegisterArrayDeclaration to declare an array in client side.So that, it looks like following code.
Client side:
var CheckBoxIDs = new Array(
'ctl00_MainContent_GridViewList_ctl02_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl03_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl04_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl05_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl06_CheckBoxItem');

Server side:
ClientScript.RegisterArrayDeclaration("CheckBoxIDs ", checkboxID);

It works fine. However, I need to add elements into the array dynamically. Instead of using ClientScript, ScriptManageer should be used this time.

ScriptManager RegisterArrayDeclaration

Wednesday, April 24, 2013

SerializationException: VewState

In order to save temporary data within a .aspx page, ViewState is my choice.  The data is a List of custom object.
ViewState["TempData"] = new List<ABC>();
Unfortunately, it throws an SerizationException. This error message shows that the class is not serializable. After I add "[Serializable] "  to the class, the problem is solved. But, I don't know why the class needs to be serialized. 

This issue is related to how ViewState packs data into a hidden field. ViewState serialized data into based 64 string and then stored into hidden form field. Later, page can get back state data by deserialized the hidden field.


Understanding ASP.NET View State
[ASP.NET] 利用ViewState保存自訂物件

Wednesday, April 17, 2013

Allow Remote WMI connection in Windows Servers 2008 R2

I wanna a C# program to get disk space of a remote server whose OS is Windows Server 2008 R2. The program and the server are under different domains. I use server's domain account to authenticate the connection. 

Unluckily, the most different part of this task is the configuration of enabling remote WMI connection, not the program to get a disk space. Two error codes:  

=> The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)  
=> Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))  

I have spent much time on the first one and it seems there are some thing wrong with my config. Here are some steps I still remember:

1. Allow WMI to communicate through firewall



2. Enable "remote launch" and "remote activation" in Access Permission of Component Services



3. Enable "remote access" in Access Permission of Component Services



4. Enable "remote enable" in WMI Control Properties



You can simply test WMI connection by using wmic command
wmic /node:localhost /user:administrator /password:1234 bios get serialnumber 

Monday, February 25, 2013

Enable DataSet View in Reporting Services

I would like to enable a view to display DataSet in a report. But, I go to Tab [View] and then search it through [Other Windows]. No relevant window can be found. After googling, the solution is View -> Reporting Data (Ctrl + Alt + D)

Thursday, December 6, 2012

SQL SERVER: Execute SSIS Package Using Store Procedure

Originally, I used 'Microsoft.SqlServer.Dts.Runtime'to execute local SSIS package under asp.net Web application. Under client's environment, SQL Server and web application are hosted on different Windows servers. Now, I use store procedure to execute SSIS package placed in DB server.
 
set @params = '/conn \"a\";"\"' + @a+ '\"" /set \package.variables[b].Value;"\"' + @b+ '\""'
set @cmd = 'C:\dtexec.exe /f ' + @PackagePath + ' ' + @params
EXEC xp_cmdshell @cmd

Dont forget to enable 'xp_cmdshell' option first!

I had to used 32 bit version of dtexec.exe as my package need OLEDB provider Jet 4.0. The provider is not available under 32 bit environment. As a result, I use the x86 version to handle this issue.

Errors when executing SQL Server SSIS Package From Stored Procedure
How to Call SSIS Package from the Stored Procedure

Thursday, November 22, 2012

Allow downloading mdb file from ASP.Net page

I would like to let users download a file after they click a button. I just use Response.Redirect("~/123.mdb") to handle this situation. 

But, an error message is shown: 
"There is no build provider registered for the extension '.mdb'. You can register one in the section in machine.config or web.config"

To Deal with this issue, go to web.config, add a fileExtension element in web.config

FileExtensions in web.config

Tuesday, October 30, 2012

Call SSIS package in C#

I need to call a SSIS package in a web application to import access data into SQL Server. However, I discover the performance is extremely low. And I don't know it's my personal problem or common issue. If possible, don't use it.

Package package = null;
DTSExecResult result;

//connection manager
package = new Application().LoadPackage(packagePath + "\\importData.dtsx", null);
package.Connections["rs"].ConnectionString = connectionStr;

//variable
Variables vars = package.Variables;
vars["TenderNumber"].Value = tenderNum;

result = package.Execute();

if (result == DTSExecResult.Failure)
{
    foreach (var e in package.Errors)
    {
        logHelper.Error("Package Execution Error", new Exception(e.Description));
    }

    throw new Exception(package.Errors[0].ToString());
}

CommandTimeout in Entity Framework

I use Entity Framework in my web application and then use it to execute a stored procedure command in SQL Server. Unluckily, I receive a connection timeout issue. First, I try to add 'Connection Timeout=120;' into connection string. But, the problem still exists. Then, solving the issue by CommandTimeout 

using (RSEntities rEntity = new RSEntities())
{
  //import data from temp tables
  rEntity.CommandTimeout = 120;
  rEntity.ExecuteStoreCommand("EXEC dbo.ImportMdbData");
}

Blank option in ASP.NET DropDownList

I would like to create an blank option in DropDownList by default and don't use append to add options into the list. So, I bind the datasource first and then insert the blank option afterwards.

.aspx.cs
DropDownTenderNumber.DataSource = list;
DropDownTenderNumber.DataBind();
DropDownTenderNumber.Items.Insert(0, new ListItem("----PLEASE SELECT----", "0"));

Further, I use 'InitialValue' in RequiredFieldValidator to exclude the blank option.

.aspx

Freeze/Lock Web Page While Processing in ASP.Net

I would like to create a layer to cover a page while it is processing. First, downloading a loading icon from ajaxload. Then, you just need to paste following code in .aspx file.

UpdateProgress

    
        
Loading
UpdatePanel

    
        
    
    
        
    

Script Manager


CSS
.overlay  
{
    position: fixed;
    z-index: 98;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    background-color: #aaa; 
    filter: alpha(opacity=80); 
    opacity: 0.8; 
}
.overlayContent
{
    z-index: 99;
    margin: 250px auto;
    width: 80px;
    height: 80px;
}
.overlayContent h2
{
    font-size: 18px;
    font-weight: bold;
    color: #000;
}
.overlayContent img
{
    width: 60px;
    height: 60px;
}
Use UpdateProgress To Cover Screen

I have tried to browse it use chrome, firefox and ie and it works fine.

globalization Element in web.config

I would like to provide a textbox to user and let them filter data by a given date in (dd/MM/yyyy) format.



query = query.Where(i => i.OperationDate <= end);

Unfortunately, query results seems not working properly. It seems that SQL Server uses date format (MM/dd/yyyy) to do the searching. Then, the question is how to specify the date format in my web application. By adding following line in web.config, the issue is solved.


CultureInfo Class:
Represents information about a specific culture including the names of the culture,the writing system, and the calendar used, as well as access to culture-specific objects that provide information for common operations, such as formatting dates and sorting strings.

Thursday, October 4, 2012

SSIS: DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER

I create an SSIS project using SQL Server Business Intelligence Development Studio under Windows Server 2008 R2. When I run the project, something is wrong and an error message is shown below:

"[OLE DB Source [1]] Error: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER."

I don't get any idea from above message. After I disable the Run64BitRunTime option, everything work fine.