Skip to main content

Posts

Showing posts with the label C#

Convert your datatable into generic poco object in c# using linq, ado and reflections.

Follow @harshit_parshii The most common problem that we face these days is to create a common class and method that can be used across all the projects and codes. So today I will be sharing my code where you can see how to make and create a generic function without using entity framework for ado. net. The scenario is like you have an old software that uses stored procedure to return set of entities as a data-table, you do not want to re-write the back-end code as you are creating a web API in c# which needs to be delivered asap. You need to map these data tables to models as you might be using MV* pattern. So here we will be doing one to one mapping of model to data- table, and in similar fashion insert or update can also be done. So basically we are converting a data-table to list of strongly typed object model to do CRUD operations. So we have following things before hand. A helper class is referenced as the database(dbFactory) which executes ado. ne...

Nested update panels with file upload and download in asp(.)net c#

This comes as basic requirements by the majority of clients that they want a good user experience with some file uploaders/downloads for templates which are further used in your application. Just in case,  you are not using any new technologies, no spa, no JSON and no angularjs etc. Your app is made of asp.net because of security concerns of your firm and not so knowledgeable managers and the page refreshes every time the second you click a button and reload the entire content, but you do not want that as you know UX design ethics, so to overcome this problem you added an update panel and a nested update panel outside button to make it asynchronous, but what is this? You are not able to make button click event to get hit in code behind c#. What will you do? You will use update panel triggers like below. <asp:UpdatePanel ID= "UpdatePanel1" runat= "server" > <ContentTemplate> <fieldset style= "width:...

Never ending (infinite) asp(.)net C# session

Session timeout is the worst nightmare of a developer especially if you do not have much experience in coding but senior enough to design an small application which becomes a mammoth in few months. So to all the mid-level experience guy, here is the key to success, do not make session variables in code behind, instead, create separate c# file either in app_code or in a separate project which you can call Utils as it will act like your utility file and create a public property in it which will call your DB whenever your session is null like following. So let's create a never ending session for the c#, Here is the scenario, suppose you have a user data-table coming from database which is on another project/DB and you have to get it through AD authentication or let's imagine it's a heavy DB call so calling, again and again, will reduce the app performance and that's why we use session which is again a non-performing thing but still better than DB chat. Here is your...

Sitefinity - Custom Image selector - Azure Player Dynamic Image selector.

