Thursday, November 29, 2007

Merge Grid View Header in Asp.Net 2.0 (C#)

To merge headers of a grid view in .NET 2.0
let take a grid view and bound it's column to some data fields on .aspx page
and add event OnRowDataBound="gvEmployee_RowDataBound" to your grid view.
Now in code behind page
just make the method for "RowDataBound" ie
protected void
gvEmployee_RowDataBound(Object sender,GridViewRowEventArgs e) this method always get executed when a grid.bind() method called any where in your code.
So here it is

protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.Header)
{
//Build custom header.
GridView oGridView = (GridView)sender;
GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell oTableCell = new TableCell();

//Add Department
oTableCell.Text = "Header Column1 name what ever you want";
oTableCell.ColumnSpan = 2;
oGridViewRow.Cells.Add(oTableCell);

//Add Employee
oTableCell = new TableCell();
oTableCell.Text = "
Header Column2 name what ever you want";
oTableCell.ColumnSpan = 3;
oGridViewRow.Cells.Add(oTableCell);
oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
}

}
Hence in the code as you can see that there are two column is being created as per your intrest
you can add your header .
I hope you will enjoy this code

Tuesday, November 27, 2007

Monday, November 26, 2007

Application Security Vulnerabilities in Web.config Files

As in devlopment scenario at very first we measue our security issue and of course we implement few stratigies to secure our application..

So you can get few nice tips from below linkz......

1- http://www.codeproject.com/useritems/web-based-applications.asp
2- http://www.codeproject.com/useritems/application-security.asp

Hope you will enjoy it....

How you can Improve .Net Application Performence

Here are few nice stuff provided by MicroSoft to improve your application's performance and scalability.....

http://msdn2.microsoft.com/en-us/library/ms998530.aspx

also you can see from here....
http://www.codeproject.com/aspnet/ASPNET_Best_Practices.asp

Cool grafix stuff

Do you really wanna see the great effort made by Design Executive "DINESH".....
Ok then check it out:
Graphics WorkStation

The above link will provide idias for the persons who are intended make new Graphics
with extra ordinary talent.
Hope you will enjoy this :)

Friday, November 23, 2007

