Showing posts with label Tips and Tricks. Show all posts
Showing posts with label Tips and Tricks. Show all posts

Tuesday, July 7, 2009

RegEx.Split vs. String.Split

String.split has some limitations in the split. but in the Regex.split will split duplicate delimeter.

The delimeter such as ||, ~~ and ::

string[] output  = null;  string inputSentence = "I am a developer|| I work on .Net || " +     "The latest framework available is 3.5";  //the following line will not work 
output = inputSentence .Split("||".ToCharArray());  
//use RegEx Split instead 
output =  System.Text.RegularExpressions.Regex.Split(inputSentence, System.Text.RegularExpressions.Regex.Escape("||"));

Tuesday, June 23, 2009

Itextsharp

Itextsharp

I have come across this from one of my friend, I just thought of sharing with everyone. i am writing in my blog.

This is opensource library, Which will create the PDF on the fly. Basically its taken from java library. now purely written in the C#.NET

Everyone can use this library with .net framework 1.1 and 2.O


You can download the dll in the given URL

http://sourceforge.net/projects/itextsharp/

Monday, January 5, 2009

Hidden Features in C#

1) Using @ for variable that are keyword.

var @object = new object();
var @string = "";
var @if = IpsoFacto();

2) Aliased Generics.

using ASimpleName = Dictionary<string, Dictionary<string, List<string>>>;


Allows you to ASimpleName,instead of

Dictionary<string, Dictionary<string, List<string>>>;

Use it when you would use the same generic big long complex thing in a lot of places.

Thursday, May 29, 2008

Restart Http Service using Command prompt

Step1: open run commmand in windows
step2: type "net start http"
step3: end

Monday, April 21, 2008

Disable Copy and Paste for greater website security

<body bgcolor="#FFFFFF" ondragstart="return false" onselectstart="return false">

Sunday, April 20, 2008

An easy way to build connection string.

Though this in not related to .NET directly but it is useful while working with ADO.NET
1) Open a New notepad and save it with "udl" extension, suppose "New.udl".
2) Now you will see that it's icon is changed.
3) Open it, you will find Data Link properties dialog box.
4) For SQl Server connection string select Microsoft OLE DB Provider For SQL Server in Provider Tab.
5) Click button "Next" or select Connection Tab
6) Here you can select all connection details and press button
Test Connection. If it is successful close this dialog box.
7) Now open this file using "Notepad", you will find the connection string. Though it is built for OLE DB
type of connection, you can use for SQL Server connection by removing Provider attribute.
NOTE: If you are using SQL Authentication with password, then check the checkbox Allow Saving Password.
This is necessary so that password appears in connection string.

Wednesday, April 16, 2008

How do we get ipaddress or hostaddress using c#

How do we get ipaddress or hostaddress using c#

HttpContext.Current.Server.MachineName

HttpContext.Current.Request.ServerVariables["local_addr"]

Changing Connection string dynamically using Application_BeginRequest

You developed webapplication, you hosted in two different server.One server name is
"Production" and another server name is "Testing". The below code connection string will change dynamically based on server host information.

Example:
<appsettings>
<add key="production" value="http://www.production.com">
<add key="ProductionconnectionString" value="server=localhost;...." >
<add key="testconnectionString" value="server=localhost;...." >
</appsettings>

Public Application_BeginRequest()
{
bool production=false;
if(Request.URL.Host==ConfiqurationManager.Appsettings("production"))
{
Production=true;
}
}

if(Production==true)
{
context.items.add("connectionStrings",ConfiqurationManager.Appsettings("production"))
}
else
{
context.items.add("connectionStrings",ConfiqurationManager.Appsettings("test"))
}
}

Support Multiple Browser Image control

You are developing a web application that must support a variety of browser. Your analysis indicates that the majority of users that will access Internet explorer 6.0 and packet internet explorer 4.0 you are creating the header for the webapplication. you want to display variety of brower. The below example you can see image display with different browser.

Example:


<asp:image id="cklogo" runat="server" ImageUrl="Images/ck_logo.jpg" PIE4.0:imageurl="Images/small/ck_logo.jpg">

Thursday, February 21, 2008

Tips & Tricks for Dotnet

1) Incremental search in Visual Studio.
1. Press Ctrl+I.
2. Start typing the text you are searching for. Note: you'll see the cursor jump to the first match, highlighting the current search string.
3. Press Ctrl+I again to jump to the next occurrence of the search string.
4. To stop search, press Esc. Advanced tip: Press Ctrl+Shift+I to search backwards.
2) Make Window Fit any Resolution
Create function definition
<script language="Javascript">
function resolution()
{
UserWidth = window.screen.availWidth
UserHeight = window.screen.availheight
window.resizeTo(UserWidth, UserHeight)
window.moveTo(0,0)
}
</script>
</head>
2) Call the function on load event of body.
<body OnLoad=”resolution();”>

3) Adding a Print Button with Javascript
Add this line to the Page_Load event

btnPrint.Attributes("onClick") ="javascript:window.print();"

