<?xml version="1.0" encoding="UTF-8" ?>
<rss version="0.91">
<channel><title>Logiclabz</title><link>http://www.logiclabz.com/</link>
<description>Logiclabz : Web development Tips and Tricks in Asp.Net, C#, Javascript, HTML, XML and XSLT etc.,</description><language>en-us</language>
<item>
<title>Alter/Change new Default Value of a Column in Sql Server 2005</title><link>http://www.logiclabz.com/sql-server/alter-change-new-default-value-of-a-column-in-already-created-table.aspx</link><description><![CDATA[<pre name="code" class="sql">
ALTER TABLE {table-name} DROP CONSTRAINT {column-constraint-name}
</pre>
<pre name="code" class="sql">
ALTER TABLE {table-name} WITH NOCHECK 
ADD CONSTRAINT [{column-constraint-name}] DEFAULT (6) FOR {column-name}
</pre>]]></description></item>
<item>
<title>T-SQL to Disable/Enable Job in SQL Server 2005</title><link>http://www.logiclabz.com/sql-server/t-sql-to-disable-enable-job-in-sql-server-2005.aspx</link><description><![CDATA[<p>The following query shows the list of jobs running on the sql server.</p>
<pre name="code" class="sql">
select * from msdb..sysjobs
</pre>
<p><input type="image" src="http://www.logiclabz.com/postimg/disable-sql-server-job-using-t-sql.gif" alt="List of Sql server Job on the server" /></p>
<p>From the msdb..sysjobs, the required job_id for enable or disable to be taken.</p>
<p>T-SQL to dis-enable specific job in sql server</p>
<pre name="code" class="sql">
 EXEC msdb..sp_update_job @job_id = '770F175B-C8A0-4157-BACD-0AF129DF570C', @enabled = 0  
</pre>
<p>T-SQL to enable specific job in sql server</p>
<pre name="code" class="sql">
 EXEC msdb..sp_update_job @job_id = '770F175B-C8A0-4157-BACD-0AF129DF570C', @enabled = 1  
</pre>
<p>&nbsp;</p>]]></description></item>
<item>
<title>Invoke URL from Sql Server 2005 Stored Procedure</title><link>http://www.logiclabz.com/sql-server/invoke-url-from-sql-server-2005-stored-procedure.aspx</link><description><![CDATA[<p>At many times, there are requirements to alert us through email or SMS.</p>
<p>On Sql Server these alert mechanism always found to be tedious.</p>
<p>Though sql server on there own provided some alert sending tools, but i found to complex in configuring them.</p>
<p>I found an other way of alerting us through invoking an url which in-turn would send email or sms as per our requirement.</p>
<p>By using xp_cmdshell command in sql server, operating system shell command can be executed.  So, xp_cmdshell command can be used to execute a vbscript file.</p>
<p>Inturn Vb Script can send email or invoke a url to alert us.   xp_cmdshell statement to be included to call the vbscript</p>
<pre name="code" class="sql">
EXEC master..xp_cmdshell 'C:\winnt\system32\cscript.exe c:\sqljobalert.vbs',no_output  
</pre>
<p>VBScript source code of c:\sqljobalert.vbs</p>
<pre name="code" class="sql">
call main()
sub main()
  'Below code would send an email from vbscript
	Dim cdomsg, icon, Flds
	Set cdomsg = wscript.CreateObject (&quot;CDO.Message&quot;)
	set icon = wscript.CreateObject (&quot;CDO.configuration&quot;)
	Set Flds = icon.Fields 
	Flds.Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;) = &quot;your smtp server&quot;
	Flds.Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;) = 2
	flds.Update
	set cdomsg.Configuration = icon
	
	cdoMsg.To = &quot;to mail id&quot;
	cdoMsg.From = &quot;from mail id&quot;
	cdoMSg.Subject = &quot;Subject of the mail&quot;
	cdomsg.TextBody = &quot;Email Message Content&quot;
	cdomsg.send

   'Below code would invoke an url from vbscript
	Dim xmlHTTP, urlString
	Set xmlHTTP = WScript.CreateObject(&quot;Msxml2.XMLHTTP&quot;)
	urlString = &quot;http://url to be invoked&quot;
	xmlHTTP.Open &quot;GET&quot;, urlString , False
	xmlHTTP.Send  &quot;&quot;