DateTime enumarable class (C#)

In .Net there is no kind of static properties in DateTime Class by which you can get
1- Last Week of Month
2- First week of month
3-Current week of Month and many more property

Here in few days I was looking for a Class that may help for above kind of topics

So, here it is...........

using System;
namespace dateTime.Utilities
{
///


/// Common DateTime Methods.
///

///

public enum Quarter
{
First = 1,
Second = 2,
Third = 3,
Fourth = 4
}

public enum Month
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}

public class DateUtilities
{
#region Quarters

public static DateTime GetStartOfQuarter( int Year, Quarter Qtr )
{
if( Qtr == Quarter.First ) // 1st Quarter = January 1 to March 31
return new DateTime( Year, 1, 1, 0, 0, 0, 0 );
else if( Qtr == Quarter.Second ) // 2nd Quarter = April 1 to June 30
return new DateTime( Year, 4, 1, 0, 0, 0, 0 );
else if( Qtr == Quarter.Third ) // 3rd Quarter = July 1 to September 30
return new DateTime( Year, 7, 1, 0, 0, 0, 0 );
else // 4th Quarter = October 1 to December 31
return new DateTime( Year, 10, 1, 0, 0, 0, 0 );
}

public static DateTime GetEndOfQuarter( int Year, Quarter Qtr )
{
if( Qtr == Quarter.First ) // 1st Quarter = January 1 to March 31
return new DateTime( Year, 3,
DateTime.DaysInMonth( Year, 3 ), 23, 59, 59, 999 );
else if( Qtr == Quarter.Second ) // 2nd Quarter = April 1 to June 30
return new DateTime( Year, 6,
DateTime.DaysInMonth( Year, 6 ), 23, 59, 59, 999 );
else if( Qtr == Quarter.Third ) // 3rd Quarter = July 1 to September 30
return new DateTime( Year, 9,
DateTime.DaysInMonth( Year, 9 ), 23, 59, 59, 999 );
else // 4th Quarter = October 1 to December 31
return new DateTime( Year, 12,
DateTime.DaysInMonth( Year, 12 ), 23, 59, 59, 999 );
}

public static Quarter GetQuarter( Month Month )
{
if( Month <= Month.March ) // 1st Quarter = January 1 to March 31 return Quarter.First; else if( ( Month >= Month.April ) && ( Month <= Month.June ) ) // 2nd Quarter = April 1 to June 30 return Quarter.Second; else if( ( Month >= Month.July ) && ( Month <= Month.September ) )
// 3rd Quarter = July 1 to September 30
return Quarter.Third;
else // 4th Quarter = October 1 to December 31
return Quarter.Fourth;
}

public static DateTime GetEndOfLastQuarter()
{
if( (Month)DateTime.Now.Month <= Month.March )
//go to last quarter of previous year
return GetEndOfQuarter( DateTime.Now.Year - 1, Quarter.Fourth);
else //return last quarter of current year
return GetEndOfQuarter( DateTime.Now.Year,
GetQuarter( (Month)DateTime.Now.Month));
}

public static DateTime GetStartOfLastQuarter()
{
if( (Month)DateTime.Now.Month <= Month.March )
//go to last quarter of previous year
return GetStartOfQuarter( DateTime.Now.Year - 1, Quarter.Fourth);
else //return last quarter of current year
return GetStartOfQuarter( DateTime.Now.Year,
GetQuarter( (Month)DateTime.Now.Month));
}

public static DateTime GetStartOfCurrentQuarter()
{
return GetStartOfQuarter( DateTime.Now.Year,
GetQuarter( (Month)DateTime.Now.Month ));
}

public static DateTime GetEndOfCurrentQuarter()
{
return GetEndOfQuarter( DateTime.Now.Year,
GetQuarter( (Month)DateTime.Now.Month ));
}
#endregion

#region Weeks
public static DateTime GetStartOfLastWeek()
{
int DaysToSubtract = (int)DateTime.Now.DayOfWeek + 7;
DateTime dt =
DateTime.Now.Subtract(System.TimeSpan.FromDays( DaysToSubtract ) );
return new DateTime( dt.Year, dt.Month, dt.Day, 0, 0, 0, 0 );
}

public static DateTime GetEndOfLastWeek()
{
DateTime dt = GetStartOfLastWeek().AddDays(6);
return new DateTime( dt.Year, dt.Month, dt.Day, 23, 59, 59, 999 );
}

public static DateTime GetStartOfCurrentWeek()
{
int DaysToSubtract = (int)DateTime.Now.DayOfWeek ;
DateTime dt =
DateTime.Now.Subtract( System.TimeSpan.FromDays( DaysToSubtract ) );
return new DateTime( dt.Year, dt.Month, dt.Day, 0, 0, 0, 0 );
}

public static DateTime GetEndOfCurrentWeek()
{
DateTime dt = GetStartOfCurrentWeek().AddDays(6);
return new DateTime( dt.Year, dt.Month, dt.Day, 23, 59, 59, 999 );
}
#endregion

#region Months

public static DateTime GetStartOfMonth( Month Month, int Year )
{
return new DateTime( Year, (int)Month, 1, 0, 0, 0, 0 );
}

public static DateTime GetEndOfMonth( Month Month, int Year )
{
return new DateTime( Year, (int)Month,
DateTime.DaysInMonth( Year, (int)Month ), 23, 59, 59, 999 );
}

public static DateTime GetStartOfLastMonth()
{
if( DateTime.Now.Month == 1 )
return GetStartOfMonth( 12, DateTime.Now.Year - 1);
else
return GetStartOfMonth( DateTime.Now.Month -1, DateTime.Now.Year );
}

public static DateTime GetEndOfLastMonth()
{
if( DateTime.Now.Month == 1 )
return GetEndOfMonth( 12, DateTime.Now.Year - 1);
else
return GetEndOfMonth( DateTime.Now.Month -1, DateTime.Now.Year );
}

public static DateTime GetStartOfCurrentMonth()
{
return GetStartOfMonth( DateTime.Now.Month, DateTime.Now.Year );
}

public static DateTime GetEndOfCurrentMonth()
{
return GetEndOfMonth( DateTime.Now.Month, DateTime.Now.Year );
}
#endregion

#region Years
public static DateTime GetStartOfYear( int Year )
{
return new DateTime( Year, 1, 1, 0, 0, 0, 0 );
}

public static DateTime GetEndOfYear( int Year )
{
return new DateTime( Year, 12,
DateTime.DaysInMonth( Year, 12 ), 23, 59, 59, 999 );
}

public static DateTime GetStartOfLastYear()
{
return GetStartOfYear( DateTime.Now.Year - 1 );
}

public static DateTime GetEndOfLastYear()
{
return GetEndOfYear( DateTime.Now.Year - 1 );
}

public static DateTime GetStartOfCurrentYear()
{
return GetStartOfYear( DateTime.Now.Year );
}

public static DateTime GetEndOfCurrentYear()
{
return GetEndOfYear( DateTime.Now.Year );
}
#endregion

#region Days
public static DateTime GetStartOfDay( DateTime date )
{
return new DateTime( date.Year, date.Month, date.Day, 0, 0, 0, 0 );
}

public static DateTime GetEndOfDay( DateTime date )
{
return new DateTime( date.Year, date.Month,
date.Day, 23, 59, 59, 999 );
}
#endregion
}
}

