asp.net

Working with Trace & Log in c#

Download Demo Console Application Here

In many applications we need to trace various event & errors.
The .NET Framework contains a mechanism called a Trace Listener. This Listener is an object which directs the trace output to an appropriate target, such as an error log for example.

Lets create a console application to understand Tracing.

First we need to configure app.config / web.config for this

Here we are adding Source named “TraceSourceApp” with switchValue All”.

SwithValue defines the tracing level. The available tracing levels are:
1. Off
2. Critical
3. Error
4. Warning
5. Information
6. Verbose
7. ActivityTracing
8. All

For more information on Tracing level Refer

Here we are using tracing level “All” as we want to trace all the events of the application.
Also we can see that this source has listener named “xml”.

As you can see, we are adding a listener of type System.Diagnostics.TextWriterTraceListener which is set to write to a log file called log.svclog (here we can also create log.log file instead of log.svclog).

Now in static main class we need to create readonly source of class “TraceSource” with source defined in app.config (In our case it is “TraceSourceApp”)

Now in your main method you need to trace the event like this

1st parameter TraceEventType identifies the type of event that has caused the trace.
For more information please refer
Bellow is demo code of main class.
2nd parameter is event identifier id.
3rd parameter message whic has to shown.

After running the console application you will get result something like bellow.

Download full source code Here

Solving “The given key was not present in the dictionary” error of visual studio 2010

While developing mvc3 razor application I came across one strange error “The given key was not present in the dictionary”.

After searching in google for hours I found the solution for this error.

Actually this error means that “This typically means that you’ve installed a custom data provider, created a Server Explorer connection using that provider, and then uninstalled the provider without deleting the Server Explorer entry”
To solve this error 1st thing you have to do is close all visual studio instances.

Now go to your user AppData directory (e.g. C:\Users\Administrator\AppData)
(Notice: AppData may not be shown in windows explorer if you have “Show hidden files and folders” turned off)

In AppData go to Roaming > Microsoft > Visual Studio > 10.0 > ServerExplorer

There you will find file named DefaultView.SEView. This files stores all connections.

This is a plain XML file, so in theory, you can locate the dud connection by its Label and just remove the containing DataViewNode XML element. In practice, I didn’t have much luck with this – the file stores blobs against connections by index, so deleting individual items can throw those indexes off. But if you’ve got a lot of connections defined and you don’t want to have to recreate them, it’s probably worth giving this a try in case you get luckier than I did.
Otherwise, just delete the DefaultView.SEView file.

Now restart visual studio & its done . Now you are free from that bad error.

Referance: http://hestia.typepad.com/flatlander/2010/08/fixing-the-server-explorer-the-given-key-was-not-present-in-the-dictionary-error.html

pass values from one page to another using query string in asp.net

Many times we want to pass values between asp.net pages. This can be easily done using Query String.
Let’s take an example where we will pass name & city of the person to page myWebpage.aspx using query string.
Let’s say we have two textboxes where we write Name & city. On submitting values of this textboxes should go to other page.

Like this link will form something like this
http://www.localhost.com/myWebpage.aspx?name=&city=
Let’s read this value on page load event of myWebpage.aspx.

Like this we can pass values from one page to another using query string

Working with NuGet in Visual Studio 2010

What is NuGet?
It is extension for visual studio by which we can easily add, remove & update libraries & tools in Visual Studio Projects.When we add any library , NuGet copies files to the solution,also it makes necessary changes to app.config or web.config & adds references.When we remove the library then it removes all related referances & reverses changes so that no error comes in solution.
NuGet provides a quick and easy way to add features to an existing application while ensuring that those features are integrated into source control. After installing a library, you can commit changes to your source control repository, and then another developer can perform a get operation against the repository in order to start working with the new features.

How NuGet Works?
Everything necessary to install a library or tool is bundled into a package (a .nupkg file). A package includes files to copy to your project and a manifest file that describes the contents of the package and what needs to be done to add or remove the library. Packages are bundled into feeds that Visual Studio accesses in order to present lists of available packages.
Nuget provides two ways to work with packages, via console or graphical window. While the graphical window is easiest, the console window is more powerful. You can now quickly add project references to many available packages via the NuGet service.

How to download NuGet?
Click Here to download NuGet.

How to Install NuGet?
We can install it Visual Studio Extension Manager.To check if your copy of Visual Studio already has the extension, look for Library Package Manager in the Tools menu of your copy of Visual Studio.

If you dont have then click Here to download.

Double Click on the extension which is downloaded.

Click on install & it will be installed.

For More Document releated to NuGet Click Here

Have a look at the sample video which explains that what NuGet can do :

