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 CdataAlso 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…. 🙂