Just use this class structure , Don't hesitate to comment me if any.With more stuff I will be here for your help :-)

XPath and XmlDocument





Lets look at above example
If you want to select all of the price elements,
here is the code
/* Use This Code to read nodes Element and element value*/
using System.Xml;
using System.Xml.XPath;
....
string fileName = "data.xml";
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr;
expr = nav.Compile("/catalog/cd/price");
XPathNodeIterator iterator = nav.Select(expr);

// Iterate on the node set
listBox1.Items.Clear();
try
{
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
listBox1.Items.Add("price: " + nav2.Value);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}


In the above code "/catalog/cd/price" to select all the price elements.

If you just want to select all the cd elements with price greater than 10.0, you can use "/catalog/cd[price>10.0]"


. Here are some more examples of XPath expressions:

1- /catalog selects the root element
2-/catalog/cd selects all the cd elements of the catalog element
3-/catalog/cd/price selects all the price elements of all the cd elements of the catalog element
4-/catalog/cd[price>10.0] selects all the cd elements with price greater than 10.0
5-starts with a slash(/) represents an absolute path to an element
6-starts with two slashes(//) selects all elements that satisfy the criteria
7-//cd selects all cd elements in the document
8- /catalog/cd/title /catalog/cd/artist selects all the title and artist elements of the cd elements of catalog
9- //title //artist selects all the title and artist elements in the document
10-/catalog/cd/* selects all the child elements of all cd elements of the catalog element
11- /catalog/*/price selects all the price elements that are grandchildren of catalog
12- /*/*/price selects all price elements which have two ancestors
13-//* selects all elements in the document
14-/catalog/cd[1]selects the first cd child of catalog
15- /catalog/cd[last()] selects the last cd child of catalog
16- /catalog/cd[price] selects all the cd elements that have price
17-/catalog/cd[price=10.90] selects cd elements with the price of 10.90
18- /catalog/cd[price=10.90]/price selects all price elements with the price of 10.90
19- //@country selects all "country" attributes
20- //cd[@country] selects cd elements which have a "country" attribute
21- //cd[@*] selects cd elements which have any attribute
22- //cd[@country='UK']selects cd elements with "country" attribute equal to 'UK'

To update a cd node, first you have to decide that which node you
are updating by Node.SelectSingleNode("XPathExpression"), and then create a new cd element. After setting the InnerXml of the new node, call ReplaceChild method of XmlElement to update
the document. The code is as follows:

XmlTextReader reader = new XmlTextReader(FILE_NAME);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();

//Select the cd node with the matching title
XmlNode oldCd;
XmlElement root = doc.DocumentElement;
oldCd = root.SelectSingleNode("/catalog/cd[title='" + oldTitle + "']");

XmlElement newCd = doc.CreateElement("cd");
newCd.SetAttribute("country",country.Text);

" + this.comboBox1.Text + "newCd.InnerXml = "" +
"" + artist.Text + "" +
"" + price.Text + "";

root.ReplaceChild(newCd, oldCd);

//save the output to a file
doc.Save(FILE_NAME);


I Hope You will enjoy with this code :-)

XML and javascript


Writing XML with JS
The object is called XMLWriter. It automatically replaces invalid characters such as quotation marks or greater than signs with the appropriate XML values. However, it does not throw exceptions on invalid tag names, because the application I’m writing won’t have the possibility of producing invalid tag names. If you want to add tag-name validation to the object, it should not be a difficult task.
The XMLWriter object can be created with the JavaScript new command like so:
var XML = new XMLWriter();
The XMLWriter object has the following public methods:
BeginNode (Name)
EndNode ()
Attrib (Name, Value)
WriteString (Value)
Node (Name, Value)
Close ()
ToString ()
BeginNode writes out an opening tag, giving it the name that you pass the method. Below is an example, followed by the XML it would produce:
XML.BeginNode(“Foo”);
//Produces: ”. The object is smart enough to know if you have written any text or inner nodes out and will write “” if necessary.
Attrib writes out an attribute and value on the currently open node. Below is an example, followed by the XML it would produce:
XML.BeginNode(“Foo”);
XML.Attrib(“Bar”, “Some Value”);
XML.EndNode();
//Produces:
WriteString writes out a string value to the XML document as illustrated below:
XML.BeginNode(“Foo”);
XML.WriteString(“Hello World”);
XML.EndNode();
//Produces Hello World
The Node method writes out a named tag and value as illustrated below:
XML.Node(“MyNode”, “My Value”);
//Produces: My Value
The Close method does not necessarily need to be called, but it’s a good idea to do so. What it does is end any nodes that have not been ended.
Finally, the ToString method returns the entire XML document as a single string (duh).