How to get the length of the image, text, ntext data type

Hi

Many a times we want to find the amount of space a particular database column is talking. We have the Len function in the SQL to get the length of a particular column data.

But the problem with this unction is that it does not work on the ntext, text or the image data type.

So when you have to find the length of a text, ntext or image data type use DATALENGTH function. This function can be used on all data types.

This is another of those functions that is not used daily but can be very helpful at difficult times.

Hope this helps
Thanks
Vikram


Nullable types and Boxing in C# 2.0

Hi

If we try boxing on a nullable type, the boxing will only take place if the object is not null. Meaning that if the HasValue is true then only object will be boxed otherwise instead of boxing the object reference is simply assigned to null.

bool? B1 = null;

object obj = B1;

// obj is null;

If the object is non-null, then boxing takes place. Only the underlying type that the nullable object is based upon is boxed. The system.Nullable that wraps the value is not boxed. These objects can also be unboxed into the nullable types like this.

bool? B2 = (bool?)obj;

This behavior provides two advantages. Firstly the nullable objects can be tested for null and secondly that the boxed nullable types fully support the functionality of the underlying type.

Hope this helps
Thanks
Vikram


Concept of Type parameter with the help of Generics

Hi

Generics are the new feature in the c# 2.0. They introduce the concept of Type parameter. This makes it possible to classes and methods that differs the specification of one or more types until the class or method is declared and used by the code, which will be using it.

Generic helps us to maximize the reuse of code, Type safety and better performance. Generics are most commonly used to create a collection. There are many new generic collection classes in the System.collection.Generics namespace. These classes are advised to be used for type safety and better performance over araylist.

We can also create our own Generic interface, classes, methods, events and delegates. Information on the types used in a generic data type may be obtained at run-time by means of reflection

We can declare a generic class like this

