Generate 16 Digit Unique Number in CSharp

Posted by Venkat | Labels: ,

Now i am going ot discuss about how to Generate 16 digit Unique number
So ,

5 digit Random using System.Random Class

5 digit number by using TimeSpan

6 numbers by System Time (HH:MM:SS)

This is the Code

System.Random fiveRandom = new Random();
TimeSpan tsFive = new TimeSpan();
tsFive = DateTime .Now.Subtract (Convert.ToDateTime ("01/01/1900"));
string rad = fiveRandom.Next(10000, 99999) +  tsFive.Days.ToString() +   System.DateTime.Now.Hour.ToString ("00") +          System.DateTime.Now.Minute.ToString ("00") +   System.DateTime.Now.Second.ToString ("00")  ;

Compare Datetime Objects

Posted by Venkat | Labels: ,

Here i am going to share
1) How to Compare two Datetime object ( here i am mentioning some ways to Compare Datetime Variables )
2) How to get the timeSpan Values from the Hours and add hours to the Datetime its easy.

It can be expressed as a date and time of day.

It compares Date with time.

DateTime chkExpireDateTime = new DateTime(2009, 8, 1, 0, 0, 0);

if (DateTime.Compare(DateTime.Now , chkExpireDateTime)< 0)
{
// the Current Datetime.now is less than ChkExpireDateTime
}
else if (DateTime.Compare(DateTime.Now, chkExpireDateTime) > 0)
{
// the Current Datetime.now is Greater than ChkExpireDateTime
}
else if (DateTime.Compare(DateTime.Now, DateTime.Now) == 0)
{
// the Current Datetime.now and ChkExpireDateTime are Equal
}

You can also Compare this way.

DateTime systemDate = DateTime.Now;
DateTime compareDate = DateTime.Today.AddHours(11D);

// less than
if (compareDate < systemDate)
Console.WriteLine("Less Than");

// equal to
if (compareDate == systemDate)
Console.WriteLine("Equal To");

// greater than
if (compareDate > systemDate)
Console.WriteLine("Greater Than");

// basically you can compare it in all the normal ways
// using !=, ==, , =, etc



The below code is used to get the Timespan for the for the hours and minutes given through the string.

string time = "02:10";
string[] pieces = time.Split(new char[] { '.' },

StringSplitOptions.RemoveEmptyEntries);
TimeSpan difference2 = new TimeSpan(Convert.ToInt32(pieces[0]),

Convert.ToInt32(pieces[1]), 0);
double minutes2 = difference2.TotalMinutes; // 130
double totalSeconds = difference2.TotalSeconds; // 7800

// this will add the seconds to the Datetime

DateTime getOutTime = new DateTime();
getOutTime = DateTime.Now.AddHours(Convert.ToDouble (getHours ));
getOutTime = DateTime.Now.AddSeconds(totalSeconds);

Response.Redirect Vs Server.Transfer

Posted by Venkat | Labels:

1) Response.Redirect(“newpage.aspx”)

In some case , if i use like this it shows ThreadAborExecution error, when the above line execute it Calls the Response.End() and it's Response.End() that calls Thread.Abort() this makes the error.

Response.Redirect(“newpage.aspx”,false) // whereas the second parameter is false / true

Ref: http://www.c6software.com/articles/ThreadAbortException.aspx

and Server.Transfer(“newpage.aspx”)

2) Server.Transfer -  redirects the client with in the same application ie, from one ASP page to other ASP page with in the application. This will not update the history.

Response.Redirect: - This redirects the client to the other URL ie, from one ASP page to other applications page. This will update the history.

3) Response.Redirect involves a roundtrip to the server

whereas Server.Transfer conserves server resources by avoiding the roundtrip. It just changes the focus of the webserver to a different page and transfers the page processing to a different page.

4) Roundtrip means in case of Response.Redirect it first sends the request for the new page to the browser then browser sends the request for the new page to the webserver then after your page changes.

But in case of Server.Transfer it directly communicate with the server to change the page hence it saves a roundtrip in the whole process.

5) If you are using Server.Transfer then you can directly access the values, controls and properties of the previous page - using Context.Item collections

- which you can’t do with Response.Redirect.

6) Response.Redirect changes the URL in the browser’s address bar. So they can be bookmarked.

Whereas Server.Transfer retains the original URL in the browser’s address bar. It just replaces the contents of the previous page with the new one.

7) Response.Redirect can be used to redirect a user to an external websites.
Ex: www.Google.com,www.hyderabadtechies.info..,

Server.Transfer can be used only on sites running on the same server. You cannot use Server.Transfer to redirect the user to a page running on a different server.

PayOffers.in