Thursday, January 10, 2008

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

Moving Listitem from left Listbox to right Listbox

protected void btnmoveright_Click(object sender, EventArgs e)
{
ArrayList arrfirst = new ArrayList();
foreach (ListItem lstfirstitem in lstfirst.Items)
{

if (lstfirstitem.Selected == true)
{
arrfirst.Add(lstfirstitem);
}

}
for (int i = 0; i < arrfirst.Count; i++)
{
ListItem tst = (ListItem)arrfirst[i];
if (lstsecond.Items.Contains(tst) == false)
{ lstsecond.Items.Add(tst);
lstfirst.Items.Remove(tst);
}
}
}


protected void btnmoveleft_Click(object sender, EventArgs e)
{
lstfirst.SelectionMode = ListSelectionMode.Multiple;
lstfirst.Attributes.Add("SelectionMode", "Multiple");
ArrayList arrsecond= new ArrayList();
foreach (ListItem lstseconditem in lstsecond.Items)
{ if (lstseconditem.Selected == true)
{ arrsecond.Add(lstseconditem); }
} for (int i = 0; i < arrsecond.Count; i++)
{ ListItem tst = (ListItem)arrsecond[i];
if (lstfirst.Items.Contains(tst) == false)
{ lstfirst.Items.Add(tst);
lstsecond.Items.Remove(tst);
}
}
lstfirst.SelectedItem.Selected = false;
lstfirst.SelectionMode = ListSelectionMode.Single;
}

Maxlength checking in Textarea Using Javascript

function maxcheck()

{
var maxval=1000;
var maxtxtlength=window.document.myform.Description.value.length;
if(parseInt(maxtxtlength)>parseInt(maxval))
{
alert("This Description field accept only 1000 characters");
window.document.myform.Description.innerText=window.document.myform.Description.value.substring(0,1000);
return false;
}
}

Friday, January 4, 2008

Defend Your Code with Top Ten Security Tips Every Developer Must Know

http://msdn.microsoft.com/msdnmag/issues/02/09/securitytips/default.aspx

Web Parts in ASP.NET

http://www.beansoftware.com/ASP.NET-Tutorials/Web-Parts.aspx

Encrypting Connection Strings in web.config file

http://www.beansoftware.com/ASP.NET-Tutorials/Encrypting-Connection-String.aspx

What is the purpose of AppendDataBoundItems for dropdown


AppendDataBoundItems

Definition:

Using this you can append dynamically.

Example

string[] sdss ={ "test", "test1" };
this.myDropDown.AppendDataBoundItems = true;
this.myDropDown.Items.Add(new ListItem("Bitte wählen...", ""));
this.myDropDown.DataSource = sdss;
this.myDropDown.DataBind();

output

Bitte wählen
test
test1


IDictionary

