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 🙂

How to run Apache & IIS parallel

Hi everyone…

Couple of months back, I was working on a ASP.NET 1.1 application which requires IIS. The application basically fetches  records from webservice and validating those records with our database.

Meanwhile I was working on an php application and it requires Apache. I was using WAMP5 ver 1.6.6 with apache 2.0.59.

Initially I have to stop IIS in order to use Apache and vice versa. As both of servers uses port 80 for all http request.

At that point, I was aware of that there must be some parameters in php configurations files which would allow me to change the http port from 80 to something else, so I just started browsing configuration files and was able to change the port from 80 to say 8080 and now both of my webservers were happy:

Now I can access my sites like;

for IIS,

http://localhost/index.aspx

for Apache

http://localhost:8080/index.php

so for php my localhost is now localhost:8080 and rest is the same. 🙂

What I did:

1. Opened the httpd.conf file
2. Searched for term 80
3. Found on first hit under parameter listen
4. Changed 80 to 8080 there
5. Restarted all wamp services. And thats it.

So rather then searching for 80, you must search for listen parameter and change it

No other changes and settings required. 🙂

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 🙂

Page Loads twice for a single click ASP.NET 2.0

Today i was writing an application which was bringing data from the webservice provided by AMAZON.COM.
What it was doing is that it was fetching records of a products like price, sku etc.. and its image url. I was displaying that product information in labels and an image in a image control.
What happens then was that first page was laoded putting information in labels and then refreshes again to display the image and when it was loading image it doesn’t loads the page as a result of post back and thus refreshes some of my data.
So if this things happens to you, i mean if your pages loads two time then there must be some controls which are causing this load two times. so check them out.

Posted in ASP.NET. 1 Comment »