Sunday 28 September 2014

CONVERT IP ADDRESS INTO URL IN C#

To convert an IP (Internet protocol) address into corresponding Url (Uniform Resource Locator)  in C#, ASP.NET you need a help of IPHostEntry and  IPAddress class. Make an object of this call and call the Dns.GetHostEntry() class to find the HostName against the Url.

IPAddress addr = IPAddress.Parse("195.175.254.2");
IPHostEntry entry = Dns.GetHostEntry(addr);
Response.Write(entry.HostName.ToString());

entry.HostName.ToString() will return the host name of the IP address.

Wednesday 6 August 2014

Export the data in excel format

Below is the code.

private void Save(string detail)
    {
        Response.ContentType = "application/ms-excel";
        Response.AddHeader("content-disposition", "attachment; filename=Data.xls");
        Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
        Response.Write("<head>");
        Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\">");
        Response.Write("<!--[if gte mso 9]>");
        Response.Write("<xml>");
        Response.Write("<x:ExcelWorkbook>");
        Response.Write("<x:ExcelWorksheets>");
        Response.Write("<x:ExcelWorksheet>");      
        Response.Write("<x:Name>Personal Detail</x:Name>");
        Response.Write("<x:WorksheetOptions>");      
        Response.Write("<x:Panes>");
        Response.Write("</x:Panes>");
        Response.Write("</x:WorksheetOptions>");
        Response.Write("</x:ExcelWorksheet>");
        Response.Write("</x:ExcelWorksheets>");
        Response.Write("</x:ExcelWorkbook>");
        Response.Write("</xml>");
        Response.Write("<![endif]-->");
        Response.Write("</head>");
        Response.Write("<body>");
        Response.Write(detail);
        Response.Write("</body>");
        Response.Write("</html>");
        // --------------------------------  
        Response.End();
    }

Wednesday 23 July 2014

Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.

Set ConcurrencyMode to Reentrant as shown below above the class implemented the Service contract interface.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

Set the property of the Form/Page class in the client application as below.
[CallbackBehavior(UseSynchronizationContext = false)]

Set the text box property CheckForIllegalCrossThreadCalls to false like below in the method describing duplext contract.
TextBox.CheckForIllegalCrossThreadCalls = false;

Thursday 10 July 2014

HTTP could not register URL http://+:8000/HelloWCF/. Your process does not have access rights to this namespace.

Most people are no longer going to be running with Administrator privileges by default like they were doing on earlier platforms. This impacts your ability to run HTTP web services because listening at a particular HTTP address is a restricted operation. By default, every HTTP path is reserved for use by the system administrator.

Your services will fail to start with an AddressAccessDeniedException if you aren't running the service from an elevated account. The account under which the Visual Studio and the debugger runs does not have the privilege (though the user account under which VS is runnig may be a part of Administrators group), and hence the error occurs. The plus sign in the URL just means that there's a wildcard being applied to the hostname.

Run application administration mode.

Right click on vs and run as administrator.

Tuesday 20 May 2014

Click Jacking

Clickjacking (also known as user-interface or UI redressing and IFRAME overlay) is an exploit in which malicious coding is hidden beneath apparently legitimate buttons or other clickable content on a website.
Ex: A visitor to a site thinks he is clicking on a button to close a window; instead, the action of clicking the “X” button prompts the computer to download a Trojan Horse, transfer money from a bank account or turn on the computer’s built-in microphone. The host website may be a legitimate site that's been hacked or a spoofed version of some well-known site. The attacker tricks users into visiting the site through links online or in email messages.
Suppose I am using iframe in your for some reason. I want to give security so that no one can hack using click jacking technique by loading his content instead of mine.
Suppose in design I am using iframe and id of iframe is ifShowThen just copy paste below code to prevent click jack on the site.

<script type="text/javascript" language="Javascript">
        function Check() {           
            try{
                if (window.top !== window.self) {
                    window.top.location = window.self.location;
                    return;
                }

                if (window.top.location.host != window.self.location.host) {
                    //window.top.location = window.self.location;
                    window.top.location = window.self.location;
                    return;
                }               
                var domain = document.getElementById('ifShow').src.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
                if (window.self.location.host != domain) {
                    window.top.location = window.self.location;
                    return;
                }
            }
            catch (ex)
            { window.top.location = window.self.location; /* everyone else */ }
           
        }
        setInterval(Check, 1000);
        Check();

    </script>

Thursday 13 March 2014

Disable/Enable all trigger in a database

To Disable All the Triggers
sp_MSforeachtable "ALTER TABLE ? DISABLE TRIGGER ALL"

To Enable All the Triggers
sp_MSforeachtable "ALTER TABLE ? ENABLE TRIGGER ALL"