Well, I am writing this post after a really long gap of time, I was away from my blog due to some personal reason and aggressive deadlines for my project, so today I thought of writing a blog about my experience with sitefinity, (http://www.sitefinity.com/) which is Content Management System aka CMS from a very famous IT firm Telerik. The CMS is purely on HTML and has its own pros and cons, it doesn't have any custom widgets available and you have to design most of them. So while developing my client website for it I found issues about the CMS shortcoming and lack of documentation, basically lack of documentation is the major problem with this CMS. So we had a requirement, we need to show the AES encrypted adaptive bit rate video to the user, we were using azure services for this, but the major requirement is that we were using a CMS and we need to make everything dynamic i.e. Business User editable and a drag and drop widget, which includes: Video URL. Video Title. Vid...

Simple Pyramid in Csharp .Net

    *   ***  ***** ******* Updated  As you can see below the code for simple pyramid has two for loop nested in each other. So for each row it enter, it increment the counter and draw the star against forming a pyramid like figure. You can use  different special counter, play with the loop and enjoy the coding. Simple Pyramid in Csharp    class Program     {          static void Main( string [] args)         {             int row, col = 0, n = 5;                                 for (row = 0; row <= n; row++)             {           ...

Rise, Fall and Struggle of Microsoft .Net [Article]

Some years ago there was a guy named Vinod Khosla from IIT- Delhi and Stanford. This awesome guy co-founded Sun Microsystem. Sun Microsystem as we all know is fully undertaken by Oracle. But Sun Microsystem came up with a fantastic language called Java. This language was becoming too famous and popular since its birth and by 90's its JAVA, JAVA and only JAVA everywhere. Microsoft who was struggling with the PC market at that time want's to capture the opportunity and Microsoft was building everything like the OS  and Office and Java was taking the credit of so called windows application so then MICROSOFT plans to launch its own language. Yes, so think back a decade or so ago. As you may recall, .Net was supposed to be much more than just the next version of Windows DNA or COM+ or COM. It was supposed to destroy Java, extend the Windows platform, and secure the Microsoft monopoly for another decade or two. But then what happened after the DOT COM BUBBLE. Rise Like m...

Three Tier Architecture : A simple Example/Illustration

Three Tier Architecture: Very Easy to understand, the three tier are: 1. Presentation layer 2. Business Layer 3. Database Layer Simple Example: Lets take example of a Songs and Artist. 1. DataAccess Layer Below is a class of Data Access Layer, that has some dummy/fudge data, we only write DataBase calling method in DataAccessLayer. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DataBaseLayer {     public class DataAccessLayer     {         string [] ArtistName = { "Akon" , "Shakira" , "Michael_Jackson" };         string [] Shakira = { "Waka Waka" , "Addicted to you" , "Beautiful Liar" };         string [] Akon = { "Chammak Challo" , "Lonely" , "Dont Matter" };         string [] Michael_Jackson = { "Beat It" , "Danger...

Using Parameterized Queries to avoid SQL Injection In C# .Net

An SQL injection attack consists of insertion or "injection" of an SQL query via the input data from the client to the application. Attackers potentially use SQL Injection to: -Logging into your applicatiob by Bypassing the authentication. -Gaining access to your sensitive information in the database. -Tampering or destroying data. There are two complementary and successful methods of mitigating SQL Injection attacks: -Parameterized queries using bound, typed parameters. -Careful use of parameterized stored procedures. So one common use of such queries is explained in the below example written in Dot Net, which uses Parameterized queries for the variables declared with initial @. And also you can avoid the error caused by Apostophe (') in the variable if you are updating a table. Like if you have a variable like O'Connor then you app will throw an error, which can be avoided by the use of Parameterized query. It is one of the best way to avoid h...

Creating a HTML tag in C# Code (TEXT AREA) Asp.Net and display it in HTML Page

Follow @harshit_parshii HtmlTextWriter:-  The HtmlTextWriter class is used to render HTML 4.0 to desktop browsers. The HtmlTextWriter is also the base class for all markup writers in theSystem.Web.UI namespace. It Writes markup characters and text to an ASP.NET server control output stream. This class provides formatting capabilities that ASP.NET server controls use when rendering markup to clients. It allows you to generate a list of HTML elements, such as <div> elements. You could use StringBuilder to create the HTML, but HtmlTextWriter is sometimes better—fewer errors are likely. Below is sample code: protected void Page_Load(object sender, EventArgs e) {     TextBox txtBox = new TextBox();     // Initialize StringWriter instance.     StringWriter stringWriter = new StringWriter();     // Put HtmlTextWriter in using block because it needs to call Dispose.     using(HtmlTextWriter writer ...

Beginners Guide to File Handling in C#, .net

Make a new project in Visual Studio (Console App) Add a new .cs file named as MySetting.cs, in that file copy paste the following code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 using System ; using System.Collections.Generic ; using System.Linq ; using System.Text ;...

Asynchronous Threading, First Code

Synchronous threading will execute each thread/command in the order they have been created/started Suppose there are 4 threads A, B, C, D Application Starts at 10:00:00 time: 10:00:00: -> Start thread A (will run for 5 seconds) time: 10:00:05: -> Start thread B (will run for 10 seconds) time: 10:00:15: -> Start Thread B1 (will run for 5 seconds) time: 10:00:20: -> Start thread C (will run for 15 seconds) end application: total run time will be 35 seconds Asynchronous threading will run the threads at the same time/ parallel processing, or we can say it will take the thread that is available at the earliest. Application Starts at 10:00:00 time: 10:00:00: -> Start thread A (will run for 5 seconds) / Start thread B (will run for 10 seconds) / Start thread C (will run for 15 seconds) time: 10:00:10: -> Start Thread B1 (will run for 5 seconds) end application: total run-time will be 15 seconds (because the longest run-time is thread C) So Async Threadi...

Inverted Pyramid, playing with stars

Playing with stars in C# *******  *****   ***     *     class Program     {          static void Main( string [] args)         {             int row, col = 0, n = 5;                                   int k = n;             for (row = 0; row <= n; row++)             {                 for ( int r = 0; r <= row; r++)                 {          ...