Error: Login failed for user “IIS APPPOOL\DefaultAppPool”

Hello Everyone,

Today I encountered an error message for an application and here is the post on resolving it.

Lets understand this error message, App  Pool user have limited applications to have access, now my other application have the same app pool which is accessing the database stuff of these app pools. So resolution is

  1. Go to IIS
  2. Application Pools
  3. Look for app pool for application used.
  4. Click it and then click Advanced Settings.
  5. Find Identity Property -> change it to Local System.

Thats all for the day.

 

C# Extension Methods in Detail

Hi,

Today we explore extension methods of C# in detail by creating a class named ExtensionClass and implementing a simple method which prints string to console.

Now define other static class and define a static method which prints simple string to console. But this static method will be extension method hence we need to define its parameters with this keyword then space then ExtensionClass (Or any class name whose extension method needs to be implemented) finally attribute.

entire code looks something like

Ela

Now we invoke Method2 from Extensionclass object which mean that method2 is defined in static Class Program but is part of Extensionclass hence instantiated from its object.finally out put will be as:

Op

Well we will have a more post on it where we create a dll and add that dll in our project then add extension method to that dll and access it from our application.

Thats all for the day.

 

Working with Cache in Asp.Net 2

Hello Everyone!

Today we explore Caching with an example. Lets quick start by building simple asp.net application which has a dropdown to select a brand which displays products in gridview control. Also there are label controls which displays Server time and Client time. So our look and feel will be as shown,

ht

Its associated markup will be as,

mark

mar

Right Now, Please replace the @outputcache directive attribute value as VaryByParam=”None” from markup and run the application which fetches data from db and works well. Also we have used javascript by using script tags and document.write() function and Date function to print Client Side time.

The code behind for dropdown changed event is as,

cc

Custom function is made which accepts name of brand and provides it to stored procedure finally binds to gridview control. The same function is used to call for dropdown changed event and for page load. The following are the table and stored procedure code.

tb

sp

Most interesting part of this concept is that as it is asked at the begining to assign VaryByParam=None so entire page is cache and what every is been selected only cached page is being displayed rather than other item is selected.

VaryByParamnone

VaryByParamnone_Multi

nocc

As you can see that this page is cached at server time 8:29:09 and same being displayed though other brands being selected but can see client time is varying because it is javascript being executed for every refresh but server returning cache copy of page rather than executing page life cycle. Duration of cache is given by attribute in OutputCache directive.

Now change varybyparam=ddlproduct which is dropdown name for which server cache for each value of dropdown and return page who exists in cache for client requesting.

Final most awaited output is :

ddlprod

ddlpro

product

As you can see that pages are cached at server time and accessed at client time interval which is being retrieved from Cache.

Thats all for the day.

Happy Programming. 🙂

 

 

Working with Cache in Asp.Net

Hello Everyone. Today we explore Asp.Net with Caching. Lets know about Cache.

Cache is a small amount of memory which is used for fast retrieval of data which boosts scalability and performance. When cache is used in application so that gets stored in server cache and on subsequent requests serves from cache rather than actual processing page/data from page life cycle process.

Lets understand Caching in Asp.net with an example.

Build a web application which has two buttons named as Load Data and Clear Data. When user clicks Load Data button then Grid view should be populated with data of products on screen which must execute stored procedure by name spgetproducts and assigns text message to a label as Data From Database

The following design is done at markup and code as shown below:

mak

c

The most interesting part in this is that stored procedure returns data i.e list of products which is stored in dataset and is assigned to Cacheobject key as Cdata. Also we add one more constraint that whenever Load Data Button is clicked we at first verify that my Cache key is null or have something in it, if it has some value then will be assigning that cache object to gridview datasource and finally binding it and assigning message to label as Data From Cache in case if it is null then will call stored procedure and fill dataset then assign it to Cache object finally message is assigned to label as Data From Database.

Here is my tiny data table and stored procedure:

tb

sp

Similarly, when Clear Data button is clicked it verifies that does Cache key is null, if it is then displays text as Nothing to Clear… otherwise it removes that key from cache and displays Cache Cleared…. Simple code is as shown:

clear

Finally the most awaited output is shown below:

As we click the Load Button we get the below output.

oo

On Second time we click Load Data Button we get the below output.

ooo

When Clicked Clear Data Button,

o1

When clicked it again,

o

so thats all for the day.

Thanks

Working with Custom Exceptions

Hello Everyone,

Today we explore handling exceptions as per our requirements which generally termed as Custom Exceptions.

Lets quick start it by creating simple console application where we accept two numbers from end user and perform division on these numbers. As aware that if second  number is zero then leads Divide By Zero Exception which we handle by our own custom exception.

Below is the code which is handled by .net framework with the output as shown below:

Custom1

Custom2

As you can see that .net’s internal exception was handled and caused the message to be displayed. Now we override this behavior by implementing our own custom exception class. We create a new class by name MyException as shown below:

custom3

Now this custom class must override default exception class, so we handled the below code.

custom4

custom5

So with a constraint on second number where we check either entered number is less than or equal to 0 we call to MyException custom class and print the message.

 

This is how we handle custom exceptions to avoid unexpected behavior of system.

Thats all for the day.

Happy Programming… 🙂

 

Async and Await in C# World

Hello

Today we discuss few things about the Async keyword in C#.

Lets create a new windows application which reads a notepad text file and prints the characters in it when clicked a button process on form. Assuming that file has huge data and takes 8 seconds to read entire data till that instance system will be unresponsive and hangs.

Lets experience the same practically. Create a form as shown below

form

Set text property of label to nothing and set text property of button as Process. Switch the click event of button by double clicking the button from the design form and initialize a variable of type int by name count also initialize the text property of label1 as “Please Wait for Processing File” then in the next line assign value to the count variable which is executed by function call processfile() and display in label1.text property as shown in below image.

no async

When this code is executed system hangs for multiple seconds till processfile() method returns no.of characters and assigns value to count variable.

The best practice is to avoid hanging of system and allows it to be responsive at all times for which we make button click event as async method which does not makes system unresponsive and allows user to perform actions with hanging system. Async method also needs to have task created and passed processfile() method to task constructor which returns integer for which task should be made of type int.

Next task should be started and code as following lines.

async code

That’s all for the day…

Happy Programming..  🙂

Explore C# Aggregate method with Lamda Expresson.

Hello,

Today we discuss few things about the aggregation function of lamda concepts.

Lets start our discussion with an example.

Consider we have string of array with countries name which must be converted in to single string as done using aggregation function with lamda expression.

To understand better we first accomplish the above task using traditional coding style later do it with lamda expression aggregation function.

Below is the code where stores countries strings in countries variable then took foreach iteration and appended strings with comma but with this we observe that at last comma (,) got appended as shown in its output after Germany string.

output no aggre

In order to remove comma(,) from last string i.e after Germany we used strings built-in function named LastIndexOf  This function returns integer value which is position of last character in string. In our case last character is comma(,) which we provided as parameter to this function and its position is returned to lastindex variable which passed to result string variable and applied remove function and now we got the expected string as shown below.

code no aggre

output no comma

Now you can see that there is no comma (,) after the Germany string as shown below.

The same task can be accomplished with aggregate function using lamda expression.

We use aggregate function on Countries string array variable and provide lamda expression with two parameters a,b and provide expected concatenation output as shown below:

code aggre

Whose output can be as expected.

output aggre

Hence it reduces the lines of code and makes life easy.

Technically it accepts at first India and US as parameter a and b then makes string as quoted in lamda expression India,US as single string then considers India,US as parameter a and b as France  and process as per expression and concatenates as single string as India,US,France and so on.

This can be elaborated as tabular format

a b a+”,”+b
India US India,US
India,US France India,US,France
India,US,France Germany India,US,France,Germany

 

As you can see that finally we got the desired string. Single line with aggregate accomplishes multiple lines of code as seen in first example of this blog.

That’s all for the day.

Happy Programming…. 🙂

 

 

Polymorphism / Inheritance in C# 2

Hello

Today we look into few more basic concepts of abstract class which continuation of my previous blog under C#.

We already declared a abstract class with abstract method and accessed it in derived classes and now we remove that abstract keyword from method allowing it to have simple method. This gives us compile error as show below:

