Monday, January 23, 2012

Working with Nullable Types

******************* Type 1 ***********************
int iUnits;
int iStock ;
if (iStock== null)
  {
      iUnits= 0;
   }
else
 {
     iUnits= (int)iStock;
}

******************* Type 2 ***********************
// using the coalesce operator, ??,
    int iUnits= iStock ?? 0; 
The coalesce operator works like this: if the first value (left hand side) is null, then C# evaluates the second expression (right hand side).

Declare Null datetime variable

****************** Type 1 *********************
System.Nullable<DateTime> dt;
dt = null;

****************** Type 2 *********************
DateTime? dt1;
dt1 = null;

How to Use foreach in Multiple classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Indexer
{

public class NameDetail
{
public string FirstName { get; set; }
public string MiddleName{ get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
String strFullname="";
if (string.IsNullOrEmpty(FirstName) == false )
{
strFullname = FirstName;
}
if (string.IsNullOrEmpty(MiddleName) == false)
{
strFullname += MiddleName;
}
if (string.IsNullOrEmpty(LastName) == false)
{
strFullname += LastName;
}
return strFullname.Trim();
}

}
}

public class Name: IDisposable, IEnumerable, IEnumerator
{
List arrPos = new List();
public int count { get ;set ;}
private int Position = -1; // This is for Ienum interface

public NameDetail this[int pos]
{
get
{
try
{
return arrPos[pos];
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
set
{
if (arrPos.Count <= pos)
{
arrPos.Insert(pos, value);
count = arrPos.Count;
}
else
{
throw new Exception("No Index postiion");
}
}
}
#region IDisposable
public void Dispose()
{
arrPos = null;
}
#endregion IDisposable


//**********************************************************
//********************* Use For Each in object *************
//*********************************************************
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
if (Position < arrPos.Count - 1)
{
++Position;
return true;
}
return false;
}
public void Reset()
{
Position =-1;
}
public object Current
{
get
{
return arrPos[Position];
}
}
#endregion
}



class Program
{
static void Main(string[] args)
{
Name a = new Name();
NameDetail ND ;
ND = new NameDetail();
ND.FirstName = "Mohit";
ND.LastName = "Agrawal";
a[0] = ND;
ND = new NameDetail();
ND.FirstName = "aaa";
a[1] = ND;

foreach (NameDetail n in a)
{
Console.WriteLine(n.FirstName );
Console.WriteLine(n.MiddleName);
Console.WriteLine(n.LastName );
Console.WriteLine(n.FullName);
}
Console.ReadKey();
a.Dispose();
a = null;
ND = null;
}
}
}

Thursday, April 21, 2011

Asp.net 2005: Generate CSV file and popup

//prepare the output stream
Response.Clear();
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition",string.Format("attachment; filename={0}", fileName));

//write the csv column headers
DataTable dt1;
dt1 = ds.Tables[1];
foreach (DataRow row in dt1.Rows)
{
for (int i = 0; i < dt1.Columns.Count; i++)
{
Response.Write("\"" + row[i].ToString() + "\"");
Response.Write((i < dt1.Columns.Count - 1) ? delimiter : Environment.NewLine);
}
}
DataTable dt;
dt = ds.Tables[0];
//write the data
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
Response.Write("\"" + row[i].ToString() + "\"" );
Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine);
}
}

Response.End();

Dynamic Create Zip file .net 2.0

Note: Add two namespace and one Reference liberary Name "vjslib".

using java.io;
using java.util.zip;


private void Zipfile(string fileName,string Extn)
{
string TempPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
string allFiles = TempPath + @"\" + fileName;
Response.Clear();
Response.ContentType = "text/zip";
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName.Replace(Extn, "zip")));
//Response.AddHeader("Content-Length", fileName.Replace("CSV", "zip").Length.ToString());

// Ziping code.
StringBuilder sb = new StringBuilder();
string ZipFileName;
string theDirectory = TempPath;
ZipFileName = TempPath + @"\" + fileName.Replace(Extn, "zip");
FileOutputStream fos = new FileOutputStream(ZipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setLevel(9);
string sourceFile = allFiles;
FileInputStream fis = new FileInputStream(sourceFile);
ZipEntry ze = new ZipEntry(sourceFile.Replace(theDirectory + @"\", ""));
zos.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len;
while ((len = fis.read(buffer)) >= 0)
{
zos.write(buffer, 0, len);
}
fis.close();
zos.closeEntry();
zos.close();
fos.close();

// popup Zip file
Response.TransmitFile(ZipFileName);
Response.End();
}

Hash Table in C#

 The Hashtable is a non-generic collection and used to store the key/value pairs based on the hash code of the key. Key will be used to acce...