Asp.Net

Windows Users List in Asp.Net c#

using System.Management;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SelectQuery query = new SelectQuery(“Win32_UserAccount”);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
{
Console.WriteLine(“Username : {0}”, envVar[“Name”]);
}

Console.ReadLine();

}
}
}

create xml file dynamically

try
{
if (System.Web.HttpContext.Current.Session[“UserId”] != null && System.Web.HttpContext.Current.Session[“EventId”] != null && System.Web.HttpContext.Current.Session[“EventConfiguration”] != null)
{

Logger.Write(LogType.INFO, ModuleToLog.EventInformation, “WriteEventHistory”, “EventHistory”, HttpContext.Current.Session[“UserName”].ToString());
string pageName = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVariables[“SCRIPT_NAME”]);
UserService.UserClient proxyFind = new ePlannerPro2Client.UserService.UserClient();

epp2ClientPL.UsersProperties result = proxyFind.LoadById(new Guid(Convert.ToString(System.Web.HttpContext.Current.Session[“UserId”])), new Guid(Convert.ToString(System.Web.HttpContext.Current.Session[“EventId”])));

if (result == null)
{
return;
}
//epp2ClientPL.UsersProperties result = proxyFind.LoadUsersByEventId(new Guid(Convert.ToString(Convert.ToString(System.Web.HttpContext.Current.Session[“EventId”])))).Where(u => u.UserId.Equals(new Guid(Convert.ToString(System.Web.HttpContext.Current.Session[“UserId”])))).SingleOrDefault();

epp2ClientPL.EventConfigurationProperties files = (epp2ClientPL.EventConfigurationProperties)System.Web.HttpContext.Current.Session[“EventConfiguration”];

string FilePath = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + “/XML”);
DirectoryInfo ObjDirectoryInfo = new DirectoryInfo(FilePath);

string strFileName = FilePath + “/” + files.EventTitle.ToString() + “_History.xml”;

XmlDocument XmlFile = new XmlDocument();

if (ObjDirectoryInfo.Exists == false)
{
ObjDirectoryInfo.Create();
}

FileInfo fi = new FileInfo(strFileName);

if (fi.Exists)
{
XmlFile.Load(strFileName);
}
else
{
XmlNode Declaration = XmlFile.CreateNode(XmlNodeType.XmlDeclaration, “”, “”);
XmlFile.AppendChild(Declaration);
XmlElement OperationElement;
OperationElement = XmlFile.CreateElement(“Operations”);
XmlFile.AppendChild(OperationElement);
XmlFile.Save(strFileName);
}

XmlNode Operations = XmlFile.SelectSingleNode(“Operations”);

XmlNode Operation1;
Operation1 = XmlFile.CreateNode(XmlNodeType.Element, “Operation”, “”);
Operations.AppendChild(Operation1);

XmlNode EventAttendeeId;
EventAttendeeId = XmlFile.CreateNode(XmlNodeType.Element, “EventAttendeeId”, “”);
EventAttendeeId.InnerText = Convert.ToString(result.EventAttendeeId);
Operation1.AppendChild(EventAttendeeId);

XmlNode FullName;
FullName = XmlFile.CreateNode(XmlNodeType.Element, “FullName”, “”);
FullName.InnerText = result.FullName;
Operation1.AppendChild(FullName);

MenusService.MenusClient proxyPage = new ePlannerPro2Client.MenusService.MenusClient();

#region “Get Page Id”
ePlannerPro2Client.MenusService.MenusClient proxyMenus = new ePlannerPro2Client.MenusService.MenusClient();
epp2ClientPL.MenusProperties[] data = proxyMenus.LoadPageMasterNameId(pageName).ToArray();
if (data == null)
return;
if (data.Count() == 0)
return;
#endregion

XmlNode PageId;
PageId = XmlFile.CreateNode(XmlNodeType.Element, “PageId”, “”);
PageId.InnerText = data[0].Id.ToString();
Operation1.AppendChild(PageId);

XmlNode OT;
OT = XmlFile.CreateNode(XmlNodeType.Element, “OperationType”, “”);
OT.InnerText = OperationType;
Operation1.AppendChild(OT);

XmlNode OperationMessage;
OperationMessage = XmlFile.CreateNode(XmlNodeType.Element, “OperationMessage”, “”);

