Wednesday, March 4, 2009

Cookies using Javascript

Cookies using Javascript

Hi all,
Today , I was working on my project and I was required to work on the cookies in the client side. Infact , I have to read and write cookie from both client side and server side. For server side it was easy to read , write and expire the cookie but I need to work bit hard in javascript for the same.
Now, I will tell you the method I wrote to read, create and expire the cookie.

//Creating Cookie

function CreateCookie(cookieName,value,hrs)
{
if (hrs)
{
var date = new Date();
date.setTime(date.getTime()+(hrs*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
document.cookie = cookieName+"="+value+expires+"; path=/";
}

//Reading Cookie Value

function ReadCookie(cookieName) {
var nameEQ = cookieName + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

//Deleting Cookie

function EraseCookie(cookieName) {
createCookie(cookieName,"",-10);
}

I hope the method I wrote are self explanatory .
But if you still have any issue, please feel free to contact/reply me.

Thanks
Arvind-Ramp
Happy Programming 

Tuesday, March 3, 2009

Working with Wilson ORmapper

Working With Wilson ORMapper

Hello ,

I am working on a project and using wilson ORMapper to communicate with the database. I faced lot of problems as I didn’t find any the proper document about how to work with ORMapper. Then I thought that after learning I would prepare a very brief document to show how to write the method to communicate with the database using ORMapper. Here are some of the method :


Updating a record.


public static User UpdateUser(IContext context, User user)
{
Global.Manager.StartTracking(user, InitialState.Updated);
Global.Manager.PersistChanges(user, PersistDepth.SingleObject);

return user;
}

Adding New Record

public static User CreateUser(IContext context, User user)
{
Global.Manager.StartTracking(user, InitialState.Inserted);
Global.Manager.PersistChanges(user, PersistDepth.SingleObject);

return user;
}

Getting Record by iD.

public static User GetUserByID(IContext context, int userID)
{
OPathQuery query = new OPathQuery("ID = ?");
return Global.Manager.GetObject(query, userID);
}

Getting collection Object of record.

public static Collection GetUsers (IContext context)
{
if (userStatus == Constants.All)
{
OPathQuery query = new OPathQuery("");
return Global.Manager.GetCollection(query);
}
}


Calling Stored Procedure using Stored Procedure

public static DataSet GetTransferDetails(IContext context, int siteID, int roomID)
{
DataSet dataSet;
SelectProcedure spTransferDetails = new SelectProcedure(null, GetTransferDetails");
spTransferDetails.AddParameter("@siteID", siteID);
spTransferDetails.AddParameter("@departmentID", departmentID); spTransferDetails.AddParameter("@roomID", roomID);

dataSet = Global.Manager.GetDataSet(spTransferDetails);
return dataSet;
}


Executing SQL query in ORMapper

public DataSet GetUserRecordHistory(int userID, string emailID)
{
string sql = String.Format("select UserName,[Time],SiteID,RoleID from User where userID = {0} and emailID = {1} Order By Time DESC", userID, emailID);
return Global.Manager.GetDataSet(sql);
}


Passing Multiple Parameter to the ORMapper query

public Product GetProduct(int manufacturerID, int productCategoryID, int productTypeID,string productName , string length, string diameter, string primaryFeature, string secondaryFeature, string otherFeature)
{
OPathQuery query = new OPathQuery("CategoryID = ? and ManufacturerID = ? and TypeID = ? and Name = ? and Length = ? and Diameter = ? and PrimaryFeature = ? and SecondaryFeature = ? and OtherFeature = ? and IsActive = 1");
return Global.Manager.GetObject(query, productCategoryID, manufacturerID, productTypeID, productName, length, diameter, primaryFeature, secondaryFeature , otherFeature);
}


Using Transaction In ORMAPPER

public static List AddProductandBarcode(IContext context, List productbarcodes)
{
Product product = null;
BarCode barcode = null;
Transaction transaction = null;
List savedProducts = new List();
try
{
transaction = Global.Manager.BeginTransaction();
foreach (ProductBarcode productBarcode in productbarcodes)
{
product = productBarcode.ProductObject;
barcode = productBarcode.BarcodeObject;

Global.Manager.StartTracking(product, InitialState.Inserted);
transaction.PersistChanges(product, PersistDepth.SingleObject);

barcode.ProductID = product.ID;

Global.Manager.StartTracking(barcode, InitialState.Inserted);
transaction.PersistChanges(barcode, PersistDepth.SingleObject);
savedProducts.Add(product);
transaction.Commit();
return savedProducts;
}
catch { transaction.Rollback(); return null; }
finally { transaction.Dispose(); }
}


Sorting Result using ORMapper

public string sortExp = "Name";
public Collection GetSites(IContext context)
{
Collection sites = null;
OPathQuery query = new OPathQuery("",sortExp);
sites = Global.Manager.GetCollection(query);
return sites;
}


Getting single Result/Execute Scalar

public static string GetTimeZoneBySiteID(int siteID)
{
string queryString = string.Format("select timezone from SiteTimeZone tz where s.SiteID = {0}", siteID);
return Global.Manager.ExecuteScalar(queryString);
}


Parameterized query in ORmapper

public static DataSet GetCaseInfo(IContext context, int siteID, DateTime dateTime)
{

DataSet dataSet = null;
string queryString = string.Format(@"select timezone,Time from SiteTimeZone tz where s.SiteID = @siteName and Time=@Time", whereString);

SelectProcedure selectProcedure = new SelectProcedure(null, queryString);
selectProcedure.AddParameter("@SiteID", siteID);
selectProcedure.AddParameter("@Time ", dateTime);

dataSet = Global.Manager.GetDataSet(selectProcedure);
return dataSet;
}


Hope u understood the methods i have written. i dont think these method need any further explanation.

if u have any query , feel free to contact/reply me .

hope to see to again.

-Thanks
Arvind - Ramp

Happy Programming

Saturday, December 20, 2008

Implementing Multiple Sorting in GridView.

Implementing Multiple Sorting in GridView.


In my project, I am using a grid view and have to apply MultiColumn sorting on the column of the Gridview. I have used listDictionary to store all the applied sort expression and also used DataView to sort the gridview data as par the sorting.

The Code I wrote is :



public ListDictionary ld_SortExpression;

public ListDictionary SortExpression
{
get
{
ld_SortExpression = (ListDictionary)ViewState["SortExpressions"];
if (ld_SortExpression == null)
ld_SortExpression = new ListDictionary();
return ld_SortExpression;
}
set { ViewState["SortExpressions"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindControls();
}

}

protected void gvRemoval_Sorting(object sender, GridViewSortEventArgs e)
{
SaveSortState(e);
BindControls();
}


Private void BindControls()
{
DataSet ds = GetData(); 'GetData()gets the data from the database.
if (ds!= null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
gridView.DataSource= SortData(ds);
gridView.DataBind();
}
}

//Saves All the SortExpression and SortDirection in ListDictionary.
Private void SaveSortState(GridViewSortEventArgs e)
{
ld_SortExpression = SortExpression;
string sortDirection = string.Empty;

if (!ld_SortExpression.Contains(e.SortExpression))
ld_SortExpression.Add(e.SortExpression, e.SortDirection.ToString().Replace("Ascending", "ASC").Replace("Descending", "DESC"))

else
{
sortDirection = ld_SortExpression[e.SortExpression].ToString();
if (sortDirection == "ASC")
{
ld_SortExpression[e.SortExpression] = "DESC";
}
else if (sortDirection == "DESC")
ld_SortExpression.Remove(e.SortExpression);
}
SortExpression = ld_SortExpression;
}

//Sorts Data.
Private DataView SortData(DataSet ds)
{
DataTable dt = ds.Tables[0];
DataView dv = dt.DefaultView;
StringBuilder sbSortExpression = new StringBuilder();

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
if (SortExpression.Count > 0)
{
string[] myKeys = new string[SortExpression.Count];
SortExpression.Keys.CopyTo(myKeys, 0);
for (int i = 0; i <= SortExpression.Count - 1; i++) { sbSortExpression.Append(myKeys[i]); sbSortExpression.Append(" "); sbSortExpression.Append(SortExpression[myKeys[i]]); if (i != SortExpression.Count - 1) { sbSortExpression.Append(", "); } } dv.Sort = sbSortExpression.ToString(); } } return dv; }



-Arvind :)

Saturday, December 13, 2008

How to get the Second Sunday of march/ First Sunday Of November in sql server for implementing Daylight saving time.

How to get the Second Sunday of march/First Sunday Of November in sql server for implementing Daylight saving time.?

Daylight saving time is the convention of advancing clocks so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.
In USA, from 2007, daylight saving time falls between ”first Sunday of November” to GetSecondSundayOfMarch.
In one of my project, i have to implement the daylight saving time and for this I have to get the second Sunday of march and first Sunday of November .In order to get on which date, “Second Sunday of march” and ”first Sunday of November” come, i created following functions in sql Server
GetSecondSundayOfMarch
CREATE function [dbo].[GetSecondSundayOfMarch](@year int)
returns datetime
as
begin
declare @dateTime datetime
set @dateTime = cast('3/8/' + cast(@year as varchar(4)) as datetime)
set @dateTime = case when datepart(dw, @dateTime) = 1
then @dateTime
else dateadd(dd, 8 - datepart(dw, @dateTime), @dateTime) end
return @dateTime
end

and first Sunday of November

CREATE function [dbo].[GetFirstSundayOfNovember](@year int)
returns datetime
as
begin
declare @dateTime datetime
set @dateTime = cast('11/1/' + cast(@year as varchar(4)) as datetime)
set @dateTime = case when datepart(dw, @dateTime) = 1
then @dateTime
else dateadd(dd, 8 - datepart(dw, @dateTime), @dateTime) end
return @dateTime
end




Thanks
Arvind - Ramp India.

Friday, November 21, 2008

How to excute Database query on remote server system

How to excute Database query on remote server system?
Ans: First Run sp_addlinkedserver storedProc with the server name as
exec sp_addlinked server ServerName

Then write your query eg:
Select * from servername.DBName.Tablename
-Arvind

Sunday, June 29, 2008