Creating Thumbnails in .NET

Hi everyone….!!!

Following is the code which can creates the thumbnails of images with no hardwork….

System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imagePath); System.Drawing.Image thumbnailImage = originalImage.GetThumbnailImage(90, 90, null, IntPtr.Zero);

thumbnailImage.Save(thumbnailPath);

See how easy it is to create thumbnail in .net

Keep Smiling 🙂

Embedding UserControl in HTML

Hi everyone….!!!

Following code I found very useful for embedding user defined controls directly in HTML.

You can use controls either from dll or an exe.

<object id=”rtfcontrol” height=”85%” width=”300″  

classid=”http://localhost/Calculator.exe#Calculator.MyControl”&gt;

</object>

 —keep Smiling 🙂

Sending Email in .net 2005

Hi everyone…

Follwoing is very handy class to send emails using .net 2005.

It was created using existing .net class and namespace and it exposes all the functionality via public properties and functions.

public class SendEMail
{
#region Private Variable
private string _smtpServer;
private string _message;
private string _fileAttachment;
private MailMessage _mailMessage;
private MailAttachment _attachments;
#endregion
#region Public Properties
public string ToAddress
{
set { _mailMessage.To = value; }
get { return _mailMessage.To; }
}
public string CCAddress
{
set { _mailMessage.Cc = value; }
get { return _mailMessage.Cc; }
}
public string BCCAddress
{
set { _mailMessage.Bcc = value; }
get { return _mailMessage.Bcc; }
}
public string FromAddress
{
set { _mailMessage.From = value; }
get { return _mailMessage.From; }
}
public string SMTPServer
{
set { _smtpServer = value; }
get { return _smtpServer; }
}
public string Message
{
set { _mailMessage.Body = value; }
get { return _mailMessage.Body; }
}
public string Subject
{
set { _mailMessage.Subject = value; }
get { return _mailMessage.Subject; }
}
public string FileAttachment
{
set { _fileAttachment = value; }
get { return _fileAttachment; }
}
#endregion
#region Constructors
public SendEMail()
{
_mailMessage = new MailMessage();
}
#endregion
#region Public Function
public bool SendMail()
{
try
{
if (ToAddress == null || ToAddress == string.Empty)
{
return false;
}
if (Message == null || Message == string.Empty)
{
return false;
}
if (FromAddress == null || FromAddress == string.Empty)
{
return false;
}
if (SMTPServer == null || SMTPServer == string.Empty)
{
SMTPServer = “localhost”;
}
SmtpMail.SmtpServer = SMTPServer;
_mailMessage.BodyFormat = MailFormat.Html;
_mailMessage.Priority = MailPriority.High;
if (FileAttachment != null && FileAttachment != string.Empty)
{
_attachments = new MailAttachment(FileAttachment);
_mailMessage.Attachments.Add(_attachments);
}
SmtpMail.Send(_mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
}

keep smiling 🙂

Connect to ORACLE database using C#

Hi everyone…

you can connect to oracle database either by OleDb object or by DSN. Following code snippet did the trick. Go through it.

using System;
using System.Data;
using System.Data.ADO;
class OracleTest
{
static void Main()
{
//UID – User Id
//PWD – Password
//Server – Your Service name
//Using Microsoft ODBC for Oracle DSN-Less
//const string strConn = “Provider=MSDASQL;DRIVER={Microsoft ODBC for ORACLE};UID=scott;PWD=tiger;Server=fnqa”;
//Using Microsoft OLEDB Provider for Oracle
const string strConn = “Provider=MSDAORA.1;
Data Source=oracle_db;User ID=scott;Password=tiger”;
//Using DSN Connection
//const string strConn =”DSN=oracle_dsn;UID=scott;PWD=tiger” ;
const string strSQL = “Select * From EMP”;
try
{
DataSet ds = new DataSet(“EmployeesSet”);
ADODataSetCommand myCommand = new ADODataSetCommand(strSQL,strConn);
myCommand.FillDataSet(ds,”Buyer”);
Console.WriteLine(ds.Xml);
}
catch(ADOException e)
{
Console.WriteLine(“Exception Occured :{0}”,e.Errors[0].Message);
}
}
}

Courtesy Ravi Shankar

Keep Smiling 🙂

Export DataSet to Excel in C#

Hi Everyone…
I have been exporting data from dataset to excel using nested loops, excel object, accessing each cell of excel sheet and each cell of dataset and so on and so forth until I find this……

Before writing about it a special thanks to Peter A. Bromberg for writing such a nice article which really made at least my life easy….:). In his article he wrote a very simple function to export data without using excel object and other messy stuff. XSL Transformation is applied to dataset and XML for excel is generated.

Below is the complete function which will write an Excel file to your local disk. You have to pass it the DataSet to export and path to where file should be generated. 

public static void CreateWorkbook(DataSet ds, String path)
{
   XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
   XslTransform xt = new XslTransform();
   StreamReader reader =new StreamReader(typeof (WorkbookEngine).Assembly.GetManifestResourceStream(typeof (WorkbookEngine), “Excel.xsl”));
   XmlTextReader xRdr = new XmlTextReader(reader);
   xt.Load(xRdr, null, null);

   StringWriter sw = new StringWriter();
   xt.Transform(xmlDataDoc, null, sw, null);

   StreamWriter myWriter = new StreamWriter (path + “\\Report.xls“);
   myWriter.Write (sw.ToString());
   myWriter.Close ();
}

and thats it. Your DataSet has been exported to an Excel file which is saved at the path passed to this function.

For original article, please click here


Keep Smiling 🙂

Making HTTP Requests without a Browser

Hi everyone…

Here is way to make HTTP requests without using a browser. 

1.  Before starting add an reference to

using System.Net

2. After that following code snippet will save the html of any webpage which will be mentioned in url variable to a string named as webPageHtml.  

string url = “<you web pages url goes here>”;
WebRequest myWebRequest = HttpWebRequest.Create(url);
Stream myWebStream = myWebRequest.GetResponse().GetResponseStream();
StreamReader myReader = new StreamReader(myWebStream);
string webPageHtml = myReader.ReadToEnd();
myReader.Close();
Console.Write (webPageHtml);
myReader.Close ();

3. Thats it.


Keep Smiling 🙂

Downloading Files using C#/VB.NET

Hi Everyone… Downloading any type of file using http request is not a big at all using either  C# or Visual Basic.NET code. Please follow the steps: 

1. You need to add an reference 

using System.Net;  2. Now you can use the class named as WebClient, so just declare an object of this class and intialize using 0 argumnet constructor. 

WebClinet myClient = new WebClient (); 

3.  Now you can use the method DownloadFile of WebClient class to start downloading the file. 

//myClient.DownloadFile (<Http Address>,<Local Path>)

string httpAddress = http://msdn2.microsoft.com/en-us/library/system.net.webclient.downloadfile(vs.71).aspx;

string localPath = “C:\myDownload”;

myClient.DownloadFile (httpAddress, localPath);

4. Thats it…change the httpAddress of your desired file and localPatth. 

Keep Smiling 🙂