// string strOperation = result.FullName + ” ” + Operation;

string strOperation = Operation;
OperationMessage.InnerText = strOperation;
Operation1.AppendChild(OperationMessage);

#region “Get Time Zone Name”
ePlannerPro2Client.TimeZoneService.TimeZone timeZone = LoadTimeZonesById(new Guid(files.TimeZoneId.ToString()));
#endregion

XmlNode OperationDate;
OperationDate = XmlFile.CreateNode(XmlNodeType.Element, “OperationDate”, “”);
// OperationDate.InnerText = DateTime.Now.ToString(“MMM dd yyyy, hh:mm:ss tt”);
OperationDate.InnerText = DateTime.Now.ToString(“MM/dd/yyyy, hh:mm:ss tt”);
Operation1.AppendChild(OperationDate);

XmlFile.Save(strFileName);
}
}
catch (Exception ex)
{
Logger.Write(LogType.EXCEPTION, ModuleToLog.EventInformation, “WriteEventHistory”, ex.StackTrace, HttpContext.Current.Session[“UserName”].ToString());
}

Client System TimeZone Offset

The getTimezoneOffset method returns an integer value representing the number of minutes between the time on the current machine and UTC. These values are appropriate to the computer the script is executed on. If it is called from a server script, the return value is appropriate to the server. If it is called from a client script, the return value is appropriate to the client.

This number will be positive if you are behind UTC (e.g., Pacific Daylight Time), and negative if you are ahead of UTC (e.g., Japan).

For example, suppose a server in New York City is contacted by a client in Los Angeles on December 1. getTimezoneOffset returns 480 if executed on the client, or 300 if executed on the server.

function TZDemo()
{
var d = new Date();
var minutes = d.getTimezoneOffset();

var s = “”;
s += “The current local time is “;
s += minutes / 60;
if (minutes < 0)
s += " hours after UTC";
else
s += " hours before UTC";

return(s);
}

Convert DateTime into Specific TimeZone wise with DayLightSavingTime Changes

DateTime date1 = Convert.ToDateTime(“15-03-2010 3:30:00 AM”); //start day of daylighttime
//DateTime date1 = Convert.ToDateTime(“07-11-2010 3:30:00 AM”);
//DateTime date1 = Convert.ToDateTime(“08-11-2010 3:30:00 AM”); // end day of daylighttime

//set local timezone
TimeZoneInfo sourceTimeZone = TimeZoneInfo.Local;

//get system timezones list
System.Collections.ObjectModel.ReadOnlyCollection timeZone = TimeZoneInfo.GetSystemTimeZones();

List tz = timeZone.ToList();

//get event time zone standard name
epp2ClientPL.EventConfigurationProperties result = (epp2ClientPL.EventConfigurationProperties)HttpContext.Current.Session[“EventConfiguration”];
string strZone = tz.AsEnumerable().Where(u => u.DisplayName.Contains(result.EventTitle.Substring(12))).SingleOrDefault().StandardName.ToString();

//set event time zone
TimeZoneInfo destinationTimeZone = TimeZoneInfo.FindSystemTimeZoneById(strZone.ToString());

//convert datetime to event time zone wise
TimeZoneInfo.ConvertTime(date1, destinationTimeZone).ToString(“dd-MM-yyyy hh:mm tt”);

return date1;

Datatable.Copy() & DataTable.Clone() ???

DataTable dtCopy = table.Copy();

it will create “dtCopy” datatable with the records from “table”,

DataTable dtClone = table.Clone();

it will create “dtClone” datatable without any records. it only copy structure of “table” datatable.

DataTable dtCopy = dataViewTable.ToTable().copy();

it will create “dtCopy” datatablw with records only contains in DataView.

TimeZone : Get the date and time as per time zone

//Get the Collection of System’s Time Zones.
System.Collections.ObjectModel.ReadOnlyCollection timeZone = TimeZoneInfo.GetSystemTimeZones();
//Convert time zone collection to LIST
List tz = timeZone.ToList();

string strZone=””;
try
{
strZone = tz.AsEnumerable().Where(u => u.DisplayName == “(UTC+01:00) West Central Africa”).SingleOrDefault().StandardName.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}

TimeZoneInfo timeZone1 = TimeZoneInfo.Local;
TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById(strZone.ToString());

