<?xml version="1.0" encoding="iso-8859-9"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coskun SUNALI &#124; .net framework, c#, asp.net, atlas, visual studio, iis and everything else &#187; code snippet</title>
	<atom:link href="http://sunali.com/tag/code-snippet/feed/" rel="self" type="application/rss+xml" />
	<link>http://sunali.com</link>
	<description>Coşkun SUNALI</description>
	<lastBuildDate>Fri, 12 Feb 2010 12:35:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Some useful UpdatePanel extensions</title>
		<link>http://sunali.com/2008/12/05/some-useful-updatepanel-extensions/</link>
		<comments>http://sunali.com/2008/12/05/some-useful-updatepanel-extensions/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 16:09:21 +0000</pubDate>
		<dc:creator>Coskun SUNALI</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[extension methods]]></category>

		<guid isPermaLink="false">http://sunali.com/?p=233</guid>
		<description><![CDATA[I want to share some pretty basic and useful UpdatePanel control extensions with you. Method names are self explaining so I will not go deep in details. public static void AlertOnLoad(this UpdatePanel updatePanel, string key, string message) { message = message.Replace("'", "\'").Replace("\r\n", "").Replace("\n", ""); message = "alert('" + message + "');"; AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, message,]]></description>
			<content:encoded><![CDATA[<p>I want to share some pretty basic and useful UpdatePanel control extensions with you. Method names are self explaining so I will not go deep in details.</p>
<pre>public static void AlertOnLoad(this UpdatePanel updatePanel, string key, string message)
{
    message = message.Replace("'", "\'").Replace("\r\n", "").Replace("\n", "");
    message = "alert('" + message + "');";
    AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, message, true);
}

public static void RedirectOnLoad(this UpdatePanel updatePanel, string key, string url)
{
    url = "top.location.href = '" + url + "';";
    AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, url, true);
}

public static void OpenWindowOnLoad(this UpdatePanel updatePanel, string key, string url)
{
    url = "window.open('" + url + "');";
    AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, url, true);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://sunali.com/2008/12/05/some-useful-updatepanel-extensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to generate RSS feeds without any 3rd party components</title>
		<link>http://sunali.com/2008/11/24/how-to-generate-rss-feeds-without-any-3rd-party-components/</link>
		<comments>http://sunali.com/2008/11/24/how-to-generate-rss-feeds-without-any-3rd-party-components/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 17:53:46 +0000</pubDate>
		<dc:creator>Coskun SUNALI</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://sunali.com/?p=215</guid>
		<description><![CDATA[RSS feeds are widely used in today&#8217;s internet world. Thus we, developers, have to implement it on our websites and projects most of the time. There are many free and paid components that generates RSS feeds but in fact you may generate it by yourself as well. I will post a generic handler&#8217;s source code]]></description>
			<content:encoded><![CDATA[<p>RSS feeds are widely used in today&#8217;s internet world. Thus we, developers, have to implement it on our websites and projects most of the time. There are many free and paid components that generates RSS feeds but in fact you may generate it by yourself as well.</p>
<p>I will post a generic handler&#8217;s source code to show you how to do that.</p>
<p><strong>Post</strong> is a class that has some basic properties like Url, Title, Teaser, etc. You may need to write your own class and implement these properties in a basic manner. Then you will need to create a <strong>GetPosts()</strong> method that returns a list of Post objects.</p>
<p><span id="more-215"></span></p>
<p><code></p>
<pre>
&lt;%@ WebHandler Language="C#" Class="Rss" %&gt;

using System;
using System.Web;
using System.Text;
using System.Xml;
using System.Collections.Generic;
using System.Linq;

public class Rss : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.Write("&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;");

        XmlTextWriter xmlWriter = new XmlTextWriter(context.Response.Output);

        xmlWriter.WriteStartElement("rss");
        xmlWriter.WriteAttributeString("version", "2.0");
        xmlWriter.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");

        xmlWriter.WriteStartElement("channel");

        #region Channel Details

        xmlWriter.WriteStartElement("title");
        xmlWriter.WriteCData("Sunali.com");
        xmlWriter.WriteEndElement(); // end title

        xmlWriter.WriteStartElement("link");
        xmlWriter.WriteCData("http://sunali.com");
        xmlWriter.WriteEndElement(); // end link

        xmlWriter.WriteStartElement("description");
        xmlWriter.WriteCData("Sunali.com Posts RSS Feed");
        xmlWriter.WriteEndElement(); // end description

        xmlWriter.WriteStartElement("language");
        xmlWriter.WriteString("tr-TR");
        xmlWriter.WriteEndElement(); // end language

        xmlWriter.WriteStartElement("pubDate");
        xmlWriter.WriteString(DateTime.Now.ToString("ddd, dd MMM yyyy hh:mm:ss zz"));
        xmlWriter.WriteEndElement(); // end pubDate
        #endregion

        // You are supposed to write your GetPosts method by yourself.
        List&lt;Post&gt; posts = GetPosts();

        foreach (Post post in posts)
        {
            xmlWriter.WriteStartElement("item");

            xmlWriter.WriteStartElement("title");
            xmlWriter.WriteCData(post.Title);
            xmlWriter.WriteEndElement(); // end title

            xmlWriter.WriteStartElement("link");
            xmlWriter.WriteCData(post.Url);
            xmlWriter.WriteEndElement(); // end link

            xmlWriter.WriteStartElement("description");
            xmlWriter.WriteCData(post.Teaser);
            xmlWriter.WriteEndElement(); // end description

            xmlWriter.WriteStartElement("dc:creator");
            xmlWriter.WriteCData(post.AuthorName);
            xmlWriter.WriteEndElement(); // end dc:creator

            xmlWriter.WriteStartElement("pubDate");
            xmlWriter.WriteString(post.PublishDate.ToString("ddd, dd MMM yyyy hh:mm:ss zz"));
            xmlWriter.WriteEndElement(); // end pubDate

            xmlWriter.WriteStartElement("guid");
            xmlWriter.WriteAttributeString("isPermaLink", "false");
            xmlWriter.WriteCData(post.Url);
            xmlWriter.WriteEndElement(); // end guid

            xmlWriter.WriteEndElement(); // end item
        }

        xmlWriter.WriteEndElement(); // end channel
        xmlWriter.WriteEndElement(); // end rss
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://sunali.com/2008/11/24/how-to-generate-rss-feeds-without-any-3rd-party-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easiest way of global error handling in an ASP.NET application</title>
		<link>http://sunali.com/2008/11/20/easiest-way-of-global-error-handling-in-an-aspnet-application/</link>
		<comments>http://sunali.com/2008/11/20/easiest-way-of-global-error-handling-in-an-aspnet-application/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 16:44:19 +0000</pubDate>
		<dc:creator>Coskun SUNALI</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[code snippet]]></category>

		<guid isPermaLink="false">http://sunali.com/?p=225</guid>
		<description><![CDATA[Since there are different ways of global error handling in ASP.NET applications and you have to implement the one that best fits into your project&#8217;s architecture, I will not be discussing all the possibilities that ASP.NET provides you. However, I will just share a small code snippet that you can use to catch unhandled exceptions]]></description>
			<content:encoded><![CDATA[<p>Since there are different ways of global error handling in ASP.NET applications and you have to implement the one that best fits into your project&#8217;s architecture, I will not be discussing all the possibilities that ASP.NET provides you. However, I will just share a small code snippet that you can use to catch unhandled exceptions and write a small piece of code to send you an email whenever an error occurs. Just put the following code in your project&#8217;s Global.asax file and it is supposed to work in case you have your SMTP server settings set correctly in the web.config file or you should modify the email sending part depending on your SMTP configuration. Hope it is going to be useful for you.</p>
<p><code></p>
<pre>
void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    if (ex != null)
    {
        if (ex.InnerException != null)
        {
            ex = ex.InnerException;
        }

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("EXCEPTION - BEGIN&lt;br/&gt;&lt;br/&gt;");
        sb.AppendFormat("URL: {0}&lt;br/&gt;", Request.Url);
        sb.AppendFormat("Time: {0}&lt;br/&gt;&lt;br/&gt;", DateTime.Now);
        sb.AppendLine("EXCEPTION DETAILS:&lt;br/&gt;&lt;br/&gt;");
        sb.Append(ex);
        sb.Append("&lt;br/&gt;&lt;br/&gt;EXCEPTION - END");
        sb.Replace("\n", "&lt;br/&gt;");

        try
        {
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

            mail.To.Add("yourname@yourdomain.com");
            mail.Body = sb.ToString();
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Subject = "Exception occured at sunali.com";
            mail.SubjectEncoding = Encoding.UTF8;

            smtp.Send(mail);
        }
        catch { }
    }
}
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://sunali.com/2008/11/20/easiest-way-of-global-error-handling-in-an-aspnet-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Strip all HTML tags from string</title>
		<link>http://sunali.com/2008/11/09/strip-all-html-tags-from-string/</link>
		<comments>http://sunali.com/2008/11/09/strip-all-html-tags-from-string/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 09:15:57 +0000</pubDate>
		<dc:creator>Coskun SUNALI</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[extension methods]]></category>

		<guid isPermaLink="false">http://sunali.com/?p=239</guid>
		<description><![CDATA[This is something we need very often. Either for security reasons or just because of some business rules. I will share a small code snippet with you as an extension method to string type so you can take advantages of using Regular Expressions to remove HTML tags from any user input. public static string StripHtml(this]]></description>
			<content:encoded><![CDATA[<p>This is something we need very often. Either for security reasons or just because of some business rules. I will share a small code snippet with you as an extension method to <em>string</em> type so you can take advantages of using Regular Expressions to remove HTML tags from any user input.<br />
<code> </code></p>
<pre>public static string StripHtml(this string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return string.Empty;
    }

    value = Regex.Replace(value, @"&lt;(.|\n)*?&gt;", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);

    return value;
}</pre>
<p>And this is how you need to use it:</p>
<pre>string var = txUserInput.Text;
var = var.StringHtml();</pre>
]]></content:encoded>
			<wfw:commentRss>http://sunali.com/2008/11/09/strip-all-html-tags-from-string/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to uppercase the first letter of a word or sentence</title>
		<link>http://sunali.com/2008/11/01/how-to-uppercase-the-first-letter-of-a-word-or-sentence/</link>
		<comments>http://sunali.com/2008/11/01/how-to-uppercase-the-first-letter-of-a-word-or-sentence/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 07:41:04 +0000</pubDate>
		<dc:creator>Coskun SUNALI</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.net framework]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[extension methods]]></category>

		<guid isPermaLink="false">http://sunali.com/?p=245</guid>
		<description><![CDATA[When I used to use PHP to develop my web applications, I really loved its basic but useful method named ucfirst. This function is explained using a simple explanation on PHP documentation: ucfirst &#8212; Make a string&#8217;s first character uppercase I believe within all those useful and complex libraries taking place in .NET Framework, it]]></description>
			<content:encoded><![CDATA[<p>When I used to use PHP to develop my web applications, I really loved its basic but useful method named <a href="www.php.net/manual/en/function.ucfirst.php" target="_blank"><em>ucfirst</em></a>. This function is explained using a simple explanation on PHP documentation:</p>
<p class="refpurpose"><strong><span class="refname">ucfirst</span> &#8212; <span class="dc-title">Make a string&#8217;s first character uppercase</span></strong></p>
<p>I believe within all those useful and complex libraries taking place in .NET Framework, it still misses these kind of basic and useful methods. However, the good side is that we can extend it using our custom extension methods.</p>
<p>Because I needed to implement such a feature as PHP offers using <strong>ucfirst</strong> function, I wrote the following simple extension to achieve the same functionality.</p>
<p><code> </code></p>
<pre>/// &lt;summary&gt;
/// Capitalizes the first letter of the word.
/// &lt;/summary&gt;
/// &lt;param name="value"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static string ToUpperFirst(this string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return string.Empty;
    }

    if (value.Length &gt; 1)
    {
        return
            string.Format("{0}{1}",
                value.Substring(0, 1).ToUpper(),
                value.Substring(1).ToLower()
            );
    }
    else
    {
        return value.ToUpper();
    }
}</pre>
<p>And this is how to use it:</p>
<p><code> </code></p>
<pre>string val = "something";
val = val.ToUpperFirst(); // result = Something</pre>
]]></content:encoded>
			<wfw:commentRss>http://sunali.com/2008/11/01/how-to-uppercase-the-first-letter-of-a-word-or-sentence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
