Wednesday, July 23, 2008

Convert a string to Proper Case

Convert a string to Proper Case
===========================

// Use this Namespace
using System.Globalization;

// Code
string myString = "thIS is tHE sAmple tExt to shoW tHIs examPle !!";

TextInfo TI = new CultureInfo("en-US",false).TextInfo;

Response.Write (TI.ToTitleCase( myString ));

Use ICallbackEventHandler in Asp.Net 2.0

Use ICallbackEventHandler in Asp.Net 2.0

==================================



For Example, you have page Layout is like this :


































Steps to Implement ICallbackEventHanler:

-------------------------------------------------



1. First define string variable in Common section of your code behind (.cs) file.



string _CallBackString;



2. Inherit ICallbackEventHandler in your page.



public partial class Customize : System.Web.UI.Page, ICallbackEventHandler



3. Now, write this code in your Page_Load() Event



ClientScriptManager cs = Page.ClientScript;



string cbRef = cs.GetCallbackEventReference(this, "arg", "ShowPop", "context");

string cbScript = "function CallPopBack(arg, context){" + cbRef + ";}";

cs.RegisterClientScriptBlock(this.GetType(), "CallPopBack", cbScript, true);



4. Now, Write some javascript code, in your design section of your page.



function GetPop()

{

var justExample = 'Hi..All..';



CallPopBack(justExample)

}

function ShowPop(result, context)

{



var strResult = new String();

strResult = result;



alert(result);

}



4. Now, In your code behind (.cs) file, write code for your EventHandle Method.



public string GetCallbackResult()

{

return _CallBackString;

}



public void RaiseCallbackEvent(string eventArgument)

{

_CallBackString = eventArgument + " " + DateTime.Now.ToShortTimeString();

}



5. Run the page and test this page.





Hope you will like it.

Check all checkbox within GridView or DataGrid in Asp.Net

Check all checkbox within GridView or DataGrid in Asp.Net
===================================================

Sometimes we need to handle functionality in our asp.net pages, like when a header
checkbox become selected, all checkbox within a GridView or DataGrid should become selected.
So here, this is the way, by it you can handle this types of functionality.

Suppose your page GridView layout is like:
-----------------------------------------------------
















Use this script to handle checkbox functionalities.
---------------------------------------------------------------



That's it !
Hope you will like it.

Check all checkbox within GridView or DataGrid in Asp.Net

Check all checkbox within GridView or DataGrid in Asp.Net
===================================================

Sometimes we need to handle functionality in our asp.net pages, like when a header
checkbox become selected, all checkbox within a GridView or DataGrid should become selected.
So here, this is the way, by it you can handle this types of functionality.

Suppose your page GridView layout is like:
-----------------------------------------------------
















Use this script to handle checkbox functionalities.
---------------------------------------------------------------



That's it !
Hope you will like it.

Use Custom paging for DataList, GridView in Asp.Net

Use Custom paging for Datalist, GridView in Asp.Net
=============================================

Suppose your HTML layout is like;
------------------------------------------