noabstract

It means compiler asking us to implement the method but not to keep its signature only since it is does not have keyword as abstract or extern or partial. So let’s define the method in abstract class as shown below:

abstractclassnoabstractmathod

Now it is noted that Transformer is an abstract class but Run is not abstract hence it is implemented and neither abstract nor virtual key word is used in above. Let’s inherit this abstract class in Car, helicopter and boat classes and implement the same Run method as shown below:

nooverridecar

nooverrideheli

nooverrideboat

Please focus that derived classes does not have override keyword because abstract class run method does not have virtual keyword for which we receive output as Run From Abstract Transformer

output

The reason behind this behavior is that always parent Run method is called because it is not allowing to gets executed of child Run method. This can be avoided if we use virtual keyword in abstract class and override keyword in child classes. With this parent Run method can be bypassed and child Run method gets executed if have override keyword else virtual Run method will be executed as shown below:

virtualtransformer

As you can see that parent abstract class have Run method with virtual keyword and respective child classes must have override keyword.

virtualcar

virtualboat

virtualheli

Now the output of the following program is as :

outputvirtualoverride

That’s all for the day.

Happy Programming.. 🙂

 

Polymorphism / Inheritance in C#

Hello Everyone.

Today we elaborate little basic things which all may be aware but my intension is to focus everything which could be helpful to a single person is what desired.

Lets create a abstract class by name Transformer and declare a abstract method Run then inherit it in class Car but child class Car does not implement abstract method for which  compile error is displayed and  run time/execution error is thrown as shown 2

Abstract class is created using abstract keyword also method Run is abstract as shown in above image.

Car

Now car class is created which a simple class which inherits abstract class Transformer using colon (:) operator used for inheritance. Also this class must implement the abstract methods else compile and runtime error is experienced as shown in above image that class Car has blue colored error message by interpreter stating does not implemented inherited abstract method as shown below:

error

Car class also implemented another method by name display which prints simple message. Now we create object of car class and calls the two method as shown below:

output

But this is not recommended by OOPS  principles of Polymorphism which states that one form of functionality can be implemented in different ways by inheriting the base class and this base class must be abstracted i.e no implementation should exists in it where as the derived classes should have its implementation based on requirements.

Consider the scenario of Transformer movie where transformer has the capability of transforming itself either as car, helicopter and boat based on instructions provided to reach from point A to B without imposing it any constraint to move either in Car or boat or in helicopter.

Here main functionality is to reach from source to destination (Point A to B). It is up to transformer to take us in which form.

Hence oops allows us to create reference type of Transformer but create object of desired class which allows loosely coupling among classes as shown below:

output1

Similarly we create two more classes of helicopter and boat and access it with reference type of transformer as shown below:

output2

Now trans is the base class reference type which is parent and capable of calling child class implemented methods but not capable of calling child self methods as in our case class Car has display method which cannot be accessed by reference type trans as shown below:

display

As you can see that intellisence not populating display method of class car that’s because transformer class is a parent which does not have signature of display method in it.

That’s all for the day.

Happy Programming.. 🙂

 

IEnumerable & IEnumerator in C#

Hello Everyone!!!

Today we discuss about the IEnumberable and IEnumerator.

To demonstrate about the accessing these concepts we need to create a simple list of type integer and  implemented as below.

list

Now start using Ienumerator  and Ienumerable as type int as shown

numera

ienum

The biggest differences between ienumerable and ienumerator is that IEnumerable does not preserve the state information while IEnumerator is capable of preserving the state and performing the operations from right from the current state.

Below is the example where we define two functions which prints 1 to 5 numbers and next prints 6 to 10 numbers.

This achieve perfect well with IEnumerator because state is preserved and produces dummy incorrect output when used IEnumerable. Following code is shown as

code

codeenum

Now we slightly modify the main method to call onetofive method with IEnumerable parameter as shown:

mainenum

Now again we  modify the main method to call onetofive method with IEnumerator parameter as shown:

numera

Output of the Ienumerator is shown below and output of IEnumerable is skipped because it is something random and is unacceptable behavior.

output

Thats all for the day.

Happy programming. 🙂