Saturday, 9 July 2011

Print html or any files from Windows form using C#

Using this code you would be able to print any type of pages.
just copy and paste this code on any button event handler.

private void buttonPrint_Click(object sender, EventArgs e)
{
try
{
Process printProc = new Process();

 printProc.StartInfo.FileName =" yourFileName";
printProc.StartInfo.UseShellExecute = true;
printProc.StartInfo.Verb = "print";
printProc.StartInfo.CreateNoWindow = true;
printProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printProc.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Jawed Blog say!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

Thanks,
Md. Jawed

Saturday, 5 March 2011

C# Code snippet to send an Email with attachment from Outlook, Yahoo, HotMail, AOL and Gmail

1. Down Load Source Code click here.
Introduction
Using this code snippet user would be able to send an email with attachment through Outlook, Yahoo, AOL, HOTMAIL and Gmail.
Background
Already loads of information are available on internet for the same purpose. But not all the code is placed at one place. So here is my small effort to accumulate all the code at one place and that is nothing better than CodeProject. Hopefully it will be helpful for the beginners or whoever in needs of it.
Using the code
Name Space:

Block Diagram(SMTP Setting for various client):
Below is the Block diagram for the SMTP settings i have used to send out an email from various client providers.
Wherever needed I have explain the code in details. The codes are self explanatory, any way for the same I have included comments. So here is the code snippet to achieve your purpose.
1. Using Outlook:
To send an email using outlook, we need to add a reference to the dynamic link library for Outlook which is called Microsoft.Office.Interop.Outlook.dll.
For the same follow the below steps
1. Go to your solution explorer
2. Click on add a reference
3. Click on .Net Tab
4. Go through the DLL and select Microsoft.Office.Interop.Outlook.dll correctly.
5. When you have selected the correct reference you select the “OK” button and this reference will be added to your project under references.

6. Now we need to add a reference in our class to the Outlook reference we have added to the project in our previous example.
using Outlook = Microsoft.Office.Interop.Outlook;
And finally the code would look something like this:

2. Using HOTMAIL:
To send an email using HotMail, we need to add a reference to the dynamic link library for Hotmail/Gmail/AOL/Yahoo which is called System.Net.Mail.
For the same follow the below steps:
1. Go to solution explorer of your project
2. Select add a reference
3. Click on .Net Tab
4. Go through the DLL and select System.Net.Mail.
5. When you have selected the correct reference you select the “OK” button and this reference will be added to your project under references.
6. Now we need to add a reference in our class to the Hotmail/gmail/aol/yahoo reference we have added to the project.
using System.Net.Mail;
Note: The HOTMAIL SMTP Server requires an encrypted connection (SSL) on port 25.
And finally the code would look something like this:

3. Using Yahoo! 4. Using AOL:
5. Using Gmail:
Note:
The GMAIL SMTP Server requires an encrypted connection (SSL) on port 487.




Already I am using the above code snippet for my automation purpose and its working fine for me!!

Thank you!!
Feel Free to provide your comments and suggestion.
Md. Jawed.

Saturday, 26 February 2011

QC DATA PULLER using C#

Using this application user can generate report of test case execution from QC(Quality center) in to HTML web page with fancy pie chart and tabular format using C# and OTA API expose from QC(Quality Center).
i have pulished an Articles on Code project for generating report for test cases executed from QC. the report would be in HTML web page format with pie chart and tabullar format.

for more details you visit below link for that:
QC Data Puller on Code Project.
Please go through the link and provide your valuabe comments and suggestion!!
Thank you!!
~jawed

Monday, 21 February 2011

Post Comments web page in ASP.Net using C#

Recently I have developed a web application automation application in which I have added a functionality for the user to post their comments, experience and questions for the admin.
Thought to share this approach with all of you.
So here is my approach what I have done.
1. An aspx page with name URComments.aspx.
2. we will use XML file to store all the comments posted by the users.
3. Used GridView to show all the comments posted by the users.
4. On page load and/or on post button click we will call a funtion to read all the comments
posted till now by the user and show that in to GridView.
5. Below controls we will on design view of URComments.aspx.
a. Text box to get the Emailid from the user.
b. Text box for the user to entered Comments and/or suggestion.
c. Button to submit comments.
d. GridView to show all the comments posted till that time.
The web page would look something like this:

Fig 1. Web Page with Post Comments Feature

6. The XML file format would be something as below. This XML file we will use to store all the comments provided by the user. B d way you can customize this as per your requirement.


fig 2. XMl File to store Comments posted by User
Page Load :
public static string xmlPath =ConfigurationSettings.AppSettings["xmlFilePath"].ToString();
//on page load redirect the user to the specific url and then show all the comments
//posted //till //now
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
showThePostOnWebPage();
}

}

Showing Post in Web Page:
//Display the Post on webPage
public bool showThePostOnWebPage()
{
bool result = false;
try
{
DataSet dsForWebPage = readXMFile();
GridViewPost.DataSource = dsForWebPage.Tables[0];
GridViewPost.DataBind();
result = true;
}
catch (Exception ex)
{

}
return result ;
}


Reading the XML file for the comments posted till now:
//Read the xml File
public DataSet readXMFile()
{
DataSet ds = new DataSet();
System.IO.FileStream fsReadXml = null;
try
{
//Read an XML file to generate serial number
fsReadXml = new System.IO.FileStream(xmlPath, System.IO.FileMode.Open);
//Declare a dataset to hold all the XML value
//Read the value from XML reader to the dataset
ds.ReadXml(fsReadXml);
}
catch (Exception ex)
{
ds = null;
}
finally
{
fsReadXml.Close();
}
return ds;
}

