<?xml version="1.0" encoding="UTF-8"?><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>C# &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/c-sharp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:23:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>C# &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>jQuery Datatable With Server Side Data</title>
		<link>https://www.sibeeshpassion.com/jquery-datatable-with-server-side-data-2/</link>
					<comments>https://www.sibeeshpassion.com/jquery-datatable-with-server-side-data-2/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 25 Feb 2016 00:00:43 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Angular]]></category>
		<category><![CDATA[Web API]]></category>
		<category><![CDATA[Angular JS]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Datatable With Server Side]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[jQuery Datatables]]></category>
		<category><![CDATA[MVC]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11266</guid>

					<description><![CDATA[Introduction In this article, we will learn how we can work with jQuery Datatables with server-side data. Here we are going to use an MVC application with jQuery and other required packages installed in it. If you are new to MVC, You can always get the tips/tricks/blogs about that here MVC Tips, Tricks, Blogs. jQuery Datatables is a client-side grid control that is lightweight and easy to use. But when it comes to grid control, it must be usable when it supports the server-side loading of data. This control is perfect for that. I guess, it is enough for the [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Introduction</h2>



<p>In this article, we will learn how we can work with jQuery Datatables with server-side data. Here we are going to use an MVC application with jQuery and other required packages installed in it. If you are new to MVC, You can always get the tips/tricks/blogs about that here MVC Tips, Tricks, Blogs. jQuery Datatables is a client-side grid control that is lightweight and easy to use. But when it comes to grid control, it must be usable when it supports the server-side loading of data. This control is perfect for that. I guess, it is enough for the introduction. Now we will start using our grid. I hope you will like this.</p>



<h2 class="wp-block-heading"><strong>Download the source code</strong></h2>



<p>You can always download the source code here:</p>



<ul class="wp-block-list"><li><a href="https://github.com/SibeeshVenu/jQuery-Datatable-With-Server-Side-Data" target="_blank" rel="noreferrer noopener">Datatable With Server-Side Data</a></li></ul>



<h2 class="wp-block-heading"><strong>Create a MVC application</strong></h2>



<p>Click File-&gt; New-&gt; Project then select MVC application. Before going to start the coding part, make sure that all the required extensions/references are installed. Below are the required things to start with.</p>



<ul class="wp-block-list"><li>Datatables Package</li><li>jQuery</li></ul>



<p>You can all the items mentioned above from NuGet. Right-click on your project name and select Manage NuGet packages.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Manage-NuGet-Package-Window-e1455700665396.png"><img fetchpriority="high" decoding="async" width="650" height="432" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Manage-NuGet-Package-Window-e1455700665396.png" alt="Manage NuGet Package Window" class="wp-image-11235" srcset="/wp-content/uploads/2016/02/Manage-NuGet-Package-Window-e1455700665396.png 650w, /wp-content/uploads/2016/02/Manage-NuGet-Package-Window-e1455700665396-300x199.png 300w, /wp-content/uploads/2016/02/Manage-NuGet-Package-Window-e1455700665396-400x266.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Manage NuGet Package Window</figcaption></figure>



<p>Once you have installed those items, please make sure that all the items(jQuery, Datatables JS files) are loaded in your scripts folder.</p>



<h2 class="wp-block-heading"><strong>Using the code</strong></h2>



<p>Now let us add the needed references.</p>



<h3 class="wp-block-heading">Include the references in your _Layout.cshtml</h3>



<p>As we have already installed all the packages we need, now we need to add the references, right? After adding the reference, your _Layout.cshtml will looks like below.</p>



<script src="https://gist.github.com/SibeeshVenu/4b71315abf732ca3a929e8750cf13638.js"></script>



<p>Here <em>MyScripts.js</em> is the JavaScript file where we are going to write our own scripts.</p>



<p><em>Add a normal MVC controller</em></p>



<p>Now we will add a normal MVC controller in our app. Once you add that you can see an ActionResult is created for us.</p>



<p><code>public ActionResult Index()<br>{<br>return View();<br>}</code></p>



<p>Right-click on the controller, and click add view, that will create a View for you. Now we will change the view as follows. </p>



<script src="https://gist.github.com/SibeeshVenu/87fa8feefb4bd7fcb9fbc867d8a23921.js"></script>



<p>So we have set the headers and footer for our grid, where we are going to load the grid control in the table <em>myGrid</em>. So far the UI part is done, now it is time to set up our database and entity model. Are you ready?</p>



<h3 class="wp-block-heading"><strong>Create a database</strong></h3>



<p>The following query can be used to create a database in your SQL Server.</p>



<script src="https://gist.github.com/SibeeshVenu/5bf2b19e004f619ebfb1f9e6229ef66b.js"></script>



<p>Now we will create a table 🙂</p>



<h3 class="wp-block-heading"><strong>Create table in database</strong></h3>



<p>Below is the query to create table in database.</p>



<script src="https://gist.github.com/SibeeshVenu/8b4797ecfbcfe5841f9ca85a5ef131c8.js"></script>



<h3 class="wp-block-heading"><strong>Insert data to table</strong></h3>



<p>To insert the data, I will attach a database script file along with the download file, you can either run that or insert some data by using the below query. By the way, if you would like to know how to generate scripts with data in SQL Server, you can check <a rel="noopener noreferrer" href="http://sibeeshpassion.com/generate-database-scripts-with-data-in-sql-server/" target="_blank">here</a>.</p>



<script src="https://gist.github.com/SibeeshVenu/9ad0fcd0e2220a4b2a93d7617716416a.js"></script>



<p>Along with this, we can create a new stored procedure that will fetch the data. Following is the query to create the stored procedure.</p>



<script src="https://gist.github.com/SibeeshVenu/40e2e26f6dd93dbac671fc9c33743ccd.js"></script>



<p>Next thing we are going to do is creating a ADO.NET Entity Data Model.</p>



<h3 class="wp-block-heading"><strong>Create Entity Data Model</strong></h3>



<p>Right-click on your model folder and click new, select ADO.NET Entity Data Model. Follow the steps given. Once you have done the processes, you can see the edmx file and other files in your model folder.</p>



<p>Now we will go back to our controller and add a new JsonResult which can be called via a new jQuery Ajax request. No worries, we will create that Ajax requests later. Once you add the Jsonresult action, I hope your controller will look like this.</p>



<script src="https://gist.github.com/SibeeshVenu/344693e90203b925d34b755e83a2ec8a.js"></script>



<p>Here <em>TrialsDBEntities </em>is our entity class. Please be noted that to use the model classes in your controller, you must add the reference as follows.</p>



<p><code>using jQuery_Datatable_With_Server_Side_Data.Models;</code></p>



<p>I know all you are familiar with this, I am just saying!. Now can we create a function <em>GetSales</em> in our model class <em>Sales </em>?.</p>



<script src="https://gist.github.com/SibeeshVenu/b7b4a55890e969fe518cd30cd24433fb.js"></script>



<p>We uses normal LINQ queries here, and we take only 100 records to load for now. If you don&#8217;t want to use this method you can call our stored procedure which we have created while creating our database. You can call this as explained in the below function.</p>



<script src="https://gist.github.com/SibeeshVenu/f189bb513e93566d140429b4a9836834.js"></script>



<p>Now the only thing pending is to call our controller JsonResult action right? We will do some code in our MyScript.js file.</p>



<script src="https://gist.github.com/SibeeshVenu/5093c34f6a455a758426ac8df2b00118.js"></script>



<p>Here <em>&#8220;dataSrc&#8221;: &#8220;&#8221; </em> should be used if you have a plain <a rel="noopener noreferrer" href="http://sibeeshpassion.com/tag/json/" target="_blank">JSON </a>data. The sample data can be find below.</p>



<script src="https://gist.github.com/SibeeshVenu/a2ae9ec5f56a05ef62f6f911e4918ba3.js"></script>



<p>We have done everything!. Can we see the output now?</p>



<h2 class="wp-block-heading"><strong>Output</strong></h2>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data.png"><img decoding="async" width="1024" height="596" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-1024x596.png" alt="jQuery Datatable With Server Side Data" class="wp-image-11267" srcset="/wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-1024x596.png 1024w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-300x175.png 300w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-768x447.png 768w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-400x233.png 400w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-1030x600.png 1030w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data.png 1097w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>jQuery Datatable With Server Side Data</figcaption></figure>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search.png"><img decoding="async" width="1024" height="379" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search-1024x379.png" alt="jQuery Datatable With Server Side Data Search" class="wp-image-11268" srcset="/wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search-1024x379.png 1024w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search-300x111.png 300w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search-768x284.png 768w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search-400x148.png 400w, /wp-content/uploads/2016/02/jQuery-Datatable-With-Server-Side-Data-Search.png 1109w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>jQuery Datatable With Server Side Data Search</figcaption></figure>



<p>Have a happy coding.</p>



<h2 class="wp-block-heading"><strong>Conclusion</strong></h2>



<p>Did I miss anything that you may think which is needed? Did you use jQuery Datatables in your application? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>



<h2 class="wp-block-heading"><strong>Your turn. What do you think?</strong></h2>



<p>A blog isn&#8217;t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>



<p>Kindest Regards<br>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/jquery-datatable-with-server-side-data-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>C sharp Interview Questions And Answers</title>
		<link>https://www.sibeeshpassion.com/c-sharp-interview-questions-and-answers/</link>
					<comments>https://www.sibeeshpassion.com/c-sharp-interview-questions-and-answers/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 24 Oct 2015 08:06:18 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[C sharp Interview Questions And Answers]]></category>
		<category><![CDATA[Career Advice]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Interview Questions For Dot Net]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=10839</guid>

					<description><![CDATA[[toc] Introduction In this article, we will discuss the most asked C# interview questions and answers. If you need to know other interview questions and answers, I strongly recommend to follow this link: Interview Questions. Now in this post, we are going to share the interview questions for C# or a Dot Net developer. No matter you are experienced or fresher, it is important that you must aware of these. So please read it carefully. I hope you will like this article. Background C# is one of my favorite programming languages. So I am really happy to share you some [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>[toc]</p>



<h2 class="wp-block-heading">Introduction</h2>



<p>In this article, we will discuss the most asked C# interview questions and answers. If you need to know other interview questions and answers, I strongly recommend to follow this link: <a href="http://sibeeshpassion.com/category/interview/" target="_blank" rel="noopener noreferrer">Interview Questions</a>. Now in this post, we are going to share the interview questions for C# or a Dot Net developer. No matter you are experienced or fresher, it is important that you must aware of these. So please read it carefully. I hope you will like this article.</p>



<h2 class="wp-block-heading">Background</h2>



<p>C# is one of my favorite programming languages. So I am really happy to share you some interview questions related to C#. You can always read my other interview questions here in the below links.</p>



<ul class="wp-block-list"><li><a rel="noopener noreferrer" href="http://sibeeshpassion.com/dot-net-interview-questions-for-experienced-and-fresher/" target="_blank">Interview Questions For Experienced and Beginner .NET Professionals</a></li><li><a rel="noopener noreferrer" href="http://sibeeshpassion.com/infosys-interview-questions-for-dotnet-professionals/" target="_blank">Infosys Interview Questions For DotNet Professionals</a></li><li><a rel="noopener noreferrer" href="http://sibeeshpassion.com/sql-interview-questions-and-answers/" target="_blank">SQL Interview Questions And Answers</a></li><li><a rel="noopener noreferrer" href="http://sibeeshpassion.com/important-ado-net-interview-questions/" target="_blank">Important ADO.NET Interview Questions</a> <p>So shall we now discuss C# interview questions? </p></li></ul>



<h3 class="wp-block-heading">C# Interview Questions   </h3>



<p>What are Abstract Classes? </p>



<p>The purpose of an abstract class is to define some common behavior that can be inherited by multiple sub classes, without implementing the entire class. In C#, the abstract keyword designates both an abstract class and a pure virtual method. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class. </p>



<h4 class="wp-block-heading">Properties </h4>



<ul class="wp-block-list"><li>An abstract class cannot be instantiated.</li><li>It can be inherited.</li><li>It can have method implementations, class members.</li><li>Only abstract classes can have abstract methods.</li></ul>



<h4 class="wp-block-heading">Syntax</h4>



<script src="https://gist.github.com/SibeeshVenu/2fa66bd9f38b408fe13317d502e08dc1.js"></script>



<h4 class="wp-block-heading">What are Abstract methods?Give an example?</h4>



<h5 class="wp-block-heading">Abstract Methods</h5>



<p>Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. (See above example for syntax)</p>



<p>Derived classes of the abstract class must implement all abstract methods.<br>When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.</p>



<h5 class="wp-block-heading">Example</h5>



<script src="https://gist.github.com/SibeeshVenu/08fb0fe1e57b4c9d8cd46d0caf2bfbe9.js"></script>



<script src="https://gist.github.com/SibeeshVenu/a597cb326601e52f65758c6cffd0047b.js"></script>



<h4 class="wp-block-heading">What is Interface? Explain with an example?</h4>



<p>An interface is useful when you want to be able to use some common functionality of otherwise unrelated classes- they share no implementation details, only the function signatures. In C#, function declarations within an interface are implicitly pure virtual.</p>



<p>An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.</p>



<h5 class="wp-block-heading">Properties</h5>



<p>An interface can be a member of a namespace or a class and can contain signatures of methods, properties, events, indexers.<br>An interface can inherit from one or more base interfaces. A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.</p>



<p><strong>Example</strong><em> </em></p>



<script src="https://gist.github.com/SibeeshVenu/3057b3fcb14f7455c4b039b217876398.js"></script>



<script src="https://gist.github.com/SibeeshVenu/d1815c018ef376231553005f421b0949.js"></script>



<script src="https://gist.github.com/SibeeshVenu/a710e1206ab1aaff58263d1b299c7b7e.js"></script>



<p><em><strong>Calling the metho</strong></em><strong>ds </strong></p>



<script src="https://gist.github.com/SibeeshVenu/e54d38f6d8751c9cf06252657197ab78.js"></script>



<script src="https://gist.github.com/SibeeshVenu/8cec8789031f25287a997b150a7fb6cf.js"></script>



<script src="https://gist.github.com/SibeeshVenu/a415a854c7cb4de11e625650a039327b.js"></script>



<h4 class="wp-block-heading">Explain The Difference Between Abstract Class and Interface ?</h4>



<p>An Abstract class doesn&#8217;t provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface. Using Abstract we cannot achieve multiple inheritances but be using an Interface we can achieve multiple inheritances. We cannot declare a member field in an Interface. We cannot use any access modifier i.e. public, private, protected, internal etc. because within an interface by default everything is public. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.</p>



<h4 class="wp-block-heading">Explain Generic Collections &amp; Array Lists ?</h4>



<p>Generics allow you to delay the specification of the data type of programming elements in a class or a method until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type. Generic Collections helps us to create flexible type-safe, strong type collections at compile time.</p>



<h5 class="wp-block-heading">Syntax</h5>



<script src="https://gist.github.com/SibeeshVenu/12e01a38f7fe93800ed2d9e28af1d248.js"></script>



<h5 class="wp-block-heading">Namespace</h5>



<script src="https://gist.github.com/SibeeshVenu/0f0b6f01abc509c5a0c1ed44aecb95c2.js"></script>



<h5 class="wp-block-heading">ArrayList</h5>



<p>They are ordered a collection of objects, that can be resized automatically, that has dynamic memory allocation and which can contain different types of data. Arraylist stores its members as objects, so in order to retrieve it, we must type cast it.</p>



<h5 class="wp-block-heading">Syntax and Example</h5>



<script src="https://gist.github.com/SibeeshVenu/8533989c2ec614777133e892986e18ae.js"></script>



<h5 class="wp-block-heading">Namespace</h5>



<script src="https://gist.github.com/SibeeshVenu/8f232eefceb895791b4a5fa193d499f2.js"></script>



<h4 class="wp-block-heading">What are Finalize and Dispose? Can you list down the differences between them?</h4>



<p>Finalizers are run by the Garbage Collection before an object that is eligible for collection is reclaimed. Dispose() is meant for cleaning up unmanaged resources, like network connections, files, handles to OS stuff, &amp;c. It works best in conjunction with the using block where the compiler makes sure that Dispose() will be called immediately once you are done with an object – and also ensures that you cannot work with the object anymore once it&#8217;s disposed of.</p>



<h5 class="wp-block-heading">Dispose() Method</h5>



<ul class="wp-block-list"><li>This dispose method will be used to free unmanaged resources like files, database connection etc.</li><li>To clear unmanaged resources, we need to write code manually to raise dispose() method.</li><li>This Dispose() method belongs to the IDisposable interface.</li><li>If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.</li><li>It will not show any effect on the performance of the website and we can use this method whenever we want to free objects immediately.</li></ul>



<h5 class="wp-block-heading">Finalize() Method</h5>



<ul class="wp-block-list"><li>This method also free unmanaged resources like database connections, files etc…</li><li>It is automatically raised by garbage collection mechanism whenever the object goes out of scope.</li><li>This method belongs to object class.</li><li>We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when garbage collection process was done.</li><li>It will show the effect on the performance of the website and it will not suitable to free objects immediately.</li></ul>



<h4 class="wp-block-heading">What is Dependency Injection?How can we implement it?</h4>



<p>Simply put Dependency injection is for decoupling two components. It can be explained by a simple example.</p>



<script src="https://gist.github.com/SibeeshVenu/6f2681e284b50352ed9e2457ef182515.js"></script>



<script src="https://gist.github.com/SibeeshVenu/22b96305a7e43336e28a3109883fce47.js"></script>



<p>Have a look at the code above.</p>



<p>I have an ErrorLogger class that writes an error into database. The method is actually called from another class ApplicationWatcher.<br>In a later stage, if I want to send an email instead of writing into the database, this design will not suffice. I will have created another class that has a method that sends an email and creates its instance in the application watches.</p>



<h4 class="wp-block-heading">What is Data Encapsulation and explain its Implementation?</h4>



<p>To know about the Data encapsulation in C#, I recommend you to read here:<br><a href="http://www.tutorialspoint.com/csharp/csharp_encapsulation.htm" target="_blank" rel="noopener noreferrer">Data Encapsulation and Its Implementation </a><br>That&#8217;s all. Have a great day.</p>



<h3 class="wp-block-heading">Conclusion</h3>



<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>



<h3 class="wp-block-heading">Your turn. What do you think?</h3>



<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>



<p>Kindest Regards<br>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/c-sharp-interview-questions-and-answers/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Using Spire.PDF In Asp.Net</title>
		<link>https://www.sibeeshpassion.com/using-spire-pdf-in-asp-net/</link>
					<comments>https://www.sibeeshpassion.com/using-spire-pdf-in-asp-net/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 24 Aug 2015 16:41:58 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Products]]></category>
		<category><![CDATA[Spire.PDF]]></category>
		<category><![CDATA[Adding a watermark To PDF]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[Convert PDF Page to Image]]></category>
		<category><![CDATA[Decryption of PDF File]]></category>
		<category><![CDATA[Encrypting PDF File]]></category>
		<category><![CDATA[Using Spire]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=10081</guid>

					<description><![CDATA[In this article we are going to see a new product Spire.PDF which helps us to create, manipulate PDF and many more. This product has been introduced by the company E-Iceblue. The company has introduced so many products like this. For example Spire.Doc, Spire.XLS. I hope you have read my articles under these categories. If you have not read them, I suggest you to rad here. Using Spire.Doc Using Spire.Xls Working With Charts Using Spire.Xls Download source code Using Spire PDF Background As you all know a PDF is a most accurate and effective format of a document. A document [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this article we are going to see a new product <em>Spire.PDF</em> which helps us to create, manipulate PDF and many more. This product has been introduced by the company <a href="http://www.e-iceblue.com/" target="_blank" rel="noopener">E-Iceblue</a>. The company has introduced so many products like this. For example Spire.Doc, Spire.XLS. I hope you have read my articles under these categories. If you have not read them, I suggest you to rad here.</p>
<li><a href="http://sibeeshpassion.com/using-spire-doc-introduction/" target="_blank" rel="noopener">Using Spire.Doc</a></li>
<li><a href="http://sibeeshpassion.com/using-spire-xls/" target="_blank" rel="noopener">Using Spire.Xls</a></li>
<li><a href="http://sibeeshpassion.com/working-with-charts-using-spire-xls/" target="_blank" rel="noopener">Working With Charts Using Spire.Xls</a></li>
<p><strong>Download source code </strong></p>
<p><a href="http://sibeeshpassion.com/download/UsingSpirePDF.rar" target="_blank" rel="noopener">Using Spire PDF</a></p>
<p><strong>Background</strong></p>
<p>As you all know a PDF is a most accurate and effective format of a document. A document is very important in our life, so we create PDF files. Now coming to the matter, As we all are developers, we develops anything that is needed. But have you ever tried creating and managing PDF file? Awww!. That&#8217;s a quite difficult task right? Still it is possible. &#8220;Everything is possible, the word impossible itself says I am possible&#8221;. Now It is time to introduce you a product which let you do these task in an easy manner. So that means, you are able to do these task with a little effort. </p>
<p><strong>Create a new Project</strong></p>
<p>Now we will create a new Project in our Visual studio.</p>
<p><div id="attachment_10091" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Create_New_Project-e1440348497731.png"><img decoding="async" aria-describedby="caption-attachment-10091" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Create_New_Project-e1440348497731.png" alt="Using_Spire_PDF_Create_New_Project" width="650" height="397" class="size-full wp-image-10091" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Create_New_Project-e1440348497731.png 650w, /wp-content/uploads/2015/08/Using_Spire_PDF_Create_New_Project-e1440348497731-300x183.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Create_New_Project-e1440348497731-400x244.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-10091" class="wp-caption-text">Using_Spire_PDF_Create_New_Project</p></div></p>
<p><strong>Add License Information To Project</strong></p>
<p>I am using evaluation version with one month temporary license. There are free versions also available for spire.pdf with some limitation. You can try that. </p>
<p><div id="attachment_10101" style="width: 664px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Add_License_To_Project.png"><img decoding="async" aria-describedby="caption-attachment-10101" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Add_License_To_Project.png" alt="Using_Spire_PDF_Add_License_To_Project" width="654" height="150" class="size-full wp-image-10101" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Add_License_To_Project.png 654w, /wp-content/uploads/2015/08/Using_Spire_PDF_Add_License_To_Project-300x69.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Add_License_To_Project-400x92.png 400w" sizes="(max-width: 654px) 100vw, 654px" /></a><p id="caption-attachment-10101" class="wp-caption-text">Using_Spire_PDF_Add_License_To_Project</p></div></p>
<p><strong>Download the files</strong></p>
<p>You can always the needed files from here: <a href="http://www.e-iceblue.com/Download/download-pdf-for-net-now.html" target="_blank" rel="noopener">Download Spire.PDF</a></p>
<p><strong>Install Spire.PDF</strong></p>
<p>Now click on the exe file after you extract the downloaded file. The installation will get started then.</p>
<p><div id="attachment_10121" style="width: 519px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Installing.png"><img decoding="async" aria-describedby="caption-attachment-10121" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Installing.png" alt="Using_Spire_PDF_Installing" width="509" height="399" class="size-full wp-image-10121" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Installing.png 509w, /wp-content/uploads/2015/08/Using_Spire_PDF_Installing-300x235.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Installing-400x314.png 400w" sizes="(max-width: 509px) 100vw, 509px" /></a><p id="caption-attachment-10121" class="wp-caption-text">Using_Spire_PDF_Installing</p></div></p>
<p><div id="attachment_10131" style="width: 519px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Installing_Complete.png"><img decoding="async" aria-describedby="caption-attachment-10131" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Installing_Complete.png" alt="Using_Spire_PDF_Installing_Complete" width="509" height="399" class="size-full wp-image-10131" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Installing_Complete.png 509w, /wp-content/uploads/2015/08/Using_Spire_PDF_Installing_Complete-300x235.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Installing_Complete-400x314.png 400w" sizes="(max-width: 509px) 100vw, 509px" /></a><p id="caption-attachment-10131" class="wp-caption-text">Using_Spire_PDF_Installing_Complete</p></div></p>
<p>So Shall we start?</p>
<p>Once you Installed, you are ready to go. We will start with a “Simple Web Application” . I hope you have a created a new web application and added your license as suggested before. Now create a page.</p>
<p>So your page will be looking as follows.</p>
<p>[html]<br />
&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;UsingSpirePDF.Default&quot; %&gt;</p>
<p>&lt;!DOCTYPE html&gt;</p>
<p>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
&lt;head runat=&quot;server&quot;&gt;<br />
    &lt;title&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br />
    &lt;div&gt;</p>
<p>    &lt;/div&gt;<br />
    &lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>Now right click on your project and click add reference, in the browse tab find out the folder in which you have installed spire.pdf. Usually it will be in the C:\Program Files\e-iceblue\Spire.pdf. Now just find your framework version from BIN folder and add Spire.pdf.dll</p>
<p><div id="attachment_10241" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Reference-e1440429536607.png"><img decoding="async" aria-describedby="caption-attachment-10241" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Reference-e1440429536607.png" alt="Using_Spire_PDF_Adding_Reference" width="650" height="447" class="size-full wp-image-10241" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Reference-e1440429536607.png 650w, /wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Reference-e1440429536607-300x206.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Reference-e1440429536607-160x110.png 160w, /wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Reference-e1440429536607-400x275.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-10241" class="wp-caption-text">Using_Spire_PDF_Adding_Reference</p></div></p>
<p>Now we have added reference too. So shall we start coding ?</p>
<p><strong>Using the code</strong></p>
<p>There are so many features available, we will look in to most useful features which you might found useful.</p>
<p><strong>Convert PDF Page to Image</strong></p>
<p>In this part, we will see how can we convert a PDF file to image with a specific resolution. It is so simple and powerful.</p>
<p>We will create a button as follows.</p>
<p>[html]<br />
&lt;asp:Button ID=&quot;btnConvertToImage&quot; runat=&quot;server&quot; Text=&quot;Convert To Image&quot; /&gt;<br />
[/html]</p>
<p>Next we need to add the needed references.</p>
<p>[csharp]<br />
using Spire.Pdf;<br />
using Spire.Pdf.Graphics;<br />
using System;<br />
using System.Drawing;<br />
[/csharp]</p>
<p>And in the button click we can write the preceding codes.</p>
<p>[csharp]<br />
 protected void btnConvertToImage_Click(object sender, EventArgs e)<br />
        {<br />
            try<br />
            {<br />
                PdfDocument documemt = new PdfDocument();<br />
                documemt.LoadFromFile(@&quot;D:\\sibeeshpassion.pdf&quot;);<br />
                Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, 400, 400);<br />
                image.Save(@&quot;D:\\result.jpg&quot;);<br />
                documemt.Close();<br />
            }<br />
            catch (Exception)<br />
            {</p>
<p>                throw;<br />
            }<br />
        }<br />
[/csharp]</p>
<p>As you can see we are calling the below function to generate the image.</p>
<p>[csharp]<br />
documemt.SaveAsImage(0, PdfImageType.Bitmap, 400, 400);<br />
[/csharp]</p>
<p>And this function has overloaded as in the below image.</p>
<p><div id="attachment_10251" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image.png"><img decoding="async" aria-describedby="caption-attachment-10251" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image-e1440431907162.png" alt="Using_Spire_PDF_Covert_PDF_To_Image" width="650" height="97" class="size-full wp-image-10251" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image-e1440431907162.png 650w, /wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image-e1440431907162-300x45.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image-e1440431907162-400x60.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-10251" class="wp-caption-text">Using_Spire_PDF_Covert_PDF_To_Image</p></div></p>
<p>Here we are taking the pdf file <em><a href="http://sibeeshpassion.com/download/sibeeshpassion.pdf" target="_blank" rel="noopener">sibeeshpassion.pdf</a></em> and we are getting a image as follows as output. </p>
<p><div id="attachment_10261" style="width: 520px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image_Output.png"><img decoding="async" aria-describedby="caption-attachment-10261" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image_Output.png" alt="Using_Spire_PDF_Covert_PDF_To_Image_Output" width="510" height="724" class="size-full wp-image-10261" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image_Output.png 251w, /wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image_Output-211x300.png 211w, /wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image_Output-400x568.png 400w, /wp-content/uploads/2015/08/Using_Spire_PDF_Covert_PDF_To_Image_Output-423x600.png 423w" sizes="(max-width: 510px) 100vw, 510px" /></a><p id="caption-attachment-10261" class="wp-caption-text">Using_Spire_PDF_Covert_PDF_To_Image_Output</p></div></p>
<p>Cool Right?</p>
<p>Now we will see another implementation.</p>
<p><strong>Security</strong></p>
<p>There are few many security options are also available with this package like Encryption and Decryption of the pdf file and creating a digital signature and many more.</p>
<p><strong>Encrypting PDF File</strong></p>
<p>In this step, we are going to set a password for our pdf document.</p>
<p>To work with security, you need to add a new reference as follows.</p>
<p>[csharp]<br />
using Spire.Pdf.Security;<br />
[/csharp]</p>
<p>Now add a new button as follows.</p>
<p>[html]<br />
&lt;asp:Button ID=&quot;btnEncryptPDF&quot; runat=&quot;server&quot; Text=&quot;Encrypt PDF&quot; OnClick=&quot;btnEncryptPDF_Click&quot;/&gt;<br />
[/html]</p>
<p>Now it is time for click event.</p>
<p>[csharp]<br />
protected void btnEncryptPDF_Click(object sender, EventArgs e)<br />
        {<br />
            try<br />
            {<br />
                PdfDocument doc = new PdfDocument(@&quot;D:\\sibeeshpassion.pdf&quot;, &quot;sibeeshpassionEncrypted&quot;);<br />
                doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;<br />
                doc.Security.OwnerPassword = &quot;sibeeshpassion&quot;;<br />
                doc.Security.UserPassword = &quot;sibeesh&quot;;<br />
                doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;<br />
            }<br />
            catch (Exception)<br />
            {</p>
<p>                throw;<br />
            }<br />
        }<br />
[/csharp]</p>
<p>Three kind of pdf key size are available as shown in the below image.</p>
<p><div id="attachment_10271" style="width: 663px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Possible_Key.png"><img decoding="async" aria-describedby="caption-attachment-10271" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Possible_Key.png" alt="Using_Spire_PDF_Possible_Key" width="653" height="108" class="size-full wp-image-10271" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Possible_Key.png 653w, /wp-content/uploads/2015/08/Using_Spire_PDF_Possible_Key-300x50.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Possible_Key-400x66.png 400w" sizes="(max-width: 653px) 100vw, 653px" /></a><p id="caption-attachment-10271" class="wp-caption-text">Using_Spire_PDF_Possible_Key</p></div></p>
<p>And as you can see, we are setting some permission by using the preceding code.</p>
<p>[csharp]<br />
 doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;<br />
[/csharp]</p>
<p>There are some other permission options also available.</p>
<p><div id="attachment_10281" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Possible_File_Permissions-e1440433409566.png"><img decoding="async" aria-describedby="caption-attachment-10281" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Possible_File_Permissions-e1440433409566.png" alt="Using_Spire_PDF_Possible_File_Permissions" width="650" height="244" class="size-full wp-image-10281" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Possible_File_Permissions-e1440433409566.png 650w, /wp-content/uploads/2015/08/Using_Spire_PDF_Possible_File_Permissions-e1440433409566-300x113.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Possible_File_Permissions-e1440433409566-400x150.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-10281" class="wp-caption-text">Using_Spire_PDF_Possible_File_Permissions</p></div></p>
<p>Now if you try to open our pdf again, it will ask you for a password which we have set in the above codes. Cool.</p>
<p><strong>Decryption of PDF File</strong></p>
<p>AS we encrypt our pdf file, we need to do decryption also right?</p>
<p>Add an another button.</p>
<p>[html]<br />
&lt;asp:Button ID=&quot;btnDecrypt&quot; runat=&quot;server&quot; Text=&quot;Decrypt PDF&quot; OnClick=&quot;btnDecrypt_Click&quot; /&gt;<br />
[/html] </p>
<p>Add the following code block in the button click event.</p>
<p>[csharp]<br />
try<br />
            {<br />
                PdfDocument doc = new PdfDocument(@&quot;D:\\sibeeshpassion.pdf&quot;, &quot;sibeeshpassionEncrypted&quot;);<br />
                //extract image<br />
                Image image = doc.Pages[0].ImagesInfo[0].Image;<br />
                doc.Close();<br />
                //Save image file.<br />
                image.Save(&quot;sibeeshpassionDecrypted.png&quot;, System.Drawing.Imaging.ImageFormat.Png);<br />
                //Launching the image file.<br />
                System.Diagnostics.Process.Start(&quot;sibeeshpassionDecrypted.png&quot;);</p>
<p>            }<br />
            catch (Exception ex)<br />
            {</p>
<p>                throw;<br />
            }<br />
[/csharp]</p>
<p>Once you run the above codes, your pdf file will be decrypted.</p>
<p><strong>Watermark</strong></p>
<p>You can add watermark to your pdf file easily with this package. </p>
<p><strong>Adding a watermark</strong></p>
<p>There are two kind of watermarks. One is text watermark and other is image watermark.</p>
<p>Now we will add another button.</p>
<p>[html]<br />
&lt;asp:Button ID=&quot;btnAddImageWatermark&quot; runat=&quot;server&quot; Text=&quot;Add Image Watermark&quot; OnClick=&quot;btnAddImageWatermark_Click&quot;  /&gt;<br />
[/html]</p>
<p>And in the button click we can write the preceding codes.</p>
<p>[csharp]<br />
protected void btnAddImageWatermark_Click(object sender, EventArgs e)<br />
        {<br />
            try<br />
            {<br />
                PdfDocument doc = new PdfDocument(&quot;D:\\Sibi.pdf&quot;, &quot;Sibi&quot;);<br />
                Image img = Image.FromFile(&quot;D:\\sibi.jpg&quot;);<br />
                doc.Pages[0].BackgroundImage = img;<br />
                doc.SaveToFile(&quot;D:\\Sibeesh.pdf&quot;);<br />
                doc.Close();</p>
<p>            }<br />
            catch (Exception)<br />
            {</p>
<p>                throw;<br />
            }<br />
        }<br />
[/csharp]</p>
<p>As you can see we are taking a pdf file and add a watermark image to the file and at last we save it to an another file. In this case from sibi.pdf to sibeesh.pdf.</p>
<p>I am taking <a href="http://sibeeshpassion.com/content/images/sibi.jpg" target="_blank" rel="noopener">sibi.jpg</a> as the water mark image, so you can see the pdf file with watermarked image as follows.</p>
<p><div id="attachment_10311" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Watermark-e1440437182738.png"><img decoding="async" aria-describedby="caption-attachment-10311" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Watermark-e1440437182738.png" alt="Using_Spire_PDF_Adding_Watermark" width="650" height="601" class="size-full wp-image-10311" srcset="/wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Watermark-e1440437182738.png 650w, /wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Watermark-e1440437182738-300x277.png 300w, /wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Watermark-e1440437182738-400x370.png 400w, /wp-content/uploads/2015/08/Using_Spire_PDF_Adding_Watermark-e1440437182738-649x600.png 649w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-10311" class="wp-caption-text">Using_Spire_PDF_Adding_Watermark</p></div></p>
<p><em>Please be noted that, you can convert your PDF file to any other file format, there are plenty of options available. Please try that too. I have given only few options which I use always.</em></p>
<p><strong>Conclusion</strong></p>
<p>These are the features I loved using Spire.PDF. It is much effective and simple. What do you think about this? Are you using Spire.PDF yet? Do you plan to try this?Did I miss anything that you may think which is needed?. I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<p><strong>Your turn. What do you think?</strong></p>
<p>A blog isn&#8217;t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you&#8217;re better off posting it on Stack Overflow instead of commenting here. Tweet or email me a link to your question there and I&#8217;ll definitely try to help if I am able to.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/using-spire-pdf-in-asp-net/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Find Occurrence Of A String</title>
		<link>https://www.sibeeshpassion.com/find-occurrence-of-a-string/</link>
					<comments>https://www.sibeeshpassion.com/find-occurrence-of-a-string/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 05 Aug 2015 00:53:48 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[C-Sharp Functions]]></category>
		<category><![CDATA[Find Occurrence of String]]></category>
		<category><![CDATA[Find String Form String]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[String Contains Substring]]></category>
		<category><![CDATA[Substring]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=7571</guid>

					<description><![CDATA[In this article you will learn the ways to find the occurrence of a string from a string, or find a sub string from a string. We are using C# language to do this demo. In times, you may have faced this requirement. And I am sure you would do that. This article is for the one who don&#8217;t know how to achieve this. We will be discussing two methods here. I hope you will like it. See demo Occurrence Of A String Demo Background Today I have got a requirement of creating a function which performs some operations according [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this article you will learn the ways to find the occurrence of a string from a string, or find a sub string from a string. We are using C# language to do this demo. In times, you may have faced this requirement. And I am sure you would do that. This article is for the one who don&#8217;t know how to achieve this. We will be discussing two methods here. I hope you will like it.</p>
<p><span style="color: #ff6600;"><strong>See demo</strong></span></p>
<p><a href="http://sibeeshpassion.com/demo/OccurrenceOfString/" target="_blank" rel="noopener">Occurrence Of A String Demo</a></p>
<p><span style="color: #ff6600;"><strong>Background</strong></span></p>
<p>Today I have got a requirement of creating a function which performs some operations according to the occurrence of a string in a given string. Like, if a string pattern contains more that one time in a given string, it must do some actions and if it is just one time it should do other actions. I have done this in two ways, here I am sharing you that. I hope some one may find it is useful.</p>
<p><span style="color: #ff6600;"><strong>Using the code</strong></span></p>
<p>We are going to discuss the following two ways to achieve this requirement.</p>
<li>Using Regex Class</li>
<li>Using Custom Function</li>
<p>We will start with a Regex class first.</p>
<p>To start with Regex, you need to add another namespace as given below.</p>
<p>[csharp]<br />
using System.Text.RegularExpressions;<br />
[/csharp]</p>
<p>Now consider we have designed our page as follows.</p>
<p>[html]<br />
&lt;div&gt;<br />
            &lt;table &gt;<br />
                &lt;tr&gt;<br />
                    &lt;td&gt;<br />
                        &lt;asp:Label ID=&quot;lblInputString&quot; runat=&quot;server&quot; Text=&quot;Input String&quot;&gt;&lt;/asp:Label&gt;<br />
                    &lt;/td&gt;<br />
                    &lt;td&gt;<br />
                        &lt;textarea id=&quot;txtInputString&quot; runat=&quot;server&quot;&gt;&lt;/textarea&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
                &lt;tr&gt;<br />
                    &lt;td&gt;<br />
                        &lt;asp:Label ID=&quot;lblPattern&quot; runat=&quot;server&quot; Text=&quot;Pattern String&quot;&gt;&lt;/asp:Label&gt;<br />
                    &lt;/td&gt;<br />
                    &lt;td&gt;<br />
                        &lt;textarea id=&quot;txtPattern&quot; runat=&quot;server&quot;&gt;&lt;/textarea&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
                &lt;tr&gt;<br />
                    &lt;td colspan=&quot;2&quot;&gt;<br />
                        &lt;asp:Button ID=&quot;btnCheckOccurance&quot; runat=&quot;server&quot; Text=&quot;Check Occurance&quot; OnClick=&quot;btnCheckOccurance_Click&quot; /&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
                &lt;tr&gt;<br />
                    &lt;td colspan=&quot;2&quot;&gt;<br />
                        &lt;asp:Label ID=&quot;lblOutput&quot; runat=&quot;server&quot; Text=&quot;See Output Here!!! &lt;i&gt; Thank you for visiting. Please Visit Again!!! &lt;/i&gt;&quot;&gt;&lt;/asp:Label&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
            &lt;/table&gt;<br />
        &lt;/div&gt;<br />
[/html]</p>
<p>And Some CSS styles as follows.</p>
<p>[css]<br />
&lt;style type=&quot;text/css&quot;&gt;<br />
        textarea {<br />
            width: 630px;<br />
            height: 100px;<br />
        }</p>
<p>        table {<br />
            border: 1px solid #ccc;<br />
            padding: 10px;<br />
            width: 800px;<br />
            text-align:center;<br />
        }</p>
<p>        tr {<br />
            border: 1px solid #999;<br />
            border-radius: 5px;<br />
        }</p>
<p>        td {<br />
            border: 1px solid #999;<br />
            padding: 10px;<br />
            border-radius: 5px;<br />
        }<br />
    &lt;/style&gt;<br />
[/css]</p>
<p>If you add the above codes and CSS, your page may looks like in the preceding image.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_1.png" alt="" /></p>
<p>Now we will see our C# code. Please add the below lines of codes in the button click event.</p>
<p>[csharp]<br />
protected void btnCheckOccurance_Click(object sender, EventArgs e)<br />
    {<br />
        int occuranceCount = 0;</p>
<p>        //Checking Occurance<br />
        occuranceCount = Regex.Matches(txtInputString.Value.ToLower().Trim(), txtPattern.Value.ToLower().Trim()).Count;<br />
        if (occuranceCount &gt; 0)<br />
            lblOutput.Text = &quot;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Sibeesh Passion &lt;/b&gt;Says &lt;br/&gt;&lt;br/&gt;The given string pattern &lt;i&gt;&quot; + txtPattern.Value + &quot;&lt;/i&gt; Contains &quot; + occuranceCount + &quot; times in  &lt;i&gt;&quot;<br />
                + txtInputString.Value + &quot;&lt;/i&gt;&lt;br/&gt;&lt;br/&gt; Thank you for visiting. Please Visit Again!!!&quot;;<br />
        else<br />
            lblOutput.Text = &quot;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Sibeesh Passion &lt;/b&gt;Says &lt;br/&gt;&lt;br/&gt;The given string pattern &lt;i&gt;&quot; + txtPattern.Value + &quot;&lt;/i&gt; Is not occured s in  &lt;i&gt;&quot;<br />
                + txtInputString.Value + &quot;&lt;/i&gt;&lt;br/&gt;&lt;br/&gt; Thank you for visiting. Please Visit Again!!!&quot;;<br />
        //End<br />
    }<br />
[/csharp]</p>
<p>In the above code, we are finding the occurrence of a string by using a <em>Regex.Matches</em> function. The <em>Regex.Matches</em> function expects two parameters. </p>
<li>Input String (In which string we need to search for a string)</li>
<li>Pattern String(What we need to search in a string)</li>
<p>We are getting those two values from our texarea and pass as follows.</p>
<p>[csharp]<br />
Regex.Matches(txtInputString.Value.ToLower().Trim(), txtPattern.Value.ToLower().Trim()).Count;<br />
[/csharp]</p>
<p>Now if you run application, you will get output as follows.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex-300x185.png" alt="Find_Occurrence_Of_A_String_Using_Regex" width="300" height="185" class="alignnone size-medium wp-image-7611" srcset="/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex-300x185.png 300w, /wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex-768x474.png 768w, /wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex-400x247.png 400w, /wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex.png 813w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex_2-e1437475061191.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Regex_2-300x183.png" alt="Find_Occurrence_Of_A_String_Using_Regex_2" width="300" height="183" class="alignnone size-medium wp-image-7621" /></a></p>
<p>As you can see I have given the words, &#8220;Sibeesh&#8221; and &#8220;passion&#8221; for testing, and it gave the correct number of occurrence right?</p>
<p>Now we will see how we can do this by Using a Custom Function.</p>
<p>Following in our function call.</p>
<p>[csharp]<br />
occuranceCount = FindOccurrences(txtInputString.Value.ToLower().Trim(), txtPattern.Value.ToLower().Trim());<br />
[/csharp]</p>
<p>And please find the function body below.</p>
<p>[csharp]<br />
 #region FindOccurrences<br />
    /// &lt;summary&gt;<br />
    /// This method is used to get the Count of Occurrences of a string in a string<br />
    /// &lt;/summary&gt;<br />
    /// &lt;param name=&quot;InputString&quot;&gt;&lt;/param&gt;<br />
    /// &lt;param name=&quot;patternString&quot;&gt;&lt;/param&gt;<br />
    /// &lt;returns&gt;Int&lt;/returns&gt;<br />
    public int FindOccurrences(string InputString, string patternString)<br />
    {<br />
        // Here we are looping through InputString<br />
        int intCount = 0;<br />
        int i = 0;<br />
        while ((i = InputString.IndexOf(patternString, i)) != -1)<br />
        {<br />
            i += patternString.Length;<br />
            intCount++;<br />
        }<br />
        return intCount;<br />
    }<br />
    #endregion<br />
[/csharp]</p>
<p>Now if your run you will get the same output as we got by using a Regex function.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Custom_Function-e1437475554507.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Custom_Function-300x184.png" alt="Find_Occurrence_Of_A_String_Using_Custom_Function" width="300" height="184" class="alignnone size-medium wp-image-7631" /></a></p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Custom_Function_2-e1437475574384.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/07/Find_Occurrence_Of_A_String_Using_Custom_Function_2-300x184.png" alt="Find_Occurrence_Of_A_String_Using_Custom_Function_2" width="300" height="184" class="alignnone size-medium wp-image-7641" /></a></p>
<p>Now we will see our complete codes.</p>
<p><strong>Complete Code</strong></p>
<p><em>Default.aspx.cs</em></p>
<p>[csharp]<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text.RegularExpressions;<br />
using System.Web;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;</p>
<p>public partial class _Default : System.Web.UI.Page<br />
{<br />
    protected void Page_Load(object sender, EventArgs e)<br />
    {</p>
<p>    }<br />
    protected void btnCheckOccurance_Click(object sender, EventArgs e)<br />
    {<br />
        int occuranceCount = 0;</p>
<p>        //Checking Occurance<br />
        //occuranceCount = Regex.Matches(txtInputString.Value.ToLower().Trim(), txtPattern.Value.ToLower().Trim()).Count;<br />
        occuranceCount = FindOccurrences(txtInputString.Value.ToLower().Trim(), txtPattern.Value.ToLower().Trim());<br />
        if (occuranceCount &gt; 0)<br />
            lblOutput.Text = &quot;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Sibeesh Passion &lt;/b&gt;Says &lt;br/&gt;&lt;br/&gt;The given string pattern &lt;i&gt;&quot; + txtPattern.Value + &quot;&lt;/i&gt; Contains &quot; + occuranceCount + &quot; times in  &lt;i&gt;&quot;<br />
                + txtInputString.Value + &quot;&lt;/i&gt;&lt;br/&gt;&lt;br/&gt; Thank you for visiting. Please Visit Again!!!&quot;;<br />
        else<br />
            lblOutput.Text = &quot;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Sibeesh Passion &lt;/b&gt;Says &lt;br/&gt;&lt;br/&gt;The given string pattern &lt;i&gt;&quot; + txtPattern.Value + &quot;&lt;/i&gt; Is not occured s in  &lt;i&gt;&quot;<br />
                + txtInputString.Value + &quot;&lt;/i&gt;&lt;br/&gt;&lt;br/&gt; Thank you for visiting. Please Visit Again!!!&quot;;<br />
        //End<br />
    }<br />
    #region FindOccurrences<br />
    /// &lt;summary&gt;<br />
    /// This method is used to get the Count of Occurrences of a string in a string<br />
    /// &lt;/summary&gt;<br />
    /// &lt;param name=&quot;InputString&quot;&gt;&lt;/param&gt;<br />
    /// &lt;param name=&quot;patternString&quot;&gt;&lt;/param&gt;<br />
    /// &lt;returns&gt;Int&lt;/returns&gt;<br />
    public int FindOccurrences(string InputString, string patternString)<br />
    {<br />
        // Here we are looping through InputString<br />
        int intCount = 0;<br />
        int i = 0;<br />
        while ((i = InputString.IndexOf(patternString, i)) != -1)<br />
        {<br />
            i += patternString.Length;<br />
            intCount++;<br />
        }<br />
        return intCount;<br />
    }<br />
    #endregion<br />
}<br />
[/csharp]</p>
<p><em>Default.aspx</em></p>
<p>[html]<br />
&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Default.aspx.cs&quot; Inherits=&quot;_Default&quot; %&gt;</p>
<p>&lt;!DOCTYPE html&gt;</p>
<p>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
&lt;head runat=&quot;server&quot;&gt;<br />
    &lt;title&gt;Find Occurrence of String,Find String Form String,Regex,String Contains Substring &#8211; Sibeesh Passion&lt;/title&gt;<br />
    &lt;style type=&quot;text/css&quot;&gt;<br />
        textarea {<br />
            width: 630px;<br />
            height: 100px;<br />
        }</p>
<p>        table {<br />
            border: 1px solid #ccc;<br />
            padding: 10px;<br />
            width: 800px;<br />
            text-align:center;<br />
        }</p>
<p>        tr {<br />
            border: 1px solid #999;<br />
            border-radius: 5px;<br />
        }</p>
<p>        td {<br />
            border: 1px solid #999;<br />
            padding: 10px;<br />
            border-radius: 5px;<br />
        }<br />
    &lt;/style&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br />
        &lt;div&gt;<br />
            &lt;table &gt;<br />
                &lt;tr&gt;<br />
                    &lt;td&gt;<br />
                        &lt;asp:Label ID=&quot;lblInputString&quot; runat=&quot;server&quot; Text=&quot;Input String&quot;&gt;&lt;/asp:Label&gt;<br />
                    &lt;/td&gt;<br />
                    &lt;td&gt;<br />
                        &lt;textarea id=&quot;txtInputString&quot; runat=&quot;server&quot;&gt;&lt;/textarea&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
                &lt;tr&gt;<br />
                    &lt;td&gt;<br />
                        &lt;asp:Label ID=&quot;lblPattern&quot; runat=&quot;server&quot; Text=&quot;Pattern String&quot;&gt;&lt;/asp:Label&gt;<br />
                    &lt;/td&gt;<br />
                    &lt;td&gt;<br />
                        &lt;textarea id=&quot;txtPattern&quot; runat=&quot;server&quot;&gt;&lt;/textarea&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
                &lt;tr&gt;<br />
                    &lt;td colspan=&quot;2&quot;&gt;<br />
                        &lt;asp:Button ID=&quot;btnCheckOccurance&quot; runat=&quot;server&quot; Text=&quot;Check Occurance&quot; OnClick=&quot;btnCheckOccurance_Click&quot; /&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
                &lt;tr&gt;<br />
                    &lt;td colspan=&quot;2&quot;&gt;<br />
                        &lt;asp:Label ID=&quot;lblOutput&quot; runat=&quot;server&quot; Text=&quot;See Output Here!!! &lt;i&gt; Thank you for visiting. Please Visit Again!!! &lt;/i&gt;&quot;&gt;&lt;/asp:Label&gt;<br />
                    &lt;/td&gt;<br />
                &lt;/tr&gt;<br />
            &lt;/table&gt;<br />
        &lt;/div&gt;<br />
    &lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p><strong>Validation</strong></p>
<p>Now if you want you can add some basic validation to your code in server side and client side. I will always recommend you to do both validation.</p>
<p><em>Server Side</em><br />
[csharp]<br />
if (txtInputString.Value.ToLower().Trim() != &quot;&quot; &amp;&amp; txtPattern.Value.ToLower().Trim() != &quot;&quot;)<br />
        {<br />
}<br />
[/csharp]</p>
<p>Place the button click event code inside of this if condition.</p>
<p><em>Client Side</em></p>
<p><em>Add a Jquery reference </em></p>
<p>[js]<br />
&lt;script src=&quot;jquery-2.0.2.min.js&quot;&gt;&lt;/script&gt;<br />
[/js]</p>
<p>And add the below scripts.</p>
<p>[js]<br />
&lt;script&gt;<br />
        $(document).ready(function () {<br />
            $(&#8216;#btnCheckOccurance&#8217;).click(function () {<br />
                if ($(&#8216;#txtInputString&#8217;).val() == &quot;&quot; || $(&#8216;#txtPattern&#8217;).val() == &quot;&quot;) {<br />
                    alert(&#8216;Values can not be empty&#8217;);<br />
                    return false;<br />
                }<br />
            });<br />
        });<br />
    &lt;/script&gt;<br />
[/js]</p>
<p><span style="color: #ff6600;"><strong>Conclusion</strong></span></p>
<p>I hope someone found this article useful. Please share me your valuable thoughts and comments. Your feedback is always welcomed.</p>
<p>Thanks in advance. Happy coding!</p>
<p>Kindest Regards<br />
<a href="https://plus.google.com/+sibeeshkv" target="_blank" rel="noopener">Sibeesh Venu</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/find-occurrence-of-a-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>CellSetGrid Implementation in VS 2012 or later</title>
		<link>https://www.sibeeshpassion.com/cellsetgrid-implementation-in-vs-2012-or-later/</link>
					<comments>https://www.sibeeshpassion.com/cellsetgrid-implementation-in-vs-2012-or-later/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Tue, 14 Jul 2015 12:03:22 +0000</pubDate>
				<category><![CDATA[Q&A]]></category>
		<category><![CDATA[ADOMD]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CellSetGrid]]></category>
		<category><![CDATA[sql server analysis services 2005 or later required]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=7011</guid>

					<description><![CDATA[Hi All, I have downloaded the CellSetGrid source code and tried to implement the grid with my cube details. I am trying to use the same in VS 2013. When I run I am getting error as &#8220;sql server analysis services 2005 or later required &#8221; even if I have already Installed latest version of SSAS. Please help me to resolve this. Thanks in advance. Kindest Regards Sibeesh Venu www.sibeeshpassion.com]]></description>
										<content:encoded><![CDATA[<p>Hi All, </p>
<p>I have downloaded the CellSetGrid source code and tried to implement the grid with my cube details. I am trying to use the same in VS 2013. When I run I am getting error as &#8220;sql server analysis services 2005 or later required &#8221; even if I have already Installed latest version of SSAS. Please help me to resolve this. Thanks in advance.</p>
<p>Kindest Regards<br />
Sibeesh Venu<br />
<a href="http://www.sibeeshpassion.com" target="_blank">www.sibeeshpassion.com</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/cellsetgrid-implementation-in-vs-2012-or-later/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Named and Optional Arguments In C#</title>
		<link>https://www.sibeeshpassion.com/named-and-optional-arguments-in-csharp/</link>
					<comments>https://www.sibeeshpassion.com/named-and-optional-arguments-in-csharp/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 02 Jul 2015 11:14:28 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# Arguments]]></category>
		<category><![CDATA[C# Parameters]]></category>
		<category><![CDATA[C# programming]]></category>
		<category><![CDATA[Named Arguments]]></category>
		<category><![CDATA[Named Parameters]]></category>
		<category><![CDATA[Optional Argument]]></category>
		<category><![CDATA[Using Named Argument]]></category>
		<category><![CDATA[Using Optional Arguments]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=5451</guid>

					<description><![CDATA[Introduction In this article we will learn about two things that we must aware of being a programmer. Named Arguments Optional Arguments Both of this has been introduced with Visual Studio 2010. Now we will go ahead in detail about those. I hope you will like it. Download Source Code NamedOptional.rar Background I am working in a project in which we are using Visual Studio 2012 and C# as the programming language, We do have so many functions in our logical layers. In those functions we used both Named and Optional Arguments. So I thought of sharing this with you. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><span style="color: #ff6600;"><strong>Introduction</strong></span></p>
<p>In this article we will learn about two things that we must aware of being a programmer.</p>
<ul>
<li>Named Arguments</li>
<li>Optional Arguments</li>
</ul>
<p>Both of this has been introduced with Visual Studio 2010. Now we will go ahead in detail about those. I hope you will like it.</p>
<p><span style="color: #ff6600;"><strong>Download Source Code</strong></span></p>
<p><a href="http://sibeeshpassion.com/download/NamedOptional.rar" target="_blank" rel="noopener">NamedOptional.rar</a></p>
<p><span style="color: #ff6600;"><strong>Background</strong></span></p>
<p>I am working in a project in which we are using Visual Studio 2012 and C# as the programming language, We do have so many functions in our logical layers. In those functions we used both Named and Optional Arguments. So I thought of sharing this with you. Please be noted that this article is for the one who have not tried these Named and Optional arguments yet.</p>
<p>Before going to the coding part, we will learn what named and optional argument is? What are all the features of these two?</p>
<p><span style="color: #ff6600;"><strong>Named Arguments</strong></span></p>
<p><em><strong>Ref: MSDN </strong></em><em> Named arguments enable you to specify an argument for a particular parameter by associating the argument with the parameter&#8217;s name rather than with the parameter&#8217;s position in the parameter list.</em></p>
<p>As said by MSDN, A named argument ,</p>
<li>Enables you to pass the argument to the function by associating the parameter&#8217;s name </li>
<li>No needs for remembering the parameters position that we are not aware of always.</li>
<li>No need to look the order of the parameters in the parameters list of called function.</li>
<li>We can specify parameter for each arguments by its name</li>
<p>No we will describe all about a named argument with a simple program. I hope we all know how to find out the area of a rectangle. Yes you are right it is A= wl (Where w is the width and l is length and A is area.) So we will be using this formula in our function. Consider following is our function call.</p>
<p>[csharp]<br />
FindArea(120, 56);<br />
[/csharp]</p>
<p>In this our first argument is length (ie 120) and second argument is width (ie 56). And we are calculating the area by that function. And following is the function definition.</p>
<p>[csharp]</p>
<p> private static double FindArea(int length, int width)<br />
        {<br />
            try<br />
            {<br />
                return (length* width);<br />
            }<br />
            catch (Exception)<br />
            {<br />
                throw new NotImplementedException();<br />
            }<br />
        }</p>
<p>[/csharp]</p>
<p>So in the first function call, we just passed the arguments by its position. Right?<br />
[csharp]<br />
            double area;<br />
            Console.WriteLine(&quot;Area with positioned argument is: &quot;);<br />
            area = FindArea(120, 56);<br />
            Console.WriteLine(area);<br />
            Console.Read();<br />
[/csharp]</p>
<p>If you run this, you will get an output as follows.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage1.png" alt="" /></p>
<p>Now here it comes the features of a named arguments. Please see the preceding function call.</p>
<p>[csharp]<br />
            Console.WriteLine(&quot;Area with Named argument is: &quot;);<br />
            area = FindArea(length: 120, width: 56);<br />
            Console.WriteLine(area);<br />
            Console.Read();<br />
[/csharp]</p>
<p>Here we are giving the named arguments in the method call.</p>
<p>[csharp]<br />
area = FindArea(length: 120, width: 56);<br />
[/csharp]</p>
<p>Now if you run this program, you will get the same result. Please see the below image.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage2.png" alt="" /></p>
<p>As I said above, we can give the names vice versa in the method call if we are using the named arguments right? Please see the preceding method call.</p>
<p>[csharp]<br />
            Console.WriteLine(&quot;Area with Named argument vice versa is: &quot;);<br />
            area = FindArea(width: 120, length: 56);<br />
            Console.WriteLine(area);<br />
            Console.Read();<br />
[/csharp]</p>
<p>Please run the program and see the output as below.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage3.png" alt="" /></p>
<p>You get the same result right? I hope you said yes.</p>
<p>One of the important use of a named argument is, when you use this in your program it improves the readability of your code. It simply says what your argument is meant to be, or what it is?.</p>
<p>Now you can give the positional arguments too. That means, a combination of both positional argument and named argument. So shall we try that?</p>
<p>[csharp]<br />
Console.WriteLine(&quot;Area with Named argument Positional Argument : &quot;);<br />
            area = FindArea(120, width: 56);<br />
            Console.WriteLine(area);<br />
            Console.Read();<br />
[/csharp]</p>
<p>In the above example we passed 120 as the length and 56 as a named argument for the parameter width.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage4.png" alt="" /></p>
<p>I hope you enjoyed using named arguments, there are some limitations too. We will discuss the limitation of a named arguments now.</p>
<p><span style="color: #ff6600;"><strong>Limitation of using a Named Argument</strong></span></p>
<p><strong>Named argument specification must appear after all fixed arguments have been specified</strong>.</p>
<p>If you use a named argument before a fixed argument you will get a compile time error as follows.<br />
<em>Named argument specification must appear after all fixed arguments have been specified</em></p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage5.png" alt="" /></p>
<p><span style="color: #ff6600;"><strong>Optional Arguments</strong></span></p>
<p><strong>Ref</strong>: MSDN <em>The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters.</em></p>
<p>As said by MSDN, a Optional Argument,</p>
<li>We can omit the argument in the call if that argument is an Optional Argument</li>
<li>Every Optional Argument has its own default value</li>
<li>It will take default value if we do not supply the value</li>
<li>A default value of a Optional Argument must be a </li>
<p>              1. Constant expression.<br />
              2. Must be a value type such as enum or struct.<br />
              3. Must be an expression of the form default(valueType)</p>
<li>It must be set at the end of parameter list</li>
<p>Now consider preceding is our function definition with optional arguments.<br />
[csharp]<br />
 private static double FindAreaWithOptional(int length, int width=56)<br />
        {<br />
            try<br />
            {<br />
                return (length * width);<br />
            }<br />
            catch (Exception)<br />
            {<br />
                throw new NotImplementedException();<br />
            }<br />
        }<br />
[/csharp]</p>
<p>Here we have set the value for width as optional and gave value as 56. right? Now we will try to call this function.</p>
<p>If you note, the IntelliSense itself shows you the optional argument as shown in the below image.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage6.png" alt="" /></p>
<p>Now if you call the function as shown in the preceding code block. The function will be fired and give you the same output.</p>
<p>[csharp]<br />
            Console.WriteLine(&quot;Area with Optional Argument : &quot;);<br />
            area = FindAreaWithOptional(120);<br />
            Console.WriteLine(area);<br />
            Console.Read();<br />
[/csharp]</p>
<p>Note that we did not get any error while compiling and it will give you an output as follows.</p>
<p><img decoding="async" src="http://sibeeshpassion.com/content/images/NamedOptionalArgumentImage7.png" alt="" /></p>
<p><span style="color: #ff6600;"><strong>Conclusion</strong></span></p>
<p>I hope you will like this article. Please share me your valuable thoughts and comments. Your feedback is always welcomed.</p>
<p>Thanks in advance. Happy coding!</p>
<p>Kindest Regards<br />
<a href="https://plus.google.com/+sibeeshkv" target="_blank" rel="noopener">Sibeesh Venu</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/named-and-optional-arguments-in-csharp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to replace number from a string in C#</title>
		<link>https://www.sibeeshpassion.com/how-to-replace-number-from-a-string-in-csharp/</link>
					<comments>https://www.sibeeshpassion.com/how-to-replace-number-from-a-string-in-csharp/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 24 Jun 2015 09:32:10 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[C# functions]]></category>
		<category><![CDATA[C# Regex]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[find number]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Replace number]]></category>
		<category><![CDATA[replace number from string]]></category>
		<category><![CDATA[separate number and string]]></category>
		<category><![CDATA[Take number from string]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=5401</guid>

					<description><![CDATA[In this pose we will see how to replace a number from a string in C#. We can do this in several ways. Here I am going to show you one. I hope you will like this. Consider following is my string. [csharp] String myString = &#34;MyString1&#34;; [/csharp] And I need to take out the number 1 from the value MyString1. What will we do? To do this we can use Regex class in C#. Following is the code to achieve the same 🙂 [csharp] myString = Regex.Replace(sortdatafield, @&#34;[\d-]&#34;, string.Empty); [/csharp] Once you done and Run, you will get the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this pose we will see how to replace a number from a string in C#. We can do this in several ways. Here I am going to show you one. I hope you will like this.</p>
<p>Consider following is my string.</p>
<p>[csharp]<br />
String myString = &quot;MyString1&quot;;<br />
[/csharp]</p>
<p>And I need to take out the number <em>1 </em>from the value <em>MyString1</em>. What will we do?</p>
<p>To do this we can use Regex class in C#. Following is the code to achieve the same 🙂</p>
<p>[csharp]<br />
myString = Regex.Replace(sortdatafield, @&quot;[\d-]&quot;, string.Empty);<br />
[/csharp]</p>
<p>Once you done and Run, you will get the new value  without numbers (<em>&#8220;MyString&#8221;</em>) in the variable <em>myString</em></p>
<p>I hope someone found this useful. Happy Coding 🙂</p>
<p>Kindly see my code snippets <a href="http://sibeeshpassion.com/category/code-snippets/" target="_blank" rel="noopener">here</a></p>
<p>Kindest Regards<br />
<a href="https://plus.google.com/+sibeeshkv" target="_blank" rel="noopener">Sibeesh Venu</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/how-to-replace-number-from-a-string-in-csharp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to take numbers from a string in C#</title>
		<link>https://www.sibeeshpassion.com/how-to-take-numbers-from-a-string-in-csharp/</link>
					<comments>https://www.sibeeshpassion.com/how-to-take-numbers-from-a-string-in-csharp/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 24 Jun 2015 09:24:03 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[C# functions]]></category>
		<category><![CDATA[C# Regex]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[find number]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[separate number and string]]></category>
		<category><![CDATA[Take number from string]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=5381</guid>

					<description><![CDATA[Consider following is my string. [csharp] String myString = &#34;MyString1&#34;; [/csharp] And I need to take out the number 1 from the value MyString1. What will we do? To do this we can use Regex class in C#. Following is the code to achieve the same 🙂 [csharp] myString = Regex.Match(myString , @&#34;\d+&#34;).Value; [/csharp] Once you done and Run, you will get value 1 in the variable myString I hope someone found this useful. Happy Coding 🙂 Kindest Regards Sibeesh Venu]]></description>
										<content:encoded><![CDATA[<p>Consider following is my string.<br />
[csharp]<br />
String myString = &quot;MyString1&quot;;<br />
[/csharp]</p>
<p>And I need to take out the number <em>1 </em>from the value <em>MyString1</em>. What will we do?</p>
<p>To do this we can use Regex class in C#. Following is the code to achieve the same 🙂</p>
<p>[csharp]<br />
myString = Regex.Match(myString , @&quot;\d+&quot;).Value;<br />
[/csharp]</p>
<p>Once you done and Run, you will get value <em>1</em> in the variable <em>myString </em></p>
<p>I hope someone found this useful. Happy Coding 🙂</p>
<p>Kindest Regards<br />
<a href="https://plus.google.com/+sibeeshkv" target="_blank" rel="noopener">Sibeesh Venu</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/how-to-take-numbers-from-a-string-in-csharp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Determine Which Browser Your Application is Running In</title>
		<link>https://www.sibeeshpassion.com/determine-which-browser-your-application-is-running-in/</link>
					<comments>https://www.sibeeshpassion.com/determine-which-browser-your-application-is-running-in/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 17 Jun 2015 01:00:38 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JQuery]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=2201</guid>

					<description><![CDATA[Introduction Today we will learn how to determine in which browser your application is currently running. I hope you will like it. Background As you all know, nowadays it is important that our application must work in all the browsers. We cannot force our clients to work with only selected browsers. It is our duty to make our application work with all the browsers. Most of the applications may encounterr some issues with browser compatibility. I encountered some issues too. To make our application work with all the browsers, you must get the browser details at run time, only then [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>Today we will learn how to determine in which browser your application is currently running. I hope you will like it.</p>
<p><strong>Background</strong></p>
<p>As you all know, nowadays it is important that our application must work in all the browsers. We cannot force our clients to work with only selected browsers. It is our duty to make our application work with all the browsers. Most of the applications may encounterr some issues with browser compatibility. I encountered some issues too. To make our application work with all the browsers, you must get the browser details at run time, only then we can write the relevant code depending on the browser. Here we will do that.</p>
<p><strong>Using the code</strong></p>
<p>Here I am using Visual Studio 2012. What I will do is to create a default page and when the page runs, we will fetch the browser details from the server.</p>
<p><strong>So let us start</strong></p>
<p>Here I will share two methods to do that.</p>
<p><em><strong>Method 1</strong></em></p>
<p>In this method, we will use a page with the following codes. This method does not need any server-side code.<br />
[html]<br />
&lt;%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“Default.aspx.cs” Inherits=“_Default” %&gt;<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html xmlns=“http://www.w3.org/1999/xhtml”&gt;<br />
&lt;head runat=“server”&gt;<br />
    &lt;title&gt;Check in which browser your are running your applciation – SibeesPassion&lt;/title&gt;<br />
    &lt;style&gt;<br />
        tr {<br />
            border: 1px solid #ccc;<br />
            padding: 5px;<br />
            text-align: center;<br />
        }<br />
    &lt;/style&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;form id=“form1″ runat=“server”&gt;<br />
        &lt;div&gt;<br />
            &lt;%= Request.Browser.Browser %&gt;<br />
        &lt;/div&gt;<br />
    &lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>We are fetching a variable Request.Browse. The browser that is generated by ASP.Net at run time, which means, when we run our application the call reaches the server and the server will generate the variable that contains the details about the browser.</p>
<p>If you run the page in any browser you will get the output as follows.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-out-in-which-browser-your-application-is-running/Images/chkbrw1.PNG" alt="" /></p>
<p>Figure 1: Google Chrome</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-out-in-which-browser-your-application-is-running/Images/chkbrw2.PNG" alt="" /></p>
<p>Figure 2: Internet Explorer</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-out-in-which-browser-your-application-is-running/Images/chkbrw3.PNG" alt="" /></p>
<p>Figure 3: Firefox (Mozilla)</p>
<p>Now will go to our second method.</p>
<p><em><strong>Method 2</strong></em></p>
<p>In this method we are fetching the browser details by writing some server-side code (C# and VB.Net). For this, please see the following codes.</p>
<p><strong>C#</strong><br />
[csharp]</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
using System.Web.Configuration;<br />
public partial class _Default : System.Web.UI.Page<br />
{<br />
    protected void Page_Load(object sender, EventArgs e)<br />
    {<br />
        HttpBrowserCapabilities myBrowser = Request.Browser;<br />
        Response.Write(“&lt;p&gt;Browser Capabilities:&lt;/p&gt;”);<br />
        Response.Write(“Type = “ + myBrowser.Type + “&lt;br&gt;”);<br />
        Response.Write(“Name = “ + myBrowser.Browser + “&lt;br&gt;”);<br />
        Response.Write(“Version = “ + myBrowser.Version + “&lt;br&gt;”);<br />
        Response.Write(“Major Version = “ + myBrowser.MajorVersion + “&lt;br&gt;”);<br />
        Response.Write(“Minor Version = “ + myBrowser.MinorVersion + “&lt;br&gt;”);<br />
        Response.Write(“Platform = “ + myBrowser.Platform + “&lt;br&gt;”);<br />
        Response.Write(“Is Beta = “ + myBrowser.Beta + “&lt;br&gt;”);<br />
        Response.Write(“Is Crawler = “ + myBrowser.Crawler + “&lt;br&gt;”);<br />
        Response.Write(“Is AOL = “ + myBrowser.AOL + “&lt;br&gt;”);<br />
        Response.Write(“Is Win16 = “ + myBrowser.Win16 + “&lt;br&gt;”);<br />
        Response.Write(“Is Win32 = “ + myBrowser.Win32 + “&lt;br&gt;”);<br />
        Response.Write(“Supports Frames = “ + myBrowser.Frames + “&lt;br&gt;”);<br />
        Response.Write(“Supports Tables = “ + myBrowser.Tables + “&lt;br&gt;”);<br />
        Response.Write(“Supports Cookies = “ + myBrowser.Cookies + “&lt;br&gt;”);<br />
        Response.Write(“Supports VB Script = “ + myBrowser.VBScript + “&lt;br&gt;”);<br />
        Response.Write(“Supports JavaScript = “ + myBrowser.JavaScript + “&lt;br&gt;”);<br />
        Response.Write(“Supports Java Applets = “ + myBrowser.JavaApplets + “&lt;br&gt;”);<br />
        Response.Write(“Supports ActiveX Controls = “ + myBrowser.ActiveXControls + “&lt;br&gt;”);<br />
        Response.Write(“CDF = “ + myBrowser.CDF + “&lt;br&gt;”);<br />
    }<br />
}<br />
[/csharp]</p>
<p><strong>VB.Net</strong></p>
<p>[vb]<br />
Imports System.Collections.Generic<br />
Imports System.Linq<br />
Imports System.Web<br />
Imports System.Web.UI<br />
Imports System.Web.UI.WebControls<br />
Imports System.Web.Configuration<br />
Public Partial Class _Default<br />
    Inherits System.Web.UI.Page<br />
    Protected Sub Page_Load(sender As Object, e As EventArgs)<br />
        Dim myBrowser As HttpBrowserCapabilities = Request.Browser<br />
        Response.Write(“&lt;p&gt;Browser Capabilities:&lt;/p&gt;”)<br />
        Response.Write(“Type = “ + myBrowser.Type + “&lt;br&gt;”)<br />
        Response.Write(“Name = “ + myBrowser.Browser + “&lt;br&gt;”)<br />
        Response.Write(“Version = “ + myBrowser.Version + “&lt;br&gt;”)<br />
        Response.Write(“Major Version = “ + myBrowser.MajorVersion + “&lt;br&gt;”)<br />
        Response.Write(“Minor Version = “ + myBrowser.MinorVersion + “&lt;br&gt;”)<br />
        Response.Write(“Platform = “ + myBrowser.Platform + “&lt;br&gt;”)<br />
        Response.Write(“Is Beta = “ + myBrowser.Beta + “&lt;br&gt;”)<br />
        Response.Write(“Is Crawler = “ + myBrowser.Crawler + “&lt;br&gt;”)<br />
        Response.Write(“Is AOL = “ + myBrowser.AOL + “&lt;br&gt;”)<br />
        Response.Write(“Is Win16 = “ + myBrowser.Win16 + “&lt;br&gt;”)<br />
        Response.Write(“Is Win32 = “ + myBrowser.Win32 + “&lt;br&gt;”)<br />
        Response.Write(“Supports Frames = “ + myBrowser.Frames + “&lt;br&gt;”)<br />
        Response.Write(“Supports Tables = “ + myBrowser.Tables + “&lt;br&gt;”)<br />
        Response.Write(“Supports Cookies = “ + myBrowser.Cookies + “&lt;br&gt;”)<br />
        Response.Write(“Supports VB Script = “ + myBrowser.VBScript + “&lt;br&gt;”)<br />
        Response.Write(“Supports JavaScript = “ + myBrowser.JavaScript + “&lt;br&gt;”)<br />
        Response.Write(“Supports Java Applets = “ + myBrowser.JavaApplets + “&lt;br&gt;”)<br />
        Response.Write(“Supports ActiveX Controls = “ + myBrowser.ActiveXControls + “&lt;br&gt;”)<br />
        Response.Write(“CDF = “ + myBrowser.CDF + “&lt;br&gt;”)<br />
    End Sub<br />
End Class<br />
[/vb]</p>
<p>What we do on the preceding code is, we are creating an object for HttpBrowserCapabilities and fetching the details from that object. Very simple. Now if you run your page, you will get the output as in the following.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-out-in-which-browser-your-application-is-running/Images/chkbrw4.PNG" alt="" /></p>
<p>Figure 4: Chrome.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-out-in-which-browser-your-application-is-running/Images/chkbrw5.PNG" alt="" /></p>
<p>Figure 5: Internet Explorer</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-out-in-which-browser-your-application-is-running/Images/chkbrw6.PNG" alt="" /></p>
<p>Figure 6: Firefox (Mozilla)</p>
<p><strong>Conclusion</strong></p>
<p>I hope you will like this article. Please share me your valuable thoughts and comments. Your feedback is always welcomed.</p>
<p>Thanks in advance. Happy coding!</p>
<p>Kindest Regards<br />
<a href="https://plus.google.com/+sibeeshkv" target="_blank" rel="noopener">Sibeesh Venu</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/determine-which-browser-your-application-is-running-in/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Uploading and Downloading in MVC Step-by-Step</title>
		<link>https://www.sibeeshpassion.com/uploading-and-downloading-in-mvc-step-by-step/</link>
					<comments>https://www.sibeeshpassion.com/uploading-and-downloading-in-mvc-step-by-step/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sun, 14 Jun 2015 01:00:52 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Download in MVC]]></category>
		<category><![CDATA[File Downloading Using MVC]]></category>
		<category><![CDATA[File Uploading Using MVC]]></category>
		<category><![CDATA[Model View Controller]]></category>
		<category><![CDATA[MVC Download]]></category>
		<category><![CDATA[MVC Upload]]></category>
		<category><![CDATA[Upload in MVC]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=1931</guid>

					<description><![CDATA[Introduction I hope you all are fine. Today we will learn how to perform upload and download operations in MVC. Please refer to the step-by-step approach in learning Model View Controller if you are new to MVC. Our MVC Master, Shivprasad koirala has explained the concepts in a perfect way. Download the source code Uploading and Downloading in MVC Step-by-Step Background Some days earlier, I got a requirement to develop a upload and download mechanism in my MVC application. After completing it perfectly, I decided to share it with you all. Using the code Before moving further into the details, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>I hope you all are fine. Today we will learn how to perform upload and download operations in MVC. Please refer to the <a href="http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in" target="_blank" rel="noopener">step-by-step approach</a> in learning Model View Controller if you are new to MVC. Our MVC Master, Shivprasad koirala has explained the concepts in a perfect way.</p>
<p><strong>Download the source code</strong></p>
<li><a href="https://code.msdn.microsoft.com/Uploading-and-Downloading-7b5277ef" target="_blank" rel="noopener">Uploading and Downloading in MVC Step-by-Step</a></li>
<p><strong>Background </strong></p>
<p>Some days earlier, I got a requirement to develop a upload and download mechanism in my MVC application. After completing it perfectly, I decided to share it with you all.</p>
<p><strong>Using the code</strong></p>
<p>Before moving further into the details, let us first list the key points we will explain in this article:</p>
<li>Create a MVC application.</li>
<li>Create a controller.</li>
<li>Create View depending on the controller.</li>
<li>Change the RouteConfig as needed.</li>
<li>Create ActionResult for the actions.</li>
<li>Create a folder where we need to save the downloaded files.</li>
<p>Let us start with creating a controller called “myAction”.<br />
[csharp]<br />
using System;<br />
using System.Collections.Generic;<br />
using System.IO;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.Mvc;<br />
namespace UploadDownloadInMVC.Controllers<br />
{<br />
    public class myActionController : Controller<br />
    {<br />
        //<br />
        // GET: /myAction/<br />
    }<br />
}<br />
[/csharp]</p>
<p>As you can see that the controller is empty; we will be writing the action results next.<br />
[csharp]<br />
public ActionResult Index()<br />
       {<br />
           foreach (string upload in Request.Files)<br />
           {<br />
               if (Request.Files[upload].FileName != “”)<br />
               {<br />
                   string path = AppDomain.CurrentDomain.BaseDirectory + “/App_Data/uploads/”;<br />
                   string filename = Path.GetFileName(Request.Files[upload].FileName);<br />
                   Request.Files[upload].SaveAs(Path.Combine(path, filename));<br />
               }<br />
           }<br />
           return View(“Upload”);<br />
       }<br />
[/csharp]</p>
<p>The action result shown above is for index. So, whenever the application loads, the action result will be fired. For that, the following changes should be done for RouteCofig.<br />
[csharp]<br />
public class RouteConfig<br />
    {<br />
        public static void RegisterRoutes(RouteCollection routes)<br />
        {<br />
            routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);<br />
            routes.MapRoute(<br />
                name: “Default”,<br />
                url: “{controller}/{action}/{id}”,<br />
                defaults: new { controller = “myAction”, action = “Index”, id = UrlParameter.Optional }<br />
            );<br />
        }<br />
    }<br />
[/csharp]</p>
<p>As you can see in the Index Action,  we are checking whether the request parameter contains the files. If it exists, we will save the selected file to the directory</p>
<p><em>/App_Data/uploads/</em> (that we need to manually create in our application). After returning to the view Upload, we need to set the Upload view.</p>
<p><strong>Upload View</strong></p>
<p>The following is the code for the Upload view.<br />
[html]<br />
@{<br />
    ViewBag.Title = “Upload”;<br />
}<br />
&lt;h2&gt;Upload&lt;/h2&gt;<br />
&lt;script src=“~/Scripts/jquery-1.11.1.min.js”&gt;&lt;/script&gt;<br />
&lt;script&gt;<br />
    $(document).ready(function () {<br />
        $(‘#btnUploadFile’).on(‘click’, function () {<br />
            var data = new FormData();<br />
            var files = $(“#fileUpload”).get(0).files;<br />
            // Add the uploaded image content to the form data collection<br />
            if (files.length &gt; 0) {<br />
                data.append(“UploadedImage”, files[0]);<br />
            }<br />
            // Make Ajax request with the contentType = false, and procesDate = false<br />
            var ajaxRequest = $.ajax({<br />
                type: “POST”,<br />
                url: “myAction/Index”,<br />
                contentType: false,<br />
                processData: false,<br />
                data: data<br />
            });<br />
            ajaxRequest.done(function (xhr, textStatus) {<br />
                // Do other operation<br />
            });<br />
        });<br />
    });<br />
&lt;/script&gt;<br />
&lt;input type=“file” name=“FileUpload1″ id=“fileUpload” /&gt;&lt;br /&gt;<br />
&lt;input id=“btnUploadFile” type=“button” value=“Upload File” /&gt;<br />
@Html.ActionLink(“Documents”, “Downloads”)<br />
[/html]</p>
<p>In the upload view, we have the following:</p>
<li>File uploader</li>
<li>Upload button</li>
<li>Ajax call to the controller ( myAction/Index)</li>
<p>Here, we are adding the uploaded image content to the form data collection.<br />
[csharp]<br />
var data = new FormData();<br />
           var files = $(“#fileUpload”).get(0).files;<br />
           // Add the uploaded image content to the form data collection<br />
           if (files.length &gt; 0) {<br />
               data.append(“UploadedImage”, files[0]);<br />
           }<br />
[/csharp]</p>
<p>Once the data is added to the form data collection, we will pass the data to the controller via ajax call. Sounds cool, right? If the procedure goes well, we will see the output as follows.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/uploading-and-downloading-in-mvc/Images/upload1.PNG" alt="" /></p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/uploading-and-downloading-in-mvc/Images/upload2.PNG" alt="" /></p>
<p>When you choose the file and click upload, your selected file will be uploaded to the folder “uploads” as we have set it in the controller.</p>
<p>We have finished the process of uploading files. We will now move to the downloading section. This is the right time to add the remaining actions to our controller. The following is the code.<br />
[csharp]<br />
public ActionResult Downloads()<br />
        {<br />
            var dir = new System.IO.DirectoryInfo(Server.MapPath(“~/App_Data/uploads/”));<br />
            System.IO.FileInfo[] fileNames = dir.GetFiles(“*.*”); List&lt;string&gt; items = new List&lt;string&gt;();<br />
            foreach (var file in fileNames)<br />
            {<br />
                items.Add(file.Name);<br />
            }<br />
            return View(items);<br />
        }<br />
        public FileResult Download(string ImageName)<br />
        {<br />
            var FileVirtualPath = “~/App_Data/uploads/” + ImageName;<br />
            return File(FileVirtualPath, “application/force-download”, Path.GetFileName(FileVirtualPath));<br />
        }<br />
[/csharp]</p>
<p>Do you remember that we have set an action link in the “Upload” View?<br />
[html]<br />
@Html.ActionLink(“Documents”, “Downloads”)<br />
[/html]</p>
<p>Next, if we click on the “Documents” link, our Action Result Downloads will be fired, right? Now, the following code will explain what is happening here.<br />
[csharp]<br />
var dir = new System.IO.DirectoryInfo(Server.MapPath(“~/App_Data/uploads/”));<br />
            System.IO.FileInfo[] fileNames = dir.GetFiles(“*.*”); List&lt;string&gt; items = new List&lt;string&gt;();<br />
            foreach (var file in fileNames)<br />
            {<br />
                items.Add(file.Name);<br />
            }<br />
            return View(items);<br />
[/csharp]</p>
<p>We are considering all the attached files, adding the file information to a list and returning this list to the view “Download”. Next, here’s the need to set another view. The following code is for the Download View.</p>
<p><strong>Download View</strong><br />
[html]<br />
@{<br />
    ViewBag.Title = “Downloads”;<br />
}<br />
&lt;h2&gt;Downloads&lt;/h2&gt;<br />
@model List&lt;string&gt;<br />
&lt;h2&gt;Downloads&lt;/h2&gt;<br />
&lt;table&gt;<br />
    &lt;tr&gt;<br />
        &lt;th&gt;File Name&lt;/th&gt;<br />
        &lt;th&gt;Link&lt;/th&gt;<br />
    &lt;/tr&gt;<br />
    @for (var i = 0; i &lt;= Model.Count – 1; i++)<br />
    {<br />
        &lt;tr&gt;<br />
            &lt;td&gt;@Model[i].ToString() &lt;/td&gt;<br />
            &lt;td&gt;@Html.ActionLink(“Download”, “Download”, new { ImageName = @Model[i].ToString() }) &lt;/td&gt;<br />
        &lt;/tr&gt;<br />
    }<br />
&lt;/table&gt;<br />
[/html]</p>
<p>Here, we are taking the information (that we are sending from the controller) about the uploaded files and creating the Html.ActionLinks dynamically.</p>
<p>Please note that we are adding the Image name to the  action. Here is the output after performing the operations.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/uploading-and-downloading-in-mvc/Images/downloadWithframe.png" alt="" /></p>
<p>As in the preceding image, when you mouse over the link, it will show the image name along with the controller URL. Click on the link to download the file. So simple, right?</p>
<p><strong>Conclusion</strong></p>
<p>I hope you liked the article. Please provide your valuable feedback; it matters a lot. You can download the source code to determine more.</p>
<p><strong>Point of interest</strong></p>
<p>MVC, MVC Upload, MVC Download, File Uploading Using MVC, File Downloading Using MVC</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/uploading-and-downloading-in-mvc-step-by-step/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
