Saturday, September 26, 2009

Send Mail through Gmail Accoun [ Asp.net C# ]

// Name Space

using System;

using System.Net;

using System.Net.Mail;

//Function

private void MailSendMessage()

{

try

{

NetworkCredential loginInfo = new NetworkCredential("username@domain.com", "password");

MailMessage msg = new MailMessage();

msg.From = new MailAddress("from@domain.com","DisplayName");

msg.To.Add(new MailAddress("to@domain.com"));

msg.Subject = "Test";

msg.Body = "Test";

msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.com");

client.Port = 587;

client.EnableSsl = true;

client.UseDefaultCredentials = false;

client.Credentials = loginInfo;

client.Send(msg);

}

catch (Exception ex)

{

Response.Write("
[1] Message :"
+ ex.Message.ToString());

Response.Write("
[2] Stack Trace :"
+ ex.StackTrace.ToString());

}

}

Thursday, May 7, 2009

ASP.NET 1.1 vs 2.0

he latest version of ASP.NET, 2.0, is more than an upgrade—it is a major update of the technology, as it changes the way ASP.NET applications are designed, compiled, and deployed. Microsoft does recognize the existence of a large number of ASP.NET 1.1 installations, so 2.0 was designed to allow older applications to work without problems. On the other hand, the need to convert existing applications to the 2.0 platform may arise. With that said, let's examine the major differences between the versions, as well as areas that may be problems during the conversion process.Version changesWhile there are many changes between ASP.NET 1.1 and 2.0, there are certain ones that have a greater impact of
  • Code-behind model: ASP.NET 2.0 introduces the concept of partial classes, whereas a class can span multiple class files. It is a key feature used in Visual Studio 2005 to separate system-generated code from user code. This negates the scenario where you find yourself scrolling through source code and encountering the code generated by the system. The partial class approach reduces the risk of inadvertently editing system code and results in smaller code-behind files.
  • Directories: ASP.NET 2.0 has added a number of special directories where 1.1 had only the one required bin directory. Most of these new directories have the App_ prefix with names like App_Code, App_Data, App_GlobalResources, and App_LocalResources, while the bin directory still exists along with a themes directory. The new directories are a key aspect of 2.0's elimination of project files.
  • No more project files: Gone are the days of project files with ASP.NET and Visual Studio 2005. The project is now the complete Web project directory, which is a drastic shift from 1.1 and can cause migration issues as described later.
  • Compilation model: With 1.1, all code was compiled into one assembly placed in the bin directory. With 2.0, the assembly is separated into multiple assemblies. These multiple assemblies may be created on-the-fly or precompiled. Examples of multiple assemblies are one assembly for each ASP.NET directory like App_Code and App_Data as well as individual assemblies for Web Forms, User Controls, and so forth. This is a major shift in the application structure; it offers more deployment options in how the application is delivered to the users.
  • Application deployment: The deployment of 1.1 applications was fairly straightforward as you moved the content files along with the necessary assembly files—it could easily be accomplished with a simple copy and paste with limited options. The 2.0 model provides various options for deploying an application. For instance, you may choose to precompile all code and deploy it or go the other way with no precompilation. Also, you can lock down deployed content files so no changes may be made after deployment (this is a major difference from 1.1).

The list provides a high-level view of the main differences between ASP.NET 1.1 and 2.0 and what can affect the conversion of an existing application to 2.0. Now let's take a closer look at possible problem areas.

Friday, May 1, 2009

Remove extra spaces from string using regular expression, asp.net, c#

public string RemoveExtaSpaces(string text)

{

Regex regex = new Regex(@”\s{2,}”, Options);


text = regex.Replace(text.Trim(), ” “); //This line removes extra spaces and make space exactly one.

//To remove the space between the end of a word and a punctuation mark used in the text we will

//be using following line of code


regex=new Regex(@”\s(\!|\.|\?|\;|\,|\:)”); // “\s” whill check for space near all puntuation marks in side ( \!|\.|\?|\;|\,|\:)”); )

text = regex.Replace(text, “$1″);

return text;


}

Thursday, April 30, 2009

Merge DataGrid Header



Introduction

This article describes the technique to merge the header of a DataGrid by redirecting the Render method of the DataGrid items.

Background

I found many times the need to merge the header of a DataGrid. So, when I was having spare time, I tried to find a way to do it, and here it is. I know that if you need to merge headers, you can use the Repeater control instead. But if you are fond of DataGrid (just like me), or may be you already used DataGrid, then this article is for you.

Using the code

When rendered, a DataGrid will be converted into a HTML Table element and the header will be the first HTML TR element. So, to have a merged header, we need to have control in the rendering of the header. This can be achieved by redirecting the Render method of the DataGrid header using the SetRenderMethodDelegate of the DataGrid header on ItemCreated event, like this


private void Datagrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//*** Examine if the item created is the header item

ListItemType lit = e.Item.ItemType;
if(ListItemType.Header == lit)
{
//*** Redirect the default header rendering method to our own method

e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
}
}

