Sunday, June 29, 2008

Accessing the windows Identity

Add the Namespace
"system.directoryServices;"
"using System.Web.Security;"


Code Logic:
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
string[] a = Context.User.Identity.Name.Split('\\');
System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
string Name = ADEntry.Properties["FullName"].Value.ToString();
Response.Write(" + Name + "");

Calendar Control customization in Visual Studio .NET (C#) code

This is all about Calendar control in .NET technology, visual Studio .Net c# (csharp) code. This can be applicable for both windows and web development.


The following code example demonstrates how to specify and code a handler for the DayRender event to make the background color yellow for the days in the displayed month. It also demonstrates how to customize the contents of a cell by adding a System.Web.UI: LiteralControl control to the cell.


void DayRender(Object source, DayRenderEventArgs e)
{
// Change the background color of the days in the month to yellow.
if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
e.Cell.BackColor=System.Drawing.Color.Yellow;
// Add custom text to cell in the Calendar control.
if (e.Day.Date.Day == 18)
e.Cell.Controls.Add(new LiteralControl("<br />Holiday"));
}


HTML code block:



<form id="form1" runat="server">
<h3>DayRender Event Example</h3>
<asp:calendar id="calendar1" runat="server" ondayrender="DayRender">
<weekenddaystyle backcolor="gray"></weekenddaystyle>
</asp:calendar>
</form>



C# code behind Calendar event:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
// Display vacation dates in yellow boxes with purple borders.
Style vacationStyle = new Style();
vacationStyle.BackColor = System.Drawing.Color.Yellow;
vacationStyle.BorderColor = System.Drawing.Color.Purple;
vacationStyle.BorderWidth = 3;

// Display weekend dates in green boxes.
Style weekendStyle = new Style();
weekendStyle.BackColor = System.Drawing.Color.Green;


if ((e.Day.Date >= new DateTime(2000,11,23)) && (e.Day.Date <= new DateTime(2000,11,30)))
{
// Apply the vacation style to the vacation dates.
e.Cell.ApplyStyle(vacationStyle);
}
else if (e.Day.IsWeekend)
{
// Apply the weekend style to the weekend dates.
e.Cell.ApplyStyle(weekendStyle);
}
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DateTime myAppointment = new DateTime(2005, 10, 1);
if (e.Day.Date == myAppointment)
{
e.Day.IsSelectable = true;
}
else
{
e.Day.IsSelectable = false;
}
}


NOTE: This is an example code block which explains about disabling a particular date or a date range. And this code, which is really grabbed a little from a tutorial help forum site, is then modified for our requirement which is what for the blog is.

How to read EXCEL data using C#.NET?

Its about how to read a set of data or specific data from EXCEL file using C#.Net. Infact, we can use SQL querying in the EXCEL file data itself.

Step-1: Include the connection string for the EXCEL file containing the filename and Provider settings.

String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("Filename.xls") + ";" + "Extended Properties=Excel 8.0;";

Step-2: Create a new connection object and open it for processing.

OleDbConnection objConn = newOleDbConnection(connectionString);
objConn.Open();

Step-3: Create a command object for querying and pass that command with the connection object created.

String strConString = "SELECT firstColumnName FROM [Sheet1$]";
//where date = CDate('" + DateTime.Today.ToShortDateString() + "')";

OleDbCommand objCmdSelect = newOleDbCommand(strConString, objConn);

Step-4: Here, in this example i am going to use DataSet for reading and holding the data, for which i am creating a Data Adapter Object.

// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapterobjAdapter1 = newOleDbDataAdapter();
// Pass the Select command to the adapter.

objAdapter1.SelectCommand = objCmdSelect;
// Create new DataSet to hold information from the worksheet.

DataSetobjDataset1 = newDataSet();


Step-5: Now, fill the data from the EXCEL file by querying with the command object and store them into the Dataset created.

// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "ExcelData");

Step-6: Process the data for displaying with the Dataset, which you can set as datasource for many ASP.NET controls.

for (int i = 0; i < objDataset1.Tables[0].Rows.Count; i++)
{
document.Write("<B>Data: </b>" + objDataset1.Tables[0].Rows[i].ItemArray[0].ToString()+"<BR>");

}

Step-7: As we are at the end, important thing is to Close the connection.

// Clean up objects.
objConn.Close();

This is a simple Data reading from an EXCEL file, which doesnt classify the name for the columns to read about. We provide or create an EXCEL file having the Column name as the first row of data.

So our example may contain that as the column name and the sheet name as the table name.

NOTE: See the usage of Sheetname with "[" and "]" for specifying the query syntax properly.

Saturday, June 28, 2008

How to zip a file using Java code?

Step-1:
We can create zip archives for files and folder contents using Java. All we need to do is to import “java.util.zip” namespace.

Step-2:
Then have to use the “ZipOutputStream” class and creating an object of that class will help us create a zip stream for creating the zip archive.

Step-3:
Then we need to read the files that are to be zipped in the byte stream format.

byte[] b = new byte[ (int)(cpFile.length()) ];
FileInputStream cpFileInputStream = new FileInputStream (cpFile);
cpFileInputStream.read(b, 0, (int)cpFile.length());



Step-4:
Now, we need to add the file to the zip archive as like writing into a file of type zipStream.


ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
cpZipOutputStream.putNextEntry(cpZipEntry);
cpZipOutputStream.write(b, 0, (int)cpFile.length());
cpZipOutputStream.closeEntry();


How to zip a folder now?
Now, we need to identify the source file type. For which we need to use “java.io” namespace.

Step-5:
Then to check the type of the source parameter, we can use like,

File cpFile = new File (strSource);
if(cpFile.isAbsolute())

System.out.println(strSource+ "is a absolute file");
else if(cpFile.isDirectory())

System.out.println(strSource+ "is a directory");
else if(cpFile.isFile())

System.out.println(strSource+ "is a simply file");
else if(cpFile.isHidden())

System.out.println(strSource+ "is a hidden file");
else
{
System.out.println("\nSource file/directory '"+strSource+ "'Not Found!");
return;
}

To check whether it is a directory
Step-5(a):
cpFile.isDirectory() returns a Boolean TRUE/FALSE value from which we can go ahead with the process.

Thats it! We've done. But this is not a complete part splitted. You can add your code part to add more spices for the zipped item to achieve.
Powered By Blogger