Tuesday, June 30, 2009

Combine byte[]

public byte[] CombineArrays(byte[] a, byte[] b)
{
byte[] c = new byte[a.Length + b.Length];
System.Buffer.BlockCopy(a, 0, c, 0, a.Length);
System.Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
return c;
}

Write text file from the byte[]

public static void WriteFileStreamFromArray(string FilePath, byte[] b)
{


FileStream fs = null;

try
{

fs = new FileStream(FilePath,FileMode.Append);

fs.Write(b, 0, b.Length);

}
finally
{
fs.Close();
}


}

Convert byte [] to string

public string convertByteArrayToString(byte[] input)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str = enc.GetString(input);
return str;
}

Get Bytes [] from the File

public byte[] GetBytesFromFile(string fullFilePath)
{
// this method is limited to 2^32 byte files (4.2 GB)

FileStream fs = File.OpenRead(fullFilePath);
try
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
return bytes;
}
finally
{
fs.Close();
}

}

Read whole text of text file into string

public string mReadFileIntoString(string FullPath, string Separator)
{
string ReturnString = string.Empty;

if (!string.IsNullOrEmpty(FullPath) && !string.IsNullOrEmpty(Separator))
{
if (File.Exists(FullPath))
{
try
{
string Line = string.Empty;

using (FileStream fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read))
{
using (StreamReader rd = new StreamReader(fs, Encoding.Default))
{
while (!rd.EndOfStream)
{
Line = rd.ReadLine();
if (string.IsNullOrEmpty(ReturnString) && !string.IsNullOrEmpty(Line))
ReturnString = Line;
else if (!string.IsNullOrEmpty(ReturnString) && !string.IsNullOrEmpty(Line))
ReturnString = ReturnString + Separator + Line;
}
rd.Close();
}
}

return ReturnString;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("{0} has Input Error, Please Check the Input Files", FullPath);
System.Windows.Forms.MessageBox.Show("Please re-run the application with correct Input");
return string.Empty;
// return string.Format("{0} has Input Error, Please Check the Input Files", FullPath);
}
}
else
{
System.Windows.Forms.MessageBox.Show("{0} does not Exists, Please Check the Input Files", FullPath);
System.Windows.Forms.MessageBox.Show("Please re-run the application with correct Input");
return string.Empty;
// return string.Format("{0} does not Exists, Please Check the Input Files", FullPath);
}
}
return null;
}

Friday, June 26, 2009

Sending Email Via SMTP mail Server

To send emails your SmtpServer should be running. This can be checked using the Internet Services Manager found in Start->Programs->Administrative Tools. When you open the Internet Services Manager you can see whether the Default SMTP Virtual Server is running or not. If the ‘Play’ icon in the toolbar is disabled it means that the Default SMTP Virtual Server is running. You can also check by Right clicking the Default SMTP Virtual Server and see whether ‘Start’ is disabled. If it is so then the Default SMTP Virtual Server is running. This SMTP Service is shipped with Windows XP Professional and Windows 2000 by default.

It is better to test your SMTP Server whether it functions properly before developing any application in ASP.Net to send emails. The easiest way to test the SMTP Server is to open a text editor like Notepad and write,

To: someperson@somesite.com
From: me@mysite.com
Subject: Testing SMTP Server in my system.

Make sure that you are typing some valid email address for the To and From fields in the text file. Now save this text file in c:\inetpub\mailroot\pickup folder. Once you save this file it will disappear indicating that the mail has been sent. If the SMTP Server is not functioning properly it will not disappear indicating that the mail is not sent. The SMTP Service uses the c:\inetpub\mailroot folder for all the mail services.

Now we will see how to send mails from your web application. Create a web application using your Visual Studio.Net IDE. Now drop four text fields in the design page and make the fourth field to accept multi-line content by setting the TextMode property to MultiLine. Drop down a submit button which can be used to send mails when clicked. Now double click the button on the form to open the code window and write code in the Button1_Click() event to send emails. The steps involved in code are:

1. Import the System.Web.Mail namespace
2. Create an instance of the MailMessage class
3. Set the properties of the MailMessage class like To, From, Subject, and Body
4. Use the Send method of the SmtpMail class to send the email message by passing the instance of the MailMessage class as the parameter to the Send method.

The code for sending emails will look somewhat like this after you follow the above steps:

using System.Web.Mail;

protected MailMessage emailMesg;

emailMesg = new MailMessage();
emailMesg.From = txtFrom.Text;
emailMesg.To = txtTo.Text;
emailMesg.Subject = txtSubject.Text;
emailMesg.Body = txtBody.Text;
emailMesg.Priority = MailPriority.High;
SmtpMail.Send(emailMesg);
By default the mail messages are sent as Plain text. It is possible to send mails in the HTML format which allows you to sent mails as web pages which is rich in look. To send emails in the HTML format you need to set the BodyFormat property of the MailMessage instance to MailFormat.Html. If you need to send mail in HTML format the above code would be added one more line like,

emailMesg.BodyFormat = MailFormat.Html;

The Body property of the MailMessage instance would be in the HTML code as given in the following example.


The above HTML code will send the mail message in white color with Maroon background color for the page. You can also send attachments with an email message. For sending attachments you have to use the MailAttachment class available. The code given below will add a file as attachment to the email message.

protected MailAttachment emailAttach;
emailAttach = new MailAttachment(@"path-to-the-document");
emailMesg.Attachments.Add(emailAttach)

This code has to be given before using the Send method. The path to the document to be attached can also be given from the File Input field in the HTML code.

Thus it is easy to send emails through a web page in your web application. You may try to send emails by following the above methods in your websites

Friday, June 19, 2009

Write CSV from Oralce Reader

public static string mWriteRequired(OracleDataReader rd)
{
StringBuilder sb = new StringBuilder();
//For field Names
for(int i = 0 ;i< rd.FieldCount;i++)
{
if( i < (rd.FieldCount - 1) )
{
sb.Append("\t" + rd.GetName(i) + "\t" + ",");
}
else
{
sb.Append("\t" + rd.GetName(i) + "\t" + "\n");
}
}
//For field Values
while( rd.Read())
{
for(int i = 0 ;i< rd.FieldCount;i++)
{
if( i < (rd.FieldCount - 1) )
{
sb.Append("\t" + rd.GetValue(i) + "\t" + ",");
}
else
{
sb.Append("\t" + rd.GetValue(i) + "\t" + "\n");
}
}
}
rd.Close();
return sb.ToString();

}

Monday, June 8, 2009

How to display Client Date and Time

function displayTime()
{
var localTime = new Date();
var year= localTime.getYear();
var month= localTime.getMonth() +1;
var date = localTime.getDate();
var hours = localTime .getHours();
var minutes = localTime .getMinutes();
var seconds = localTime .getSeconds();
var div=document.getElementById("div1");
div.innerText=year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds;
}
 
Locations of visitors to this page