// Stylesheet
/* Start Pager 2 style */
.Pager2 { border-collapse:collapse;}
.Pager2 a { color:#0080C0; font-weight:bold; margin:1px; padding:2px 5px; border:1px solid white; text-decoration:none }
.Pager2 a:hover { color:White; font-weight:bold; border:1px #0080C0 solid; background-color:#0080C0 }
.Pager2 span { margin:1px; padding:2px 5px; background-color:#0080C0; color:White; border:1px #0080C0 solid}
/* End Pager 2 style */

// Page HTML layout

Name :


















ID :<%#Eval("ID").ToString() %>
Name<%#Eval("Name").ToString() %>
Date<%#Eval("RegisterDate").ToString() %>








// Javascript Method


// Code Behind Part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}

private void BindGrid()
{
Company objCompany = new Company();

objCompany.name = txtName.Text.Trim();
objCompany.status = 3;
objCompany.SortBy = "Name";
objCompany.SortOrder = SortDirection.Ascending;
objCompany.PageNo = Request.QueryString["Page"] != null ? int.Parse(Request.QueryString["Page"].ToString()) - 1 : 0;
objCompany.PageSize = 5;

DataSet dsCompanyList = objCompany.GetCompanyRegisterListAll();

if (dsCompanyList != null && dsCompanyList.Tables[0].Rows.Count > 0)
{
dlCompanylist.DataSource = dsCompanyList.Tables[0];
dlCompanylist.DataBind();

ltPaging.Text = this.Paging(objCompany.PageNo + 1, int.Parse(dsCompanyList.Tables[1].Rows[0][0].ToString()), objCompany.PageSize);
}
}


private string Paging(int Page, int TotalRecords, int PageSize)
{
int PageNo = 0;
string NextPage = "";
string PreviousPage = "";
string Print = "";
string Range = "";
string Pager = "";

int Start = 0;
int StartRange = 0;
int EndRange = 0;

if (TotalRecords > PageSize)
{
double RecordForPaging = Math.Ceiling((Convert.ToDouble(TotalRecords) / Convert.ToDouble(PageSize)));
double RecordPage, v;
int NewNo;

if (RecordForPaging > Math.Floor(RecordForPaging))
{
RecordPage = (Math.Floor(RecordForPaging)) + 1;
}
else
{
RecordPage = RecordForPaging;
}

if (RecordPage <= PageSize)
v = RecordPage;
else
v = 5;

if (Page != 1)
PreviousPage = "";
else
PreviousPage = "";

if (Page != RecordPage)
NextPage = "";

Print = "";

if (Page == 1)
{
for (PageNo = 1; PageNo <= v; PageNo++)
{
if (RecordPage >= PageNo)
{
if (PageNo == Page)
{
Print += " " + PageNo + "";
}
else
{
Print += " " + PageNo + "";
}
}
}
}
else if (Page <= RecordPage)
{
if (PageNo <= RecordPage)
NewNo = 2;
else
NewNo = Page - 5;

if (PageNo <= RecordPage)
NewNo = Page - 5;


for (PageNo = NewNo; PageNo <= Page + 5; PageNo++)
{
if (PageNo > 0)
{
if (PageNo == Page)
Print += " " + PageNo + "";
else
{
if (PageNo <= RecordPage)
Print += " " + PageNo + "";
}
}
}
}

Start = (Page - 1) * PageSize;
StartRange = Start + 1;
EndRange = Start + PageSize;

if (EndRange >= TotalRecords)
EndRange = TotalRecords; //end display
Range = StartRange + "-" + EndRange + " of " + TotalRecords;
Pager = "";
Pager += "
";
Pager += "";
Pager += "
" + PreviousPage + "
" + Print + "
" + NextPage + "
" + Range + "
";
return Pager;
}

return string.Empty;
}


That's It !
Hope you will like it.

Remove HTML string - tag from specified string

Remove HTML string - tag from specified string.
========================================

using System.Text.RegularExpressions;

public static string RemoveHtml(string strSource)
{

string pattern = @"<(.|\n)*?>";

strSource = Regex.Replace(strSource, pattern, string.Empty);


return strSource;
}

That's It !!
Hope you will like it.

Remove HTML string - tag from specified string

Remove HTML string - tag from specified string.
========================================

using System.Text.RegularExpressions;

public static string RemoveHtml(string strSource)
{

string pattern = @"<(.|\n)*?>";

strSource = Regex.Replace(strSource, pattern, string.Empty);


return strSource;
}

That's It !!
Hope you will like it.

What is Side-by-Side Execution in .Net Framework ?

Side-by-Side Execution in .Net Framework
===================================

Using Side-by-Side Execution
-----------------------------------

Side-by-side execution is the ability to install multiple versions of code so that an application can choose which version of the common language runtime or of a component it uses. Subsequent installations of other versions of the runtime, an application, or a component will not affect applications already installed.

Side-by-Side Execution Fundamentals
--------------------------------------------

The way that the common language runtime manages side-by-side execution depends on the type of application you are running. This section applies to all managed executable applications, such as Windows Forms, and executable applications installed locally through an Internet browser. ASP.NET applications and XML Web services, as well as other hosted applications, have their own method of side-by-side execution.

What is Side-by-Side Execution in .Net Framework ?

Side-by-Side Execution in .Net Framework
===================================

Using Side-by-Side Execution
-----------------------------------

Side-by-side execution is the ability to install multiple versions of code so that an application can choose which version of the common language runtime or of a component it uses. Subsequent installations of other versions of the runtime, an application, or a component will not affect applications already installed.

Side-by-Side Execution Fundamentals
--------------------------------------------

The way that the common language runtime manages side-by-side execution depends on the type of application you are running. This section applies to all managed executable applications, such as Windows Forms, and executable applications installed locally through an Internet browser. ASP.NET applications and XML Web services, as well as other hosted applications, have their own method of side-by-side execution.

Thursday, June 12, 2008

Use Dynamic stylesheet class for messages in Asp.Net

Hi,

In the web application, some times we need to use a message style for
only one Label, like, suppose if our data has been added successfully, we display
message like 'Records has been added successfully'. So, depend on
system's different situation, we have to display label message with style color
combination.

So, Here you can find the solution. By it, you can display your message
with sytle as per your system's situation.

Just copy and paste the style script and copy C# method in your page.

You can find it here...



(Download these images for stylesheet)








protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "Records has been Added Successfully.";
this.SetStyle(lblMessage, MessageType.Info);
}

private void SetStyle(Label objLabel, MessageType msgType)
{
if (string.IsNullOrEmpty(objLabel.Text.Trim()))
objLabel.CssClass = "";
else
{
switch (msgType)
{
case MessageType.Error:
objLabel.CssClass = "MessageError";
break;
case MessageType.Info:
objLabel.CssClass = "MessageInfo";
break;
case MessageType.Success:
objLabel.CssClass = "MessageSuccess";
break;
}
}
}

// Define enum in the outer side of your page class.
public enum MessageType
{
Success,
Info,
Error
}

Now, Run your page and check it.

That's it !
Hope you will like it.

Create and Export-Import Excel file in Asp.Net

Create and Export-Import Excel file in Asp.Net
=======================================


// This method create an Excel file and export it for download
private void CreateExcelFileandDownload()
{

try
{
// Create a new Excel file.

string[] connectStrings = new string[] {
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\TEMP\\TestExcel2003Output.xls\";Extended Properties=\"Excel 8.0;HDR=Yes;\";",
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\TEMP\\TestExcel2007Output.xlsx\";Extended Properties=\"Excel 12.0 Xml;HDR=Yes;\";"
};

string dropTableStmt = "DROP TABLE [test]";
string createTableStmt = "CREATE TABLE [test] ( [Integer] int, [String] varchar(40), [Double] float, [Date] datetime, [Boolean] bit )";
string insertStmt = "INSERT INTO [test] ([Integer], [String], [Double], [Date], [Boolean]) VALUES ({0}, '{1}', {2}, '{3}', {4})";
object[] data = new object[] {

new object[] { 2628013, "Anderson", 0.617715356, new DateTime( 2008, 5, 5 ), true },

new object[] { 2628015, "Rainaud", 0.64933168, new DateTime( 2007, 4, 10 ), false },

new object[] { 2628017, "Dennis", 0.62140731, new DateTime( 2006, 3, 15 ), true },

new object[] { 2628019, "Schoenster", 0.599058708, new DateTime( 2005, 2, 20 ), false },

new object[] { 2628041, "Ganun", 0.593402527, new DateTime( 2004, 1, 25 ), true }

};

foreach (string connect in connectStrings)
{
OleDbConnection con = new OleDbConnection(connect);
con.Open();
if (con.State == ConnectionState.Open)
{
OleDbCommand cmd = con.CreateCommand();
cmd.CommandTimeout = 0;
try
{
// Only need this on runs subsequent to first time
cmd.CommandText = dropTableStmt;
cmd.ExecuteNonQuery();
}
catch
{
// First run will cause exception because table (worksheet) doesn't exist
}

cmd.CommandText = createTableStmt;
cmd.ExecuteNonQuery();
foreach (object[] row in data)
{
cmd.CommandText = String.Format(insertStmt, row[0], row[1], row[2], row[3], row[4]);
cmd.ExecuteNonQuery();
}

cmd.Dispose();
if (con.State == ConnectionState.Open)
con.Close();
con.Dispose();
}
}

// Download Created File

// For Office 2007 format
string FileName = @"C:\TEMP\TestExcel2007Output.xlsx";
// For Office 97 - 2003 format
string FileName2 = @"C:\TEMP\TestExcel2003Output.xls";

Response.Clear();
Response.ClearContent();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("Content-Disposition", "attachment; filename=Name.xlsx;");

byte[] buffer = System.IO.File.ReadAllBytes(FileName);

System.IO.MemoryStream mem = new System.IO.MemoryStream();
mem.Write(buffer, 0, buffer.Length);

mem.WriteTo(Response.OutputStream);
Response.End();
}
catch (Exception ex)
{
// throw an exception
}

}

Tuesday, June 10, 2008

SW DEVELOPER'S PROFILE

About me: I think I am changing the world, but I am not. I think I am contributing to the Indian economy, but I guess I am not. I think I love my work, but I do not. I think I hate all people who made me earn my engineering degree, and I do. I think I am living, but and most importantly, I am LOOKING for someone Ok...I won't be funny anymore. I am a cool guy with a zeal to enjoy life (If you know me--> "Just stop laughing"

Birthday : The day my PL is about to fire me.

Age : 10111

Here for : web browsing in company hours.

Children : can't be (hey, don't get me wrong here)

Languages I speak : Java, C/C++, 010101110101

Religion: I get holidays on all religious festivals, so I love all religions.

Political view : the guy sitting beside me is a pig

Humor : weekly.

Fashion: Ask my company HR. Btw, I like jeans, t-shirt and a cross-bag.

Smoking: The second greatest pleasure on the earth.

Drinking : The first is this.

Pets: Yeah, my PL looks like a dog.

Living: Common, this is a stupid one. How can this be asked to a software engineer? Believe me, I am living

Hometown : My company (Oh God! Please bring my appraiser to this page)

Webpage: http://naukri.com, http://jobsahead.comß- Isnt it Ultimate?

Passions: searching for the cheapest pub around, cursing my company, looking for other company, remembering my good old college days, worrying about my future.

Sports: quake, CS (Counter Strike), computer chess.

Books : "How to lose weight in 20 days?", "How to live a happy life?", "101 ways to attract a girl", "Java Unleashed", "C++ at your footsteps", Others censored.

Music: Metallica, Pink Floyd, Nirvana, ACDC,BSB and anything depressing.

Tv shows : can't afford one.

Cuisines: Bread Butter, Maggi, anything available within 200 meteres of Home

Regards,
MILIND KANSAGARA.

SW DEVELOPER'S PROFILE

About me: I think I am changing the world, but I am not. I think I am contributing to the Indian economy, but I guess I am not. I think I love my work, but I do not. I think I hate all people who made me earn my engineering degree, and I do. I think I am living, but and most importantly, I am LOOKING for someone Ok...I won't be funny anymore. I am a cool guy with a zeal to enjoy life (If you know me--> "Just stop laughing"

Birthday : The day my PL is about to fire me.

Age : 10111

Here for : web browsing in company hours.

Children : can't be (hey, don't get me wrong here)

Languages I speak : Java, C/C++, 010101110101

Religion: I get holidays on all religious festivals, so I love all religions.

Political view : the guy sitting beside me is a pig

Humor : weekly.

Fashion: Ask my company HR. Btw, I like jeans, t-shirt and a cross-bag.

Smoking: The second greatest pleasure on the earth.

Drinking : The first is this.

Pets: Yeah, my PL looks like a dog.

Living: Common, this is a stupid one. How can this be asked to a software engineer? Believe me, I am living

Hometown : My company (Oh God! Please bring my appraiser to this page)

Webpage: http://naukri.com, http://jobsahead.comß- Isnt it Ultimate?

Passions: searching for the cheapest pub around, cursing my company, looking for other company, remembering my good old college days, worrying about my future.

Sports: quake, CS (Counter Strike), computer chess.

Books : "How to lose weight in 20 days?", "How to live a happy life?", "101 ways to attract a girl", "Java Unleashed", "C++ at your footsteps", Others censored.

Music: Metallica, Pink Floyd, Nirvana, ACDC,BSB and anything depressing.

Tv shows : can't afford one.

Cuisines: Bread Butter, Maggi, anything available within 200 meteres of Home

Regards,
MILIND KANSAGARA.