end sub </pre>
<p>This can be used to alert on sql server job failure</p>
<p>&nbsp;</p>]]></description></item>
<item>
<title>ASP.Net C# function to Detect Request from Mobile Device</title><link>http://www.logiclabz.com/asp-net/detect-request-url-from-mobile-device-pda.aspx</link><description><![CDATA[<p>
Most of popular websites have own mobile websites on their subdomain which is not so popular. Hence when mobile users go to main webpage should be redirected to their mobile site.
</p>
<p>
So we need to write a small piece of code in main webpage to detect whether the user is from mobile device.<br />
If the user is detected to be from mobile, we can redirect to its corresponding mobile website.
</p>
<p>
The ASP.Net C# function to detect whether the request is from mobile device.
</p>
<pre name="code" class="c-sharp">
    public static Boolean isMobile()
    {
        HttpContext curcontext = HttpContext.Current;

        string user_agent = curcontext.Request.ServerVariables["HTTP_USER_AGENT"];
        user_agent = user_agent.ToLower();
        
        
        // Checks the user-agent
        if (user_agent != null )
        {
            // Checks if its a Windows browser but not a Windows Mobile browser
            if (user_agent.Contains("windows") && !user_agent.Contains("windows ce"))
            {
                return false;
            }

            // Checks if it is a mobile browser
            string pattern = "up.browser|up.link|windows ce|iphone|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp";
            MatchCollection mc = Regex.Matches(user_agent, pattern, RegexOptions.IgnoreCase);
            if (mc.Count &gt; 0)
                return true;

           // Checks if the 4 first chars of the user-agent match any of the most popular user-agents
            string popUA = "|acs-|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv|palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3c |wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda|xda-|";
            if (popUA.Contains("|" + user_agent.Substring(0, 4) + "|"))
                return true;
        }
        
        // Checks the accept header for wap.wml or wap.xhtml support
        string accept = curcontext.Request.ServerVariables["HTTP_ACCEPT"];
        if (accept != null )
        {
            if (accept.Contains("text/vnd.wap.wml") || accept.Contains("application/vnd.wap.xhtml+xml"))
            {
                return true;
            }
        }

        // Checks if it has any mobile HTTP headers

        string x_wap_profile = curcontext.Request.ServerVariables["HTTP_X_WAP_PROFILE"];
        string profile = curcontext.Request.ServerVariables["HTTP_PROFILE"];
        string opera = curcontext.Request.Headers["HTTP_X_OPERAMINI_PHONE_UA"];

        if (x_wap_profile != null || profile != null || opera != null )
        {
            return true;
        }

        return false;
    }
</pre>]]></description></item>
<item>
<title>RSS pubDate in .Net c#</title><link>http://www.logiclabz.com/c/rss-pubdate-in-net-c.aspx</link><description><![CDATA[<p>
RSS pubDate is the publication date for the content in the channel.
</p>
<p>
RSS pubDate element is of format "Wed, 14 Sep 1983 00:00:01 GMT"
</p>
<p>
This pubData element format can be achieved in C# .Net using the following piece of code.
</p>

<pre name="code" class="c-sharp">
DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss K")
</pre>

<p>
output would be 
</p>
<p>
<strong>"Tue, 03 Nov 2009 13:45:57 +05:30"</strong>
</p>]]></description></item>
<item>
<title>C# .Net: enumerate Hashtable elements|Loop Hashtable elements</title><link>http://www.logiclabz.com/c/to-enumerate-through-all-the-keys-values-of-a-hashtable-in-c-net.aspx</link><description><![CDATA[<p>
Since each element of the hashtable is of IDictionary key/value pair type.

Hashtable can be enumerated through DictionaryEntry structure in System.Collections namespace in c#.

DictionaryEntry class defines a dictionary key/value pair that can be set or retrieved.
</p>

<p>
The following sample would help in hashtable elements enumeration.
</p>



<pre name="code" class="c-sharp">
//create a new hashtable
System.Collections.Hashtable hsh = new System.Collections.Hashtable();

//add some elements in the hashtable
hsh.Add("1", "london dreams review");
hsh.Add("2", "karisma");
hsh.Add("3", "the band movie");
hsh.Add("4", "end of days");

//enumerate or looping through all the elements in the hashtable
foreach (System.Collections.DictionaryEntry entry in hsh)
{
  Console.WriteLine("Key: " + entry.Key.ToString() + " | " + "Value: " + entry.Value.ToString());
}
</pre>]]></description></item>
<item>
<title>.Net C# Function to Convert XML Document into HTML String using XSLT</title><link>http://www.logiclabz.com/c/net-c-function-to-convert-xml-document-into-html-string-using-xslt.aspx</link><description><![CDATA[<p>
XML file can be transformed to HTML using XSLT.

