HaresH Chaudhari

Freelance Web Developer

Get First and Last Date from Calender control in Asp.Net

Hello guys

Here I am going to explain how we can get the first date and last date for selected month and year from asp.net calender control. Here we just need quite easy logic on 2 events on calender. The events are Prerender and Dayrender. In PreRender, I have take session as null so everytime when page is loading, it get null before processing on calender days. Then I have write some logic to show FirstDate, Lastdate and also same for each week in the month.

C# Code
protected void Calendar1_PreRender(object sender, EventArgs e)
{
Session["FirstDay"] = null;
Session["LastDay"] = null;
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (Session["FirstDay"] == null)
Session["FirstDay"] = e.Day.Date;

if (Session["LastDay"] == null)
Session["LastDay"] = e.Day.Date.AddDays(41);

string LastDates = Convert.ToDateTime(Session["LastDay"]).ToShortDateString();
if (e.Day.Date.ToShortDateString() == LastDates)
{
DateTime FirstDate = Convert.ToDateTime(Session["FirstDay"]);
DateTime LastDate = Convert.ToDateTime(Session["LastDay"]);
Response.Write(“FirstDay:”+Session["FirstDay"].ToString() + “<br />”);
Response.Write(“LastDay:”+Session["LastDay"].ToString() + “<br />”);
for (DateTime i = FirstDate; i <= LastDate; )
{
DateTime WeekStart = i;
DateTime WeekEnd = i.AddDays(6);
Response.Write(“Week Start: ” + WeekStart.ToShortDateString() + “<br />”);
Response.Write(“WeekEnd: ” + WeekEnd.ToShortDateString() + “<br /><br />”);
i = i.AddDays(7);
}
}
}

I have also attached screen for better understand. I hope this will be helpful for you guys.

Thanks

Filed under: Asp.Net, C#.Net, Web Development, , ,

Encrypt and Decrypt values in C#.Net

This article gives a brief overview of Cryptography and the Cryptography support provided by the .NET Framework. I begin by introducing Cryptography and then proceed to examine the various types of it. In particular, I review and analyze the various cryptography algorithms and objects supported by .NET. I conclude after proposing and briefly discussing the algorithmic technique that would work best for you.

Please find below a  simple code for how to do encrypt and decrypt values in c#.net

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

namespace Test
{
public class EncryptDecrypt
{
#region Declaration
static byte[] TripleDESKey1 = new byte[] { 51, 11, 17, 11, 31, 31, 37, 7, 23, 13, 23, 31, 43, 41, 7, 19, 91, 91, 47, 7, 37, 13, 19, 41 };
static byte[] TripleDESIV1 = new byte[] { 55, 13, 17, 31, 41, 51, 23, 7 };

#endregion

/// <summary>
/// To Encrypt String
/// </summary>
/// <param name=”value”>String To Encrypt</param>
/// <returns>Returns Encrypted String</returns>
public static string ToEncrypt(string value)
{
System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

//des.GenerateIV();
//des.GenerateKey();

des.Key = TripleDESKey1;
des.IV = TripleDESIV1;
//TripleDESKey1 = des.Key;//To Store Current Key To Use it in Decryption
//TripleDESIV1 = des.IV;//To Store Current IV To Use it in Decryption

System.IO.MemoryStream ms;

if (value.Length >= 1)
ms = new System.IO.MemoryStream(((value.Length * 2) – 1));
else
ms = new System.IO.MemoryStream();

ms.Position = 0;
System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(value);
encStream.Write(plainBytes, 0, plainBytes.Length);
encStream.FlushFinalBlock();
encStream.Close();

return Convert.ToBase64String(plainBytes);
}

/// <summary>
/// To Decrypt Data Encrypted From TripleDEC Algoritham
/// </summary>
/// <param name=”value”>String Value To Decrypt</param>
/// <returns>Return Decrypted Data</returns>
public static string ToDecrypt(string value)
{
try
{
System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
//System.IO.MemoryStream ms = new System.IO.MemoryStream(((value.Length * 2) – 1));
System.IO.MemoryStream ms;
if (value.Length >= 1)
ms = new System.IO.MemoryStream(((value.Length * 2) – 1));
else
ms = new System.IO.MemoryStream();

ms.Position = 0;
System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(TripleDESKey1, TripleDESIV1), System.Security.Cryptography.CryptoStreamMode.Write);
byte[] plainBytes = Convert.FromBase64String(value);
encStream.Write(plainBytes, 0, plainBytes.Length);
return System.Text.Encoding.UTF8.GetString(plainBytes);
}
catch (Exception ex)
{
throw ex;
}

}
}
}

Thank you

Filed under: C#.Net, Web Development, , ,

Blog Stats

  • 1,762 hits

 

May 2012
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  
Follow

Get every new post delivered to your Inbox.