On Button click (when user tried to submit the post):
protected void ButtonPost_Click(object sender, EventArgs e)
{
FileStream fsxml = null;
FileStream fs = null;
try
{
//check that the comments textbox is not empty
if (TextBoxComments.Text != null && TextBoxComments.Text != string.Empty )
{
//Read an xml file to get all the post commented till now
DataSet dsSet = readXMFile();
if (dsSet != null)
{
//make sure that table exist
int totalRowCount = dsSet.Tables[0].Rows.Count;
string strSerialNumber = null;
string getTheNumber = null;
if (totalRowCount <= 0) { getTheNumber = "1"; } else
{
//get the number
strSerialNumber = dsSet.Tables[0].Rows[totalRowCount - 1][4].ToString();
getTheNumber = strSerialNumber.Remove(0, 3);
}

Int32 currentSerialNumber = Convert.ToInt32(getTheNumber);
Int32 nextSerilaNumber = currentSerialNumber + 1;
string strSerialNumberToadd = "CFG" + nextSerilaNumber.ToString();
fs = new FileStream(xmlPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(fs);
// New XML Element Created
XmlElement newcatalogentry = xmldoc.CreateElement("CFGPost");
// New Attribute Created
XmlAttribute newcatalogattr = xmldoc.CreateAttribute("ID");
// Value given for the new attribute
newcatalogattr.Value = strSerialNumberToadd;
// Attach the attribute to the XML element
newcatalogentry.SetAttributeNode(newcatalogattr);
// First Element - Comments
XmlElement firstelement = xmldoc.CreateElement("Comments");
// Value given for the first element
firstelement.InnerText = TextBoxComments.Text.ToString();
// Append the newly created element as a child element
newcatalogentry.AppendChild(firstelement);
// Second Element - Date
XmlElement secondelement = xmldoc.CreateElement("Date");
// Value given for the second element
secondelement.InnerText = DateTime.Now.ToString();
// Append the newly created element as a child element
newcatalogentry.AppendChild(secondelement);
// Third Element - EmailID
XmlElement thridelement = xmldoc.CreateElement("EmailId");
// Value given for the third element
thridelement.InnerText = TextBoxEmailId.Text;
// Append the newly created element as a child element
newcatalogentry.AppendChild(thridelement);
// Fourth Element - UserName
XmlElement fourthelement = xmldoc.CreateElement("UserName");
// Value given for the fourth element
fourthelement.InnerText = hostName.ToString();
// Append the newly created element as a child element
newcatalogentry.AppendChild(fourthelement);
// New XML element inserted into the document
xmldoc.DocumentElement.InsertAfter(newcatalogentry,xmldoc.DocumentElement.LastChild);
fs.Close();
// An instance of FileStream class created
// The first parameter is the path to the XML file
fsxml = new FileStream(xmlPath, FileMode.Truncate,
FileAccess.Write,
FileShare.ReadWrite);
// XML Document Saved
xmldoc.Save(fsxml);
fsxml.Close();
//Clean the text box area
TextBoxEmailId.Text = string.Empty;
TextBoxComments.Text = string.Empty;
bool flag = showThePostOnWebPage();
RegisterClientScriptBlock("js", "
");
}
else
{
//show the message to the user that currently we are facing some issue
RegisterClientScriptBlock("js", "
"
);
}
}
//if it is empty then show the message to user
else
{
RegisterClientScriptBlock("js", "
"
);

}
}
catch (Exception ex)
{

}
}

Method whenever user clicked on Pagination of Grid View
//fire this even whenever user clicked on pagination at gridView
protected void GridViewPost_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridViewPost.PageIndex = e.NewPageIndex;
showThePostOnWebPage();

}
catch (Exception ex)
{

}
}

Now you are ready with the Post Comments project.
You can integrate this with your web application to provide the user an option to share their suggestion/comments and issues with others.
Hope you would get benefited form this.

Feel free to provide your comments.
*************************
Thank You,
Md. Jawed

Wednesday, 16 February 2011

Debugging, Tracing and Instrumentation in .NET and ASP.NET (14 FAQ) by Shivprasad koirala

A nicely explained article about Debugging, Tracing and Instrumentation in .Net.
This article is posted on Code Project by Shivprasad koirala.
He has explained everything in such a nice way with nice images and videos that you will understand all the pin point of Debugging, Tracing and Instrumentation in .Net.
Let me put some contents from his post:
What is Instrumentation?
Debugging application is not just about pressing F 10 or F 11 and watching “add watch windows” using visual studio IDE. Debugging becomes pretty complex on production environments where the project is deployed in a release mode and you need to figure out till what point the code ran and when did it crash. The worst part is you are enjoying your sleep at home and suddenly someone calls up and says, “Hey! the application crashed in production”.If your application is well planned with proper instrumentation you can just say the person to view the event viewer or a log file for further details and you can give the solution on the phone itself.So defining instrumentation in short, it’s the ability of the application to monitor execution path at critical points to do debugging and measure performance.
What is debugging and tracing?
We would like to enable application instrumentation in two situations while you are doing development and while your application is in production. When you monitor application execution and performance in development stage is termed as “Debugging” and when you do in deployed environment it’s termed as ‘Tracing’.


Thanks to Shivprasad koirala for posting such a useful and knowledgeable articles.
Hats off to you!!!
~jawed