Jul 25, 2008

Alternating styles in ListView- without AlternatingItemTemplate

From within any template, you have access to the current index of the row within the whole data set, using Container.DataItemIndex, and within the currently displayed items, using Container.DisplayIndex. This gives us an easy way to alternate styles:

<ItemTemplate>
<li class="<%# Container.DisplayIndex % 2 == 0 ? "even" : "odd" %>">
<%# Eval("Name") %>
</li>
</ItemTemplate>


Just define the even and odd classes in your stylesheet and you're pretty much done.

kick it on DotNetKicks.com

Jul 24, 2008

Batch Inserts to SQL Server- Stored procedure method

To do batch Updates to SQL server

  • Create DataTable or DataSet and populate it with required rows
  • Next Create a SqlCommand and SqlDataAdapter using that SqlCommand
  • Remember to set the UpdateRowSource property on the Command the the appropriate value
  • Set the UpdateBatchSize Property on the SqlDataAdapter
  • then call SqlDataAdapter.Update(dt) to push the updates to SQL server
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("asin"));
dt.Columns.Add(new DataColumn("trackasin"));
dt.Columns.Add(new DataColumn("isrc"));
for(int i =0; i < 2000;i++)
{
DataRow dr = dt.NewRow();
dr["asin"] = asin;
dr["trackasin"] = trackasin;
dr["isrc"] = isrc;
//dr.RowState = DataRowState.Added;
dt.Rows.Add(dr);
}
------------------------------------------------
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MySpaceMusic"].ConnectionString))
{
SqlCommand command = new SqlCommand(INSERT_TRACK, connection);
command.CommandType = CommandType.StoredProcedure;
command.UpdatedRowSource = UpdateRowSource.None;
command.CommandTimeout = commandTimeout;

command.Parameters.Add("@asin", SqlDbType.VarChar, 255, dt.Columns[0].ColumnName);
command.Parameters.Add("@trackAsin", SqlDbType.VarChar, 255, dt.Columns[1].ColumnName);
command.Parameters.Add("@isrc", SqlDbType.VarChar, 600, dt.Columns[2].ColumnName);

SqlDataAdapter adpt = new SqlDataAdapter();

adpt.InsertCommand = command;
adpt.UpdateBatchSize = batchSize;
try
{
connection.Open();
int recordsInserted = adpt.Update(dt);
}
finally
{
adpt.Dispose();
}
}


kick it on DotNetKicks.com

Jul 3, 2008

क्यूट सोंग .. नानी तेरी मोरनी को मोर ले गए

Naani Teri Morni Ko Mor Le Gaye
Baaki Jo Bacha Tha Kaale Chor Le Gaye

Khaake Peeke Mote Hoke,
Chor Baithe Rail Mein
Choron Vaala Dibba Kat Ke, Pahuncha Seedhe Jail Mein

Naani Teri Morni Ko...

Un Choron Ki Khoob Khabar Li,
Mote Thaanedaar Ne
Moron Ko Bhi Khoob Nachaaya,
Jungal Ki Sarkaar Ne

Naani Teri Morni Ko...
Achhi Naani Pyaari Naani,
Roosa-Roosi Chhod De Jaldi Se Ek Paisa De De,
Tu Kanjoosi chod de...

Nani teri morni ko chor le gaye
baaki jo bacha tha kaale chor le gaye

kick it on DotNetKicks.com

May 6, 2008

Unity Application block and Generic Singleton

Here are some features of the Unity Application block. (Note there is overhead in using this block… i.e. overhead while creating instances of objects that have dependent objects)

http://msdn.microsoft.com/en-us/library/cc440954.aspx

  Highlights of the Unity Application Block

The Unity Application Block includes the following features:

· It provides a mechanism for building (or assembling) instances of objects, which may contain other dependent object instances.

· It exposes RegisterType methods that support configuring the container with type mappings and objects (including singleton instances) and Resolve methods that return instances of built objects that can contain any dependent objects.

· It provides inversion of control (IoC) functionality by allowing injection of preconfigured objects into classes built by the application block. Developers can specify an interface or class type in the constructor (constructor injection) or apply to properties and methods attributes to initiate property injection and method call injection.

· It supports a hierarchy for containers. A container may have child container(s), allowing object location queries to pass from the child out through the parent container(s).

· It can read configuration information from standard configuration systems, such as XML files, and use it to configure the container( The Unity Container which hold references to the objects built using Resolve method).