How to uninstall NuGet?
Run visual studio as Administration.
Go to Tools -> Extension Manager.
there you can find “NuGet Package Manager” & there will be uninstall button.
We can uninstall it from there.

Format Date Time using BoundColumn / ButtonColumn in DataGrid

A very common desire is to set a column of a DataGrid/GridView to display just the month, day and year (or other custom formats) for a dateTime field.

Below is an example for the same,

The first column with header ‘DateTime 1′ is a BoundColumn which uses DataFormatString=”{0:yyyy.MM.dd}”. This will format a date time string of 28-10-2011 12:52:00 to 2011.10.28.

The same can be used with ButtonColumn, as shown here the date time string of 28-10-2011 12:52:00 is converted to a format of 2011.10.28 12:52

How to show image from binary data in database in MVC3 razor?

In this tutorial i will explain that how can we show image in web page from binary data of database.

Get image from database in your controller….

Create the action in your controller which will get the binary data of the image from database in the variable.

Return the File to the view with binary data & image file type.

Call the action from the view

Now we need to call the action from the view. Which can be done as shown above.

Where :

GetImage : Action Name. (here put your action name)

ImageLoader : Controller Name. (here put your controller name)

@Model.Id : id of the data for which you want to show the image (here put id for which you want to show the image )

OperationContract attributes/properties of the WCF in .net framework 4.0

OperationContract attributes/properties

1. Action:

2. AsycnPattern:

3. IsInitiating:If we are using this attribute the SessionMode attribute of the ServiceContract should be defined.It has two modes

a. True:It is default mode .If IsInitiating property is true then that method can be called at the beginning.
b. False: If IsInitiating property is false then that method cannot be called at the beginning.

4. IsTerminating: If we are using this attribute the SessionMode attribute of the ServiceContract should be defined.It has two modes

a. True:If IsTerminating property is true then session will end after implementation of that method.
b. False: IsTerminating property is true then session will not end after implementation of that method.

5. Name: This is used to provide the name to the method by which client will call it.

6. ProtectionLevel:It is System.Net.Security.ProtectionLevel which defines the protection level of the service contract. It has three modes.

a. EncryptAndSign: It ensures that data will be encrypted & signed before it is transmitted.
b. None: Data will not be signed nor encrypted & it is transmitted as it is.
c. Sign: Data will only signed before it is transmitted.

7. ReplyAction:

8. IsOneWay:It has two modes

a. True: It specifies that the method will not return anything. Method’s return type should be “void” if IsOnwWay is true.
b. False: It is default value for the OperationContract. No need to specify this if method returns anything.

Using of update panel (UpdatePanel) of ajax in asp.net

In this tutorial i will explain how can we use UpdatePanel control which you can find in the section Ajax Extensions in the Toolbox…

UpdatePanel is used if you want to do partial post back i.e. if we want to post back only some part of the page & not whole page.

For example take a example of taking two numbers in the text box & showing it in the third text box on button click. As it is very easy you can create this of your own. create this all controls in the UpdatePanel.

Remember to put ScriptManager Control before the UpdatePanelas it is required to run any java script of Ajax.

Now take one more text box out side the UpdatePanel just to ensure that partial post back is occuring.

Your code will look something like this….

& button click code would be something like this …

Now run your application…..

When page opens …write something in the textbox which is outside the update panel ….

& then put some numbers in the first & second text box & click on add …..

Post back will happen only for that portion & addition will come on the third textbox ….

But you can see that text written in the last textbox is as it is as only controls inside had a post back & not whole page…

UpdatePanel has two properties UpdateMode & ChildrenAsTriggers .

UpdateMode:

It has two values

1)Always: It indicates that content of the each control in side that UpdatePanel should be updated on each post back which originates from the page.

2)Conditional: It indicates that control should be updated only on the post back of specific controls.

ChildrenAsTriggers:

When this property is set to true then any updates triggered by the nested UpdatePanel will also trigger an update to the parent UpdatePanel.

How to show multiple columns in the single dropdown list using sqlreader?

Here i will show you how can u bind the multiple columns ti the single dropdown list..

Steps:

Add dropdown list from toolbox.

Now bind the data at the page load event of the page.

Remember to bind the data source to the dropdown list …..

& also bind the column to the drop down list like i did i.e.

Like this you can bind multiple columns to the dropdown list ….

Converting normal textbox into password textbox on clicking on the textbox

In this tutorial i will show how can u show the text in the textbox which is having TextMode=password but when clicked on that textbox it converts it to the password textbox.

Steps:

Take one textbox & set its TextMode to password.

now go to source mode of visual studio write this in the textbox

After adding this what happens is …..

It will initially show text “PASSWORD”.

After that when you click on it ..it will be converted password textbox which will show text as “*”

your textbox markup should look something like this….

1 2 3  Scroll to top