Console.WriteLine(“================= Time Zone =============”);
Console.WriteLine(timeZone1.DisplayName);
Console.WriteLine(timeZone2.DisplayName);
DateTime dtNow = DateTime.Now;
DateTime dtOperationDate = DateTime.Now.AddHours(-1);
Console.WriteLine(“========================================”);

Console.WriteLine(“Local DateTime = ” + dtNow);
Console.WriteLine(“Local Operation Date = ” + dtOperationDate);

try
{
DateTime convertDate = TimeZoneInfo.ConvertTime(dtNow, timeZone1, timeZone2);
DateTime convertOperationDate = TimeZoneInfo.ConvertTime(dtOperationDate, timeZone1, timeZone2);
Console.WriteLine(“Converted DateTime = ” + convertDate.ToString());
Console.WriteLine(“Converted Operation Date = ” + convertOperationDate.ToString());

Console.WriteLine(“Subracted date time = ” + convertDate.Subtract(convertOperationDate).Hours
+ “:” + convertDate.Subtract(convertOperationDate).Minutes + ” hrs ago”);
}
catch(Exception ex)
{
Console.WriteLine( ex.Message.ToString());
}
}
}
}

For More information see following links:

http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx

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

Insert Current DateTime Into SQL Using Asp.Net

Aspx Form
Javascript Function For Check validate time inserted or not

function CheckTime(Control) {
var txtBreakdownTime = document.getElementById(“”);
var ddlAMPM = document.getElementById(“”);
if (txtBreakdownTime != null && ddlAMPM != null) {
var BreakdownTime = txtBreakdownTime.value;
var HourNMinute = new Array();
HourNMinute = BreakdownTime.split(‘:’);
if (parseInt(HourNMinute[0]) > 12) {
alert(“Invalid Time Entry.”);
Control.focus();
}
if (parseInt(HourNMinute[1]) > 59) {
alert(“Invalid Time Entry.”);
Control.focus();
}
if (parseInt(HourNMinute[0]) == 12 && parseInt(HourNMinute[1]) > 0 && ddlAMPM.value == “PM”) {
alert(“Invalid Time Entry.”);
ddlAMPM.value = “AM”;
Control.focus();
}
}
——————————————————————

—————————————————————-
CS Page

DateTime StoppedAt;
String Start = txtDate.Text.Trim() + ” ” + txtBreakdownTime.Text.Trim() + “:00 ” + ddlAMPM.Value.Trim();
if (DateTime.TryParse(Start, out StoppedAt) == true)
{
StoppedAt = Convert.ToDateTime(Start);
sl.Add(“StoppedAt”, StoppedAt);
}

Sort on Gridview

Default.aspx.cs page:

Delcare Object
DAL objDal = new DAL();
string SORT_DESC = “Desc”;
string SORT_ASC = “Asc”;

Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindCountryGrid();

if (!String.IsNullOrEmpty(Request.QueryString[“Result”]))
{
DisplayMessage();
}
}
}

Bind Grid Code

private void BindCountryGrid()
{
DataTable dtCountry = new DataTable();
dtCountry = objDal.GetDataTable(“spGetCountry”);
if (dtCountry != null)
{
if (dtCountry.Rows.Count > 0)
{
dgCountry.DataSource = dtCountry;
dgCountry.DataBind();
ViewState[“dtCountry”] = dtCountry;
}
}
}

Sort the data of Gridview

protected void dgCountry_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState[“sortMode”] == null)
{
ViewState[“sortMode”] = SORT_DESC;
}
if (ViewState[“sortMode”] != null)
{
if (Convert.ToString(ViewState[“sortMode”]).Trim().Equals(SORT_ASC))
{
ViewState[“sortMode”] = SORT_DESC;
}
else
{
ViewState[“sortMode”] = SORT_ASC;
}
}

string sortexpr = e.SortExpression;

DataView dvProd = default(DataView);
DataTable dtProd = new DataTable();
dtProd = (DataTable)ViewState[“dtCountry”];

if ((dtProd != null))
{
if (dtProd.Rows.Count > 0)
{
dvProd = dtProd.DefaultView;
dvProd.Sort = sortexpr.Trim() + ” ” + ViewState[“sortMode”];
dgCountry.DataSource = dvProd;
dgCountry.DataBind();
}
}
}

Note : You have to sorting property true of gridview on aspx page.