Dynamically generating thumbnail images in ASP.NET with C# without affecting initial page load time

May 7, 2008

Creating thumbnail with ASP.NET with C#. There are several ways to do that but this way I feel much better and efficient and also this doesn’t affect initial page load time.

Introduction

While working for a website or an web application, you must have came across a situation where you need to display a thumbnail images for a larger images. There are several work around for it but I am going to show how to do that in the much better and efficient way, in this way your initial page load time will not be delayed because of the thumbnail images.

Approach

My approach of creating thumbnail is to create an arrays of bytes at run time and specify the img src attribute to it. I will also display NoImage.gif if my no source is being given to the function or any error occurred while dynamically generating thumbnail images.

To do this you will have to create a simple .aspx page that contains nothing but a Page_Load event.

<%@ Page Language="C#" AutoEventWireup="true"
      CodeFile="ShowImage.aspx.cs" Inherits="images_ShowImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Show Image</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

Just right click to your project and add a .aspx page with default contents mostly similar to above.

For more, visit http://www.dotnetfunda.com/articles/article63.aspx


What is ASP.NET 2.0 Page Life Cycle?

May 7, 2008

Main Events
=============
1. OnPreInit
2. OnInit
3. OnInitComplete
4. LoadViewState
5. OnPreLoad
6. OnLoad
7. RaisePostBackEvent
8. OnLoadComplete
9. OnPreRender
10. OnPreRenderComplete
11. SaveViewState
12. OnRender
13. OnUnload

Detailed Events
============
1. Constructor
2. Construct
3. TestDeviceFilter
4. AddParsedSubObject
5. DeterminePostBackMode
6. OnPreInit
7. LoadPersonalizationData
8. InitializeThemes
9. OnInit
10. ApplyControlSkin
11. ApplyPersonalization
12. OnInitComplete
13. LoadPageStateFromPersistenceMedium
14. LoadControlState
15. LoadViewState
16. ProcessPostData1
17. OnPreLoad
18. OnLoad
19. ProcessPostData2
20. RaiseChangedEvents
21. RaisePostBackEvent
22. OnLoadComplete
23. OnPreRender
24. OnPreRenderComplete
25. SavePersonalizationData
26. SaveControlState
27. SaveViewState
28. SavePageStateToPersistenceMedium
29. Render
30. OnUnload

 

For more visit http://dotnetfunda.com/interview/interview.aspx?qid=11


Looping through all rows of the DataTable

May 7, 2008

Simply write
DataTable dTable = dSet.Tables[0];
foreach (DataRow row in dTable.Rows)
{
myValule = row["ColumnName"].ToString();
}


Dynamically writing Meta tags on Master page from a User Control or any other class file

May 6, 2008

While developing web sites, you must have came across situations where you need to write Meta tags from the user control or any class files.

At first look it seems very difficult but I think it is not that much.

In My example I am writing Meta contents of my page from a utility class file.

Lets say my following method is in utility.cs file
// method that will write meta tags for keyword and description.

    public static void WriteMetaTags(Control ctrl, string keyword, string desc)    {

        Page pge = (Page)ctrl;

        HtmlMeta mKey = new HtmlMeta();        mKey.Name = "keyword";        mKey.Content = key;        pge.Header.Controls.Add(mKey);

        HtmlMeta mDesc = new HtmlMeta();        mDesc.Name = "description";        mDesc.Content = desc;        pge.Header.Controls.Add(mDesc);

    }

For complete article visit, http://www.dotnetfunda.com/articles/article2.aspx


Generating Ms Word document in ASP.NET and C#

May 6, 2008

It is very frequntly asked question in any of the forum or question-answer section on websites. So I decided to write a very compact article with source code.

It is as simple as 123.

Lets start with creating a simple .aspx page where we will give a text box to enter user’s Name and a button to click on. After clicking on the button Ms Word document will be generated.
.aspx page
The code will be