The following C# function would helps us converting XML Document to HTML String using XSLT file.
</p>


Namespace used:

<pre name="code" class="c-sharp">
	using System.Xml;
	using System.Xml.Xsl;
</pre>
<pre name="code" class="c-sharp">
	public static string ConvertXML(XmlDocument InputXMLDocument, string XSLTFilePath
 , XsltArgumentList XSLTArgs)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            XslCompiledTransform xslTrans = new XslCompiledTransform();
            xslTrans.Load(XSLTFilePath);
            xslTrans.Transform(InputXMLDocument.CreateNavigator(), XSLTArgs, sw);
            return sw.ToString();
        }
</pre>


Sample XML file

<pre name="code" class="xml">
	&lt;?xml version="1.0" encoding="utf-8" ?&gt;
	&lt;sexystars&gt;
	  &lt;category name="Bollywood"&gt;
	    &lt;sexystar name="Antra mali" /&gt;
	    &lt;sexystar name="Deepika Padukone" /&gt;
	    &lt;sexystar name="Mandira Bedi" /&gt;
	  &lt;/category&gt;
	  &lt;category name="Hollywood"&gt;
	    &lt;sexystar name="Jennifer Lopez" /&gt;
	    &lt;sexystar name="Jessica Alba" /&gt;
	  &lt;/category&gt;
	&lt;/sexystars&gt;
</pre>

Sample XSLT file

<pre name="code" class="xml">
	&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
	&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
	  &lt;xsl:output omit-xml-declaration="yes"/&gt;
	  &lt;xsl:template match="sexystars"&gt;
	    &lt;div&gt;
	      &lt;xsl:apply-templates select="category" /&gt;
	    &lt;/div&gt;
	  &lt;/xsl:template&gt;
	  &lt;xsl:template match="category"&gt;
	    &lt;h2&gt;
	      &lt;xsl:value-of select="@name"/&gt;
	    &lt;/h2&gt;
	    &lt;ul&gt;
	      &lt;xsl:apply-templates select="sexystar" /&gt;
	    &lt;/ul&gt;
	  &lt;/xsl:template&gt;
	  &lt;xsl:template match="sexystar"&gt;
	    &lt;li&gt;
	      &lt;xsl:value-of select="@name"/&gt;
	    &lt;/li&gt;
	  &lt;/xsl:template&gt;
	&lt;/xsl:stylesheet&gt;
</pre>

ConvertXML function can be used as

<pre name="code" class="c-sharp">
	XmlDocument xDoc=new XmlDocument();
        xDoc.Load(@"C:\sample.xml");
        string returnhtml = ConvertXML(xDoc, @"C:\sample.xslt", new XsltArgumentList());
</pre>

Output would be

<pre name="code" class="xml">
	&lt;div&gt;
	  &lt;h2&gt;Bollywood&lt;/h2&gt;
	    &lt;ul&gt;
	      &lt;li&gt;Antra mali&lt;/li&gt;
	      &lt;li&gt;Deepika Padukone&lt;/li&gt;
	      &lt;li&gt;Mandira Bedi&lt;/li&gt;
	    &lt;/ul&gt;
	  &lt;h2&gt;Hollywood&lt;/h2&gt;
	    &lt;ul&gt;
	      &lt;li&gt;Jennifer Lopez&lt;/li&gt;
	      &lt;li&gt;Jessica Alba&lt;/li&gt;
	    &lt;/ul&gt;
	&lt;/div&gt;
</pre>]]></description></item>
<item>
<title>Blocking Dynamic IP’s from Crawling Website in IIS</title><link>http://www.logiclabz.com/general/blocking-dynamic-ip-s-from-crawling-website-in-iis.aspx</link><description><![CDATA[<p>
Crawling from Dynamic IP’s can be with or without cookies i.e. generally without cookies may be due to some program written to crawl and with cookie would be manual crawl.
</p>
<br/>
<img alt="block dynamic IP crawlers in IIS" src="/postimg/block-dynamic-ip-IIS.jpg" style="float: left;margin-right:10px;" />
<h3>Dynamic IP’s without cookie</h3>
<p>
	Crawling without cookie would happen generally due to some program written to crawl. When program is written to crawl, the request to our website from this IP would be in huge number than normal.
</p>
<p>
	This type of crawling can be handled by storing the incoming IP’s to our website in a HASHTABLE with count. Thereby if the count for a particular IP reaches a limit in a given time, we can take a measure to block this IP or to show a captcha image.