public class GenericClass<T>
{

Continued...

Using static class to separate data and behavior independent of any object

Hi

A few days back I was having a conversation with my colleague on the use of static class. At that point I decided to write a post on the issue giving an example on when to use a static class and members.

We use static class when we have to separate data and behavior that will be independent of any object identity. The data and functions do not change regardless of what happens to the object.

A static class can only contain static members. We cannot create an instance of a static class. Static class is always sealed and they cannot contain instance constructor. Hence we can also say that creating a static class is nearly same as creating a class with only static members and a private constructor (private constructor prevents the class from being instantiated).

Continued...

Using registry to save configuration setting

Hi

In windows application most of the time we have many configuration data, that need to be maintained for the application. This data can be easily saved in the application setting. But many a time we want to save the data in the registry. Here is how we do that/

To save the configuration values to the registry use the UserAppDataRegistry.SetValue method.

Application.UserAppDataRegistry.SetValue("Value", Value);

Now to load the values from the registry we have to use the getValue method. Always use this method inside the try and catch block because it will throw an exception if the value is not present in the registry.

Also remember to remove all the asterisks in the assembly version from the assembly.cs file and give it a proper assembly version otherwise the registry key will change every time you build the application.

Hope this helps
Thanks
Vikram


Trace output capturing in a text file.

Hi

In asp.net 2.0 it’s very easy to capture the trace file and keep it in a text file, Xml file or database. ASP.NET 2.0 introduces flexibility to output the trace information. We can also pass the trace output to any data storage we want.

The TraceContext class now features a TraceFinished event that provides programmatic access to trace output. In the page load event we create a handler for the TraceFinished event.

Trace.TraceFinished += new TraceContextEventHandler(OnTraceFinished);

Now we can use this tracefinished event to log the trace data.

 
void OnTraceFinished (object sender, TraceContextEventArgs e)

{

    foreach (TraceContextRecord record in e.TraceRecords)

    {

Continued...

Finding the statistics and other data for the site

Hi

In last six month I have tried to optimize my site for the Search engines. When doing so I am constantly looking at some of the sites/tools to know the position of my site in regards. These are publicly available free tools and shows statistics for websites. Here is a list of things that people might be interested, in context of there site.

Technical data of a site

IP Address - by DomainTools Search

Other Sites on an IP Address - by IP Query at Live.com

Name Server - by DomainTools Search

W3C Validation for the site page - by W3C Markup Validator

CSS Validation - by W3C CSS Validator

Continued...

Using the WAITFOR keyword in SQL server 2000 to perform a task at a given time or after a given time

Hi

SQL server 2000 also has a WAITFOR keyword to suspend the execution of a connection for a given time interval or a specified day of time has reached. The WAITFOR command can be specified with either of the two clauses.

The delay keyword with the amount of time to be passed before the WAITFOR statement is completed. This time can be up to 24 hours. Or we can use the time keyword followed by a time to specify the completion of the WAITFOR statement.

Here are two example of using the statement.

WAITFOR DELAY '00:00:02'

SELECT * FROM CATEGORIES

This example delays the execution of the select statement by 2 seconds.

BEGIN

   WAITFOR TIME '23:00'

   DBCC CHECKALLOC

END

Continued...

Showing hierarchical data from the database using the tree view control in asp.net 2.0

Hi

One of the cool new controls of the asp.net 2.0 is the tree view control. But there is no direct support in the treeview control to populate hierarchical data on the fly from the datasource. In this post I will talk about populating hierarchical data from the database to be shown in the treeview.

For the Example purpose lets say we have a table with hierarchical data. The table has three fields Cat_id, Name and ParentID. The parentId references the Cat_id to make the data hierarchical. On how to retirieve hierarchical data check my previous post.

First define a treeview control in the page like below

<asp:TreeView 

  ID="TreeView1"

  ExpandDepth="0"

  PopulateNodesFromClient="true"

  ShowLines="true"

  ShowExpandCollapse="true"

Continued...

How to use Tab Extender to use multiple tabs without postback

Hi

One of the very useful new extender in the Asp.Net Ajax is the tab control. This control is useful when you want to show the date in tab format without causing the postback when the user navigates from one tab to another.

A TabContainer is a host for a number of TabPanel controls. Each TabPanel defines its HeaderText or HeaderTemplate as well as a ContentTemplate that defines its content. The most recent tab should remain selected after a postback. The extender also maintains the Enabled state of the tabs after the postback

While using the Tab Extender we need to define the Tab Container and multiple Tab panel inside the tab container.

Continued...

How to show SharePoint type menu using the dropdown extender of the Asp.net Ajax extension

Hi

The drop down control of the asp.net Ajax extension is a really cool control. It shows some button on the mouse over and allows user to click on the button. Dropdown is an ASP.NET AJAX extender that can be attached to almost any ASP.NET control to provide a SharePoint-style drop-down menu.

The displayed menu is just a panel control. The dropdown gets activated on the click (Both left and right) of the attached control. There is also a cool animation effect on the mouse over of the attached control.

The dropdown extender has two main properties. The TargetControlID is the ID of the control that will show the dropdown. The DropDownControlID is the Id of the control, which will be displayed as the dropdown. This is normally a panel.

Hope this helps
Thanks
Vikram


Using the calendar control with its built in Animation Feature.

Hi

Calendar control is one of the most required client controls in the web development. This extender can be attached to any textbox. It provides client-side date-picking functionality with customizable date format and UI in a popup control. The animation while choosing the year, month and date is really cool.

By clicking on the title of the calendar you can change the view from Days in the current month, to Months in the current year. Another click will switch to Years in the current Decade. This action allows you to easily jump to dates in the past or the future from within the calendar control.

Continued...

AlwaysVisibleControl in the Asp.Net Ajax extension for showing a control even when user Scrolls

Hi

Another very cool control of the Asp.Net Ajax extension is the AlwaysVisibleControl. As the name suggest always visible control is always visible in the page. This is really helpful if you have a long scrolling page and you want some Part of the page to be always visible.

The main properties of the extender control are TargetControlID. This spifies the Control that will be always visible. Typically this will be user control. VerticalSide property can be either Top Middle or bottom. This property is used to define where the control be docked on the vertical end. Similarly HorizontalSide property takes Left, Center or Right as a paramater and defines where to dock the control horizontally. The VerticalOffset and HorizontalOffset property defines distance from the edge of the browser in pixel. We can also define the duration in seconds of the scrolling effect of the control. By default this property is set to 0.1 Second.

Continued...

How can I get a schema the record set that is returned from a stored procedure?

Hi

A few days back one of my collegue was asking me on how does the dataset gets the schema of the Table even when we are using the Stored procedure. He wanted to get the schema himself and work around that.

The data reader includes a getSchemaTable method which gets the schema in a data table on the result set. each row in the returned data table corresponds to a Column in the actual resutset. This schema can be utilized to infer the Column Name, its data type etc. To get additional schema information like base table name, column name etc. with commandBehaviour.keyinfo.

There are a few problems with this. If the stored procedure returns different result set depending on the input parameter, this might not work. Also if the stored procedure is using a Temporary table you might not get the addition schema with the CommandBehavior.KeyInfo.

Continued...

How to fetch both Master and detail data from the database

Hi

A few days back I was having a discussion with my colleges on how to fetch data when you want to show category data and also their sub-categories. What I mean is that you have a master table Category (Cat_ID, Cat_Name) and a subcategory table (Subcat_ID, subCat_Name, Cat_ID). Now you want to show category in a gridview and in show the subcategories in the nested grid views inside it.

For this example purpose lets say we have 5 rows in category table and 20 rows in the subcategory table

Continued...
 
Copyright © 2006 - 2008 Vikram Lakhotia