*Add a button to the page called 'btnPrint' Technically, that's it, but, let's say you want to do something a little fancier, using DotNet.
Then, add a subroutine (let's call it 'DoPrint'):

Sub doPrint(Source as Object, E as EventArgs)
lblResults.visible="True"
lblResults.text="Page has successfully been sent to the printer!"
End Sub
Then, add a direction to this sub, in the button's tag: onServerclick="doPrint"

4) *To print certain areas of a page
Create a style sheet for printing purpose, normally by removing/hiding background colors, images.... After that include it with media="print",
so that this style sheet will be applied while printing.
<LINK media="print" href="styles/printStyle.css" type="text/css" rel="stylesheet">
5) Fade Effect on Page Exit
<meta http-equiv="Page-Exit" content="progid:DXImageTransform.Microsoft.Fade(duration=.9)">

6)Disabling a Button Until Processing is Complete
Here's the scenario - let's say you have an Insert subroutine,
called 'doInsert'.
You want to immediately disable the Submit button, so that the end-user won't click it multiple times, therefore, submitting the same data multiple times. For this,

use a regular HTML button, including a Runat="server" and an 'OnServerClick' event designation - like this:
<INPUT id="Button1" onclick="document.form1.Button1.disabled=true;" type="button" value="Submit - Insert Data" name="Button1" runat="server" onserverclick="doInsert">
Then, in the very last line of the 'doInsert' subroutine, add this line: Button1.enabled="True"

Thursday, January 10, 2008

.NET BEST PRACTICES

.NET BEST PRACTICES
# Use Master Pages for common content across pages.
# Use the “@” prefix for string literals instead of using escaped strings.
# Avoid Empty Catch-blocks# In the .NET framework 2.0: use the New AppendDataBoundItems property in ListControls.
# Close DataReaders and Connections as soon as possible.
# All important graphics should have tool tip and alternate text.
# Use Typed Datasets wherever possible and required.
# Use Validators / Validator Groups for validation.
# Include exception handling in the Application_Error event of global.asax file for unhandled exceptions – implement a Global.asax error handler.
# Choose the most appropriate data type. It is advisable to stop using the “image” data type in SQL Server for any fresh development. Microsoft is planning to phase it out in future versions of SQL Server. You should use the “varbinary(max)” data type instead.

.NET Security Tips

.NET Security Tips
# Keep “Persist Security Info” as false in the connection string.
# Remove the authentication cookie in Session_End event or application specific logout event.
# Security decisions should not rely only on client-side validations – they should be verified on the server side too!
# Hashed password format should be specified in provider configuration.
# The website should be partitioned into public access areas and restricted areas that require authentication access. Navigation between these areas should not flow sensitive credentials information.
# Protect your website against Cross-Site Scripting or XSS attacks
# Dynamic queries that accept user input should be used only if stored procedures cannot be used. Even these queries should use parameters.
# Ensure that utilizing the “View source” option does not divulge any sensitive information.
# Set mode attribute in customErrors to On to prevent displaying detailed error messages to the caller.
# Ensure that utilizing the “View source” option does not divulge any sensitive information.

Performance Boosting .NET coding tips

Performance Boosting .NET coding tips
# Instantiate Objects only if really needed { will NOT use up memory unnecessarily }
# Prefer using String.Empty instead of "" for string initialisation
# Prefer using StringBuilder for dynamic creation of Strings.
# Avoid String datatype while using Switch Case programming-construct
# Use Server.Transfer() instead of Response.Redirect() method.
# Avoid use of Page.DataBind() method
# Define the four most frequently used local variables as the first four variables.
# Even inside vb.net OR c# code, Avoid using the “select * from table” syntax.
# Use proper column case in queries.dr[“ProductName”] vs dr[“PRODUCTNAME”]
# Use Page.IsPostBack to minimize redundant processing in page load.
# Avoid server round trips (by using client scripts, validator controls, html controls, caching techniques (for pages, user controls and objects)).
# Disable view state on a control-level / page-level if you do not need it.DataGrid with ViewState disabled is 66% faster than DataGrid with ViewState enabled.
# Minimize the number of objects you store in view state.
# Use a DataReader for fast and efficient data binding.
# Prefer to Use try/finally on disposable resources.
# Minimize calls to DataBinder.Eval

Wednesday, October 17, 2007

Tips for Developer

Disabling a Button Until Processing is Complete

Here's the scenario - let's say you have an Insert subroutine, called 'doInsert'. You want to immediately disable the Submit button, so that the end-user won't click it multiple times, therefore, submitting the same data multiple times.

For this, use a regular HTML button, including a Runat="server" and an 'OnServerClick' event designation - like this:

<INPUT id="Button1" onclick="document.form1.Button1.disabled=true;" type="button" value="Submit - Insert Data" name="Button1" runat="server" onserverclick="doInsert"&rt;

Then, in the very last line of the 'doInsert' subroutine, add this line:

Button1.enabled="True"


There are three ways to add Javascript attributes to a particular ASP.Net Server control:

1. button1.Attributes.Add("OnClick", "document.form1.txtName.focus();")
2. button1.Attributes("OnClick")="document.form1.txtName.focus();"
3. button1.clientclick="function name"";

Sunday, September 23, 2007

Disable right click option on IE(Internet Explorer)

A simple tip
To disable right click option on IE(Internet Explorer)
use body oncontextmenu="return false"