</p>
<p>
	HASHTABLE can be cleared when count of HASHTABLE reaches a defined count, so that memory of the application due to HASHTABLE is maintained without hauling our website.
</p>
<br/>
<br/>
<h3>Dynamic IP’s without cookie</h3>
<p>
	Crawling with cookie would happen generally when group of people manually involved in taking the content of our website. 
Hence when captcha image is shown, they enter captcha and keeps crawling our website. When we block the IP, they recreate a new IP by rebooting the MODEM.
</p>
<p>
This type of crawling can be handled as follows
<ul class="nrmul">
<li>A distinct cookie can be set in 403.6 (IP address rejected) htm page in IIS, as 403.6 IP address rejected htm page would be normally shown when IP is blocked from our website.</li>
<li>Hence when new request comes with distinct cookie, we can take a measure to block them even if it’s from newly recreated IP by rebooting MODEM.</li>
</ul>
</p>
<p>
Though this cannot be 100% safe but at least we can make the crawler think for a while.
</p>
<p>
Also, the distinct security cookie thus created can be normal html cookie. But the crawler can easily clear this cookie through browser and can restart crawling. Hence to extend our security further we can create a flash cookie which cannot be cleared through any browser.
</p>]]></description></item>
<item>
<title>T-SQL to change schema/owner name in Sql Server</title><link>http://www.logiclabz.com/sql-server/t-sql-to-change-schema-owner-name-in-sql-server.aspx</link><description><![CDATA[<p>
Syntax for changing owner name or schema of database objects such as stored procedures, functions, and views is
</p> 
<pre name="code" class="sql" >
	exec sys.sp_changeobjectowner &lt;&lt;object name&gt;&gt;, &lt;&lt;new owner name&gt;&gt;
</pre>
<p>
The <b>object name(@objname)</b> parameter should in the format  "[owner].[object]".
The <b>new owner name(@newowner)</b> should be valid name from 
</p>
<pre name="code" class="sql" >
	select * from sysusers
</pre>
<img src="/postimg/list-of-sys-users-sql-server.gif" title="select * from sysusers" alt="select * from sysusers"/>

<p>
The following sql stores list of user created table with name and its schema or owner name
</p>
<pre name="code" class="sql" >
	select t.[name] as TableName,t.object_id,s.[name] as OwnerName 
	from sys.tables t  inner join sys.schemas s
	on s.schema_id=t.schema_id where t.is_ms_shipped=0
</pre>
<img src="/postimg/list-of-tables-with-schema.gif" title="list of tables with name and its schema" alt="list of tables with name and its schema"/>

<p>
The following example changes the table name "CityStateMaster" of "dbo" schema to "guest" schema.
</p>
<pre name="code" class="sql" >
	sp_changeobjectowner 'guest.CityStateMaster',dbo
</pre>
<img src="/postimg/list-of-tables-with-schema-1.gif" title="list of tables with name and its schema" alt="list of tables with name and its schema"/>
<p>
The following sql can be used to generate sqls to convert schema or owner of all user created functions and stored procedures to 'dbo' owner.
</p>
<pre name="code" class="sql" >
SELECT 'exec sp_changeobjectowner ''' + QUOTENAME(ROUTINE_SCHEMA) + '.' +
QUOTENAME(ROUTINE_NAME) + ''',' + 'dbo;' FROM INFORMATION_SCHEMA.ROUTINES
WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),'IsMSShipped') = 0	
</pre>]]></description></item>
<item>
<title>Download file with UserId and Password using .Net C#</title><link>http://www.logiclabz.com/c/download-file-with-userid-and-password-using-net-c.aspx</link><description><![CDATA[<p>
System.Net.NetworkCredential class in .Net provides credentials for password-based authentication schemes.
</p>

<p>
System.Net.WebClient class in .Net provides DownloadData method to download file from given URI.
</p>

<p>
System.Net.WebClient is given with the required login username and password details with System.Net.NetworkCredential.
</p>

<p>
DownloadData method gives the result byte[] of the file, which can be stored to path using System.IO.FileStream class.
</p>

<pre name="code" class="c-sharp">
WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential("username", "password");
byte[] buff = wc.DownloadData("http://www.somedomain.com/filewithlogin.zip");
using (System.IO.FileStream fs = 
new FileStream(@"C:\downloads\filewithlogin.zip", FileMode.Create))
{
      fs.Write(buff, 0, buff.Length);
      fs.Close();
}
</pre>]]></description></item>
</channel></rss>