<form id="form1" runat="server">    <div>        Write your name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>        <asp:RequiredFieldValidator id="req1" runat="server" ControlToValidate="txtName" Text="*"></asp:RequiredFieldValidator>        <br />        <asp:Button ID="btn" runat="server" OnClick="GenerateMsWordDoc" Text="Generate Ms Word Document" />    </div>    </form>

For complete article, visit http://www.dotnetfunda.com/articles/article9.aspx


Conditional statement in CSS

May 6, 2008

You must have came across some .css issues related with the browser behavior. Some of the styles that works in IE doesn’t work in FireFox.

In that situation you need to write conditional code for both browsers.
Below is the sample code that demonstrate how to write conditional statement tin CSS.

<html>    <head>        <title>Conditional CSS</title>        <style type="text/css">            body            {                color:blue;            }        </style>        <!--[if IE 7]>        <style type=”text/css”>        body {            background-color:red;        }        </style>        <![endif]–>    </head>

    <body>        <p>            SHEO NARAYAHN        </p>    </body></html>

For more visit: http://www.dotnetfunda.com/articles/article15.aspx


Creating/Setting properties of User Control in ASP.NET

May 6, 2008

You might have came across a situation where you need to pass a value from your .aspx page to the user control (.ascx). This is possible in several ways including storing values into session or database and again retrieving at the user controls.

However, the easiest and optimized way is to create a properties of the user control and set it into the .aspx page and access it from the user control.

Today, I am going to describe you a step-wise process of creating, setting and retrieving properties of the user control.

For more visit http://www.dotnetfunda.com/articles/article16.aspx


4-Tier Architecture in ASP.NET with C#

May 6, 2008

Code Updated on 21st April 2008 to support Sorting, Paging and data manipulation through Stored Procedure in DAL

Almost all of us must have heard about 3-Tier architecture but what is this 4-Tier architecture? What are the benefits and how it is different from other architectures?

Well, the architecture I am going to demonstrate here is just enhancement of 3-Tier archicture. In this architecture; you no need of writing long function parameters throughout the layers (as in traditionally 3-Tier archicture has to) and the actual objects of the application will be in a separate tier so that in future you can separately use these objects for enhancements. Change in the object definition can be done without touching the entire Business Access Layers …………

Let me explain you step-wise process of creatioin of 4-Tier architecture application.

In this application, I am going to take example of a Person that will have 3 properties: FirstName, LastName, Age. We will create a separate pages to insert these records (default.aspx) into database and list,update,delete records (list.aspx) from database. In this application we will have following 4-Tiers

1. Business Object [BO]
2. Business Access Layer [BAL]
3. Data Access Layer [DAL]
4. UI (4-Tier) folder [UI]

Picture - 1 (Solution Explorer)


For original article visit http://www.dotnetfunda.com/articles/article18.aspx


Several Ways to populate DropDownList Controls

May 6, 2008

There are several ways to populate DropDownList control. In this article I have tried to cover some of them including populating DropDownList manually, programmatically, through xml, database, arraylist, hashtable. I have also covered how to specify different background color of every items of the DropDownList and how to generate DropDownList on the fly. At last I have covered how to get its index, text and value properties.

Here one thing is worth mentioning that DropDownList is rendered as the Select HTML tag in your page. Let’s start with different ways of populating DropDownList server controls first.

For more visit http://www.dotnetfunda.com/articles/article30.aspx


Transferring data from SQL Server database to MySql database

May 6, 2008

Hi, The other day I was facing problem while migrating my Sql Server database tables into MySql database. I tried to google it but couldn’t find any great solution that can do it through code easily. Thought to share this simple code to all of you.

Introduction

My problem was that I had a Sql Server database at my webserver and I had to migrate its data for any reason to my MySql database table that had the same table structure as the Sql Server had. As I didn’t had enough permission on Server to use DTS or other type of services to directly transfer my data to MySql so I had one option left that is to write a code that can get all data from Sql Server and transfer into MySql database.

Prerequisites

In order to use following function, you must have same Database table structure into both Sql Server and MySql database. If you have different structure then you may have to slightly play with the columns of the DataTable inside DataSet.

For original article, visit http://www.dotnetfunda.com/articles/article60.aspx