And here is our own Render method:

/// <summary>

/// This is our custom render method for the grid header item

/// </summary>

/// <param name="writer"></param>

/// <param name="ctl"></param>

private void NewRenderMethod(HtmlTextWriter writer, Control ctl)
{
//*** We don't need to write the tag

// because it's already written by the writer

// so now we write the Name column

writer.Write("Name\n");

//*** The Age column must have the rowspan attribute

// and must be rendered inside the

// first row so it will centered vertically

TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count-1];
cell.Attributes.Add("rowspan","2");
cell.RenderControl(writer);

//*** Now we close the first row, so we can insert the second one

writer.Write("\n");

//*** Add the style attributes that was defined in design time

// to our second row so they both will have the same appearance

DataGrid1.HeaderStyle.AddAttributesToRender(writer);

//*** Insert the second row

writer.RenderBeginTag("TR");

//*** Render all the cells that was defined

// in design time, except the last one

// because we already rendered it above

for(int i=0; i<= ctl.Controls.Count-2; i++)
{
ctl.Controls[i].RenderControl(writer);
}

//*** We don't need to write the close tag

// because the writer will do that for us

// and so we're done :)

}

I have created a decorator class to decorate a DataGrid (ASPNetDatagridDecorator class) to have a merge header, and all you need to do is define the header cell like this (you can use the auto format feature, but doesn't work for all):


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

if(!this.IsPostBack)
{
TableCell cell = null;
DataGrid1.DataSource = GetData();
DataGrid1.DataBind();

m_add.DatagridToDecorate = Datagrid2;
ArrayList header = new ArrayList();

// cell = new TableCell();

// cell.Text = "Code";

// cell.RowSpan = 2;

// cell.HorizontalAlign = HorizontalAlign.Center;

// header.Add(cell);


cell = new TableCell();
cell.Text = "Name";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);

cell = new TableCell();
cell.Text = "Name";
cell.ColumnSpan = 3;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);

cell = new TableCell();
cell.Text = "Age";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);

cell = new TableCell();
cell.Text = "School";
cell.ColumnSpan = 3;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);

cell = new TableCell();
cell.Text = "Religion";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);

m_add.AddMergeHeader(header);

Datagrid2.DataSource = GetData();
Datagrid2.DataBind();

}

}

By Courtesy of Codeproject

Merge Header - Gridview/DataGrid


Introduction

During development with GridView, we might come across many situations in which we need to extend GridView for our requirements. For example, we need to have a separate header other than the header provided by GridView. In that case we need to add new GridView item of header type. In this article we will see how merge two or more columns.

Requirement

In this example we have simple GridView which is fetching data from xml file and displaying that in the table structure using GridView. In this GridView, we need to add two additional Header with text "Department" and "Employee". First department column should occupy first two columns and Employee column should occupy rest three columns.

Problem

I found in the internet that there are many ways to do this, but we might end up with column alignment problems.

Solution

Here we are going to see one of the best method to do that. To have additional header, we need to add one GridViewRow of header type to GridView inside OnRowCreated event method. The code snippet for doing this,

protected void GridView_Merge_Header_RowCreated(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 = "Department";
oTableCell.ColumnSpan = 2;
oGridViewRow.Cells.Add(oTableCell);

//Add Employee
oTableCell = new TableCell();
oTableCell.Text = "Employee";
oTableCell.ColumnSpan = 3;
oGridViewRow.Cells.Add(oTableCell);
oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
}
}

By Courtesy of Codeproject