Thursday, January 10, 2008

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;
}
}

Sunday, January 6, 2008

How do I refresh my cache when the data changes?

http://bdotnet.in/blogs/lohithbn/archive/2007/12/22/sql-cache-dependency.aspx

How to run exe file through C# DotNET

System.Diagnostics;


Process.Start(@"c:\bala.exe");

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);

Thursday, October 18, 2007

Null-Coalescing Operator ( ?? )

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“।


This can now be abbreviated as follows using the Null-Coalescing Operator:


string fileName = tempFileName ?? "Untitled";

The logic is the same। If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.


The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;

int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero )।


These Conditional and Null-Coalescing Operators aren't the most self-describing operators :),

but I do love programming in C#!

C# 2.0 Anonymous Methods

C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible. This is akin to the best practice of keeping the declaration of a variable, for example, as close to the code that uses it.
Here is a simple example of using an anonymous method to find all the even integers from 1...10:
private int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };


int[] evenIntegers = Array.FindAll(_integers, delegate(int integer)
{
return (integer%2 == 0);
}
);

The Anonymous Method is:
delegate(int integer)
{
return (integer%2 == 0);
}

which is called for each integer in the array and returns either true or false depending on if the integer is even।


If you don't use an anonymous method, you will need to create a separate method as such:
private int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] evenIntegers = Array.FindAll(_integers, IsEven);
private bool IsEven(int integer)
{
return (integer%2 == 0);
}

When you have very simple methods like above that won't be reused, I find it much more elegant and meaningful to use anonymous methods. The code stays closer together which makes it easier to follow and maintain.
Here is an example that uses an anonymous method to get the list of cities in a state selected in a DropDownList ( called States ):
List citiesInFlorida =cities.FindAll(delegate(City city)
{
return city.State.Name.Equals(States.SelectedValue);
}
);

You can also use anonymous methods as such:
button1.Click +=
delegate
{
MessageBox.Show("Hello");
};

which for such a simple operation doesn't “deserve“ a separate method to handle the event.
Other uses of anonymous methods would be for asynchronous callback methods, etc.
Anonymous methods don't have the cool factor of Generics, but they do offer a more expressive in-line approach to creating methods that can make your code easier to follow and maintain.