Tuesday, November 28, 2006

How to open web page from windows application (C#/NET)

How to open web page from windows application (C#/NET)
Introduction
Some time it’s requiring to open IE from dot net application. For example: If you want your window application opens the web (.aspx) page. Then you require some code that should call web application and start the web project.
You can open any web site through this application like: www.yahoomail.com, www.gmail.com etc.
Follow the three simple step to create this application.
1) Give the web reference of Microsoft.mshtml.dll and Interop.SHDocVw.dll DLLs to your project
Path:

a) C:\WINDOWS\assembly\GAC\Microsoft.mshtml

\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll

b) Download DLL from net and put into "\...\obj\Debug\Interop.SHDocVw.dll" folder

2) Create one cs file “Browser.cs” in same project
using SHDocVw;
public class Browser
{
public static IWebBrowser2 TheInstance = new InternetExplorerClass() as IWebBrowser2;
}
3) Add this on the top of the page
using mshtml;
write following code in the onload event or Startup event.
object missing = System.Reflection.Missing.Value;
// any URL you can put here like: www.yahoomail.com etc.
Browser.TheInstance.Navigate
ref missing, ref missing, ref missing, ref missing);
Browser.TheInstance.Visible = true;
while (Browser.TheInstance.Busy)
Thread.Sleep(1000);
IHTMLDocument2 doc = Browser.TheInstance.Document as IHTMLDocument2;
IHTMLElement body = doc.body;
IHTMLElementCollection children = body.all as IHTMLElementCollection;
foreach (IHTMLElement child in children)
{
if (child.id != null)
{
string elementName = child.id.Trim();
//if you know in that web page one button control name
//is "btnStartQueue" then you can do click operation from below code
if (elementName.Trim() == "btnStartQueue")
{
child.click();
}
}
}
Ritesh Kumar Kesharwani
Cell : 91-9890301536


Want to start your own business? Learn how on
Yahoo! Small Business.

Friday, November 10, 2006

Reading resource files from dotnet application

Overview:

This document is useful for creating multilingual application using dot net. Language and time change accordingly to area and geographic difference. This document will help you to create application which will take care of language difference and guide you to create multilingual application.


Introduction:
There are multiple approaches to develop multilingual application like:
1) Create multiple pages for different languages and display based on language setting.
2) Create multiple database tables for different languages and fetch data accordingly.
3) Create resource files for different languages.
This will have a better performance compared to fetching from the database.
Third approach will have a better performance compared to others approaches and easy to implement also. To implements this application requires a specific setting and knowledge about Globalization, Localization and culture.
Globalization is a process of identifying all the parts of your application that need to be different for respective languages and separate them from the core application.
Localization is process of creating and configuring your application for a specific language.
A culture is the combination of the language that you speak and the geographical location you belong to. It also includes the way you represent dates, times and currencies. A culture is represented as shown in the following example:

es-CO - For the culture determined by the Spanish in Colombia
fr-FR - For the culture determined by the French in France
fr-CA - For the culture determined by the French in Canada
en-US - culture represents English language being spoken in US
en - For the culture determined by the English in General
Source Code:
Create application to read resource file using C#.net windows or web application.
Follow the simple four steps to create application.
1) Set global environment language setting in web.config/app/config file.
For example:
<add key="GlobalEnvironmentLanguage" value="en"></add>
2) Create one .cs file (for example: LeadMgmtResourceFile.cs)
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Resources;
using System.Configuration;
namespace TEST.LeadMgmt
{
[Serializable]
public class LeadMgmtResourceFile
{
private CultureInfo culture;
public string GetResourceError(string errorId)
{
string resourceCulture = ConfigurationManager.AppSettings.Get("GlobalEnvironmentLanguage");
try
{
if (resourceCulture == string.Empty resourceCulture == null)
{
return "Language culture not mentioned in the config file.";
}
culture = CultureInfo.CreateSpecificCulture(resourceCulture);
if (culture.ToString() == "es-CO")
{
culture = CultureInfo.CreateSpecificCulture("es-CO");
}
else if (culture.ToString() == "fr-FR")
{
culture = CultureInfo.CreateSpecificCulture("fr-FR");
}
else if (culture.ToString() == "fr-CA")
{
culture = CultureInfo.CreateSpecificCulture("fr-CA");
}
else
{
culture = CultureInfo.CreateSpecificCulture("en");
}
ResourceManager rm = new ResourceManager("LeadMgmtUtility” + ResourceFileName , typeof(LeadMgmtResourceFile).Assembly);
return rm.GetString(errorId, culture);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
LeadMgmtUtility :
This is project name where LeadMgmtResourceFile.cs file exist
ResourceFileName :
This is Resource file for different -2 language
1) LeadMgmtResource.es-CO.resx
2) LeadMgmtResource.fr-CA.resx
3) LeadMgmtResource.fr-FR.resx
4) LeadMgmtResource.en.resx
LeadMgmtResourceFile :
This is class name where GetResourceError() exist to read resource file
3) Create resource files for each language like:
<base file name>.<locale>.resx
a. LeadMgmtResource.es-CO.resx
b. LeadMgmtResource.fr-CA.resx
c. LeadMgmtResource.fr-FR.resx
d. LeadMgmtResource.en.resx
The way to store error in resource file for example data store in the “LeadMgmtResource.en.resx” file is:
Name Value Comments
E01001 Invalid user name in English Use Case 01 Error Number 001
E01002 Node not present in XML file. Use Case 01 Error Number 002
4) Call data stored in the resource file.
// Create the object of resource file class.
LeadMgmtResourceFile objResourceError = new LeadMgmtResourceFile();
//Value from the Resource file for a particular error code
logResponse = objResourceError.GetResourceError(resource_ErrorId).Trim();


Ritesh Kesharwani
Infosys , Pune