Hashtable ht = new Hashtable();
ht.Add(1, "bala");
ht.Add(2, "bala1");
IDictionary idic = (IDictionary)ht;
foreach (DictionaryEntry de in idic)
{
Response.Write(de.Key);
Response.Write(de.Value);
Response.Write("
");
}

out put

2bala1
1bala

IDictionaryEnumerator

Hashtable ht = new Hashtable();
ht.Add(1, "bala");
ht.Add(2, "bala1");
IDictionaryEnumerator ss = ht.GetEnumerator();
while (ss.MoveNext())
{
Response.Write(ss.Key);
Response.Write(" "+ss.Value);
Response.Write("
");
}

output:
2 bala1
1 bala

Inbuilt Interface Using Dotnet

1) IEnumerator


Definition:

Using this interface you can read the array,string, Performance is good.


Example:1

string[] sdss ={ "test", "test1" };

IEnumerator enumerator = sdss.GetEnumerator();
while (enumerator.MoveNext())
{
Response.Write(enumerator.Current.ToString() +"
");
}

output

test
test1

Example:2

string s="dd";
IEnumerator enumerator = s.GetEnumerator();
while (enumerator.MoveNext())
{
Response.Write(enumerator.Current.ToString() +"
");

}

output

d
d

Thursday, January 3, 2008

Retrieving Selected Text Using Javascript

function copy()

{

var selectedText = document.selection;

if (selectedText.type == 'Text')

{

var newRange = selectedText.createRange();

form1.txtTest.focus();alert(newRange.text)}else{alert('Please Select The Text');

}}

Tuesday, January 1, 2008

Interop Services

Definition

>>handling unmanaged code we have to use Interop services.

The Microsoft .NET Framework promotes interaction with COM components,
COM+ services, external type libraries, and many operating system services. Data types, method signatures, and error-handling mechanisms vary between managed and unmanaged object models. To simplify interoperation between .NET Framework components and unmanaged code and to ease the migration path, the common language runtime conceals from both clients and servers the differences in these object models.
Code executing under the control of the runtime is called managed code. Conversely, code that runs outside the runtime is called unmanaged code.

Example of Unmanaged code

COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code.

Thursday, December 13, 2007

Sending Email using "System.Net.Mail"

<%@ Import Namespace="System.Net.Mail" %>
void Page_Load()
{
//SmtpClient client = new SmtpClient();
//client.Host = "localhost";
//client.Port = 25;
//client.Send("steve@somewhere", "bob@somewhere.com", "Let's eat lunch!", "Lunch at the Steak House?");
}

Sunday, December 9, 2007

Import Excel to datagrid

http://www.dotnetspider.com/kb/Article3526.aspx

Generating excel sheet for all the tables the Data Set contains using Excel XML.

public class ExcelGenerator
{
/// Create and Excel File using Excel Xml
///
/// Complete File Path
/// Data Set
/// Work Sheet Name
/// No of records to show per sheet.
public static void CreateExcelXML(string FileName, DataSet Ds, string SheetName, int RecordsPerSheet)
{
StringWriter S = new StringWriter();
XmlTextWriter X = new XmlTextWriter(S);
StreamWriter Writer = new StreamWriter(FileName, false);
try
{
SheetName = (SheetName.Trim().Length == 0) ? "WorkSheet" : SheetName;
RecordsPerSheet = (RecordsPerSheet <= 0) ? 650000 : RecordsPerSheet;

X.Formatting = Formatting.Indented;
X.Indentation = 4;
X.WriteProcessingInstruction("xml", "version=\"1.0\"");
X.WriteProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"");
X.WriteStartElement("Workbook", string.Empty);
X.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
X.WriteAttributeString("xmlns:o", "urn:schemas-microsoft-com:office:office");
X.WriteAttributeString("xmlns:x", "urn:schemas-microsoft-com:office:excel");
X.WriteAttributeString("xmlns:ss", "urn:schemas-microsoft-com:office:spreadsheet");
X.WriteAttributeString("xmlns:html", "http://www.w3.org/TR/REC-html40");

X.WriteStartElement("DocumentProperties", string.Empty);
X.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:office");
X.WriteElementString("Author", "Farhan Khan");
X.WriteElementString("LastAuthor", string.Empty);
X.WriteElementString("Created", string.Empty);
X.WriteElementString("Company", string.Empty);
X.WriteElementString("Version", "1.0");
X.WriteEndElement();

X.WriteStartElement("ExcelWorkBook", string.Empty);
X.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:office");
X.WriteEndElement();

X.WriteStartElement("Styles", string.Empty);
X.WriteStartElement("Style", string.Empty);
X.WriteAttributeString("ss:ID", "Default");
X.WriteAttributeString("ss:Name", "Normal");
X.WriteStartElement("Alignment", string.Empty);
X.WriteAttributeString("ss:Vertical", "Bottom");
X.WriteEndElement();
X.WriteElementString("Borders", string.Empty);
X.WriteElementString("Font", string.Empty);
X.WriteElementString("Interior", string.Empty);
X.WriteElementString("NumberFormat", string.Empty);
X.WriteElementString("Protection", string.Empty);
X.WriteEndElement();
X.WriteEndElement();


for (int i = 0; i < Ds.Tables.Count; i++)
{
int k = 0;
for (int j = 0; j < Ds.Tables[i].Rows.Count; j++)
{
string Name = string.Empty;
if (j == 0)
{
Name = SheetName + " " + Convert.ToString(i + 1) + "." + k.ToString();
WriteWorkSheetStart(X, Ds, i, Name);
k++;
}
else if ((j % RecordsPerSheet) == 0)
{
Name = SheetName + " " + Convert.ToString(i + 1) + "." + k.ToString();
WriteWorkSheetEnd(X);
WriteWorkSheetStart(X, Ds, i, Name);
k++;
}
// Writing Data Rows
X.WriteStartElement("Row");
for (int x = 0; x < Ds.Tables[i].Columns.Count; x++)
{
X.WriteStartElement("Cell");
X.WriteStartElement("Data");
X.WriteAttributeString("ss:Type", "String");
X.WriteValue(Ds.Tables[i].Rows[j][x].ToString());
X.WriteEndElement();
X.WriteEndElement();
}
X.WriteEndElement();
}
WriteWorkSheetEnd(X);
}

X.WriteEndElement(); // End Workbook Element
X.Flush();
Writer.Write(S.ToString());
}
finally
{
Writer.Close();
S.Close();
X.Close();
S.Dispose();
Writer.Dispose();
}
}


private static void WriteWorkSheetStart(XmlTextWriter X, DataSet Ds, int i, string SheetName)
{
X.WriteStartElement("Worksheet", string.Empty);
X.WriteAttributeString("ss:Name", SheetName);
X.WriteStartElement("Table", string.Empty);
X.WriteAttributeString("ss:ExpandedColumnCount", Ds.Tables[i].Columns.Count.ToString());
X.WriteAttributeString("ss:ExpandedRowCount", Convert.ToString(Ds.Tables[i].Rows.Count + 1));
X.WriteAttributeString("x:FullColumns", "1");
X.WriteAttributeString("x:FullRows", "1");
X.WriteStartElement("Row");
for (int j = 0; j < Ds.Tables[i].Columns.Count; j++)
{
X.WriteStartElement("Cell");
X.WriteStartElement("Data");
X.WriteAttributeString("ss:Type", "String");
X.WriteValue(Ds.Tables[i].Columns[j].ColumnName);
X.WriteEndElement();
X.WriteEndElement();
}
X.WriteEndElement();
}

private static void WriteWorkSheetEnd(XmlTextWriter X)
{
X.WriteEndElement();
X.WriteStartElement("WorksheetOptions", string.Empty);
X.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:excel");
X.WriteStartElement("Panes", string.Empty);
X.WriteStartElement("Pane", string.Empty);
X.WriteElementString("ActiveRow", "2");
X.WriteElementString("ActiveCol", "1");
X.WriteEndElement();
X.WriteEndElement();
X.WriteElementString("ProtectObjects", "False");
X.WriteElementString("ProtectScenarios", "False");
X.WriteEndElement();
X.WriteEndElement();
}
}

Friday, November 2, 2007

Tips for spliting string using C#

Spliting string using C#.netstring bala = "slfjslf/dfd";
bala.Split(new string[] { "slf" }, StringSplitOptions.None);