· It makes no demands on the object class definition. There is no requirement to apply attributes to classes (except when using property or method call injection), and there are no limitations on the class declaration.

· It supports custom container extensions that developers can implement; for example, methods to allow additional object construction and container features such as caching.

And here is the code for the Generic Singleton:

/// <summary>
/// Provides a Singleton implementation using Generics.
/// </summary>
/// <typeparam name="T">Type of singleton instance</typeparam>
public sealed class Singleton<T> where T : new()
{
Singleton() { }
public static T Instance
{
get
{ return Nested.instance; }
}

class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested() { }
internal static readonly T instance = new T();
}
}


The code for returning the instance then becomes:



        public static BlogSettings Instance
{
get
{
return Utils.Singleton<BlogSettings>.Instance;
}
}

You have to also change the constructor for BlogSettings to public to allow this this to work

kick it on DotNetKicks.com

Apr 30, 2008

Debugging Win Service

For debugging a Startup of Windows Service add the call to the Method Debugger.Launch or Debugger.Break. This call can be placed in the Constructor of the Service.cs or in the first line of the OnStart() method.

Calling Debugger.Launch() or Debugger.Break() in your code allow you to debug such problems.

For debugging an already running WindowService, simply attach the Debugger to to an existing process using "Debug\Attach to Process" menu item in VS 2005/VS 2008.


Visual Linq is a tool to visually create your Linq to SQL queries: http://code.msdn.microsoft.com/vlinq


A Primer on WCF :

It is all about Service Contracts, Operation Contracts, Data Contracts and Fault Contracts and other type of Contracts

Contracts usually enforced on Interfaces and the Classes then implement these interfaces.

If Service Contract, Operation Contract attributes are applied to classes then we have an additional advantage that the methods can be marked private and can still be accessed by WCF clients.

DataContracts are used to create .NET objects that can be sent to/fro between the client and the WCF Service. thus the DataMember attribute tells the runtime which properties need to be serialized/deserialized by the framework before being passed onto the network.

We dont need DataContracts for Intrinsic objects like Int32, string etc since they are natively serializable

FaultContracts can be applied to methods and can be used by the Client to determine what exceptions occurred at the server during processing of the request. Thus .NET exceptions are converted to FaultExceptions which are then sent over the wire as SOAP faults and then the client can reconstruct them to extract the Fault Detail from the FaultException

A good introduction to WCF can be found here: David Chappell's intro to WCF

Bindings and endpoints form the heart of WCF, we can have different kinds of bindings,

  • BasicHTTPBinding which allows Http and HTTPS access.
  • WSHttpBinding which adds the WS specifications to the binding to provide for reliability, transactions, security and other features.
  • WSDualHttpBinding: use this for two-way communication between client and service, only HTTP is supported
  • FederatedBinding: which means that if authenticated by one of the services, then the same ticket can be used to authenticate the client to other WCF/Java services
  • NETTCPBinding : used when communicating using TCP protocol as the transport
  • NamedPipeBinding: used within the local machine for inter process communication on the same machine.

Endpoints define how the service can be accessed by the client. Each WCF Service exposes endpoints to clients to publicize their services. Specify a different endpoint for each binding used by your service, thus the same machine can expose the service over HTTP and TCP using basicHttpBinding and NetTcpBinding respoectively and thus exposing endpoints for each of them. Endpoints consist of address, behavior etc

John Sharp's WCF Step by Step from Microsoft Press is an excellent book to get started.

kick it on DotNetKicks.com

Apr 6, 2008

Get back almost 1GB of free disk space after Vista SP1 installation

If you have installed SP1 of Vista you certainly recognized that your drive lost around 1Gb of free space.

The solution is to use a tool that comes with the SP1 to recover your free space at the price of not being able to come back and uninstall the SP1.

This tool is vsp1cln.exe that you can run like this:

vistasp1

kick it on DotNetKicks.com

Mar 27, 2008

Add Windows Explorer to your Visual Studio tools menu

.NET Tip of The Day: Add Windows Explorer to your Visual Studio tools menu

kick it on DotNetKicks.com

Mar 3, 2008

Concurrency and Co-ordination Runtime - CCR

Pretty cool Library from Microsoft for performing concurrent operations like multithreading operations and async tasks. Essenitally you get a guide to migrate all your APM ( Asynchronous Programming Model) tasks to the CCR tasks so that synchronization and co-ordination issues are automatically handled for you behind the scenes.

Some of the good links on CCR are:

kick it on DotNetKicks.com