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

Tuesday, June 06, 2006

New Data Type in Oracle 9i which allows virtually store any data type

New Data Type in Oracle 9i which allows virtually store any data type
ANYDATA
ANYDATASET
Oracle version 9 introduced interesting new data types, which allow developers to declare a variable that can contain any type of data. The data types are ANYDATA for a single item, ANYDATASET for a TABLE or VARRAY of data, and ANYTYPE, which describes the type of data stored in ANYDATA or ANYDATASET variables and columns. These data types are important for processing XML data stored in the database, or for Advanced Queues. The documentation mentions that the ANYDATA data type can be used to serialize objects as well.


Ritesh Kumar Kesharwani
Cell : 91-9890901287



How low will we go? Check out Yahoo! Messenger’s low PC-to-Phone call rates.

Wednesday, April 19, 2006

Convert String to Date and check Date Validation on Server side in C#.Net.

Convert String to Date and check Date Validation on Server side in C#.Net.

There are lots of functions available in client side for date validation.
Sometime server side date validation function require
It’s very simple but require much time to right. (C#.Net)

SupportedDateFormats : It is a customise function where you can refer the date format whatever you want.

ConvertStringToDate : This will return True or Flase based on date format supported. And give converted Date from String.

public static bool ConvertStringToDate(string dateString, ref DateTime resultDate)
{
try
{
resultDate = DateTime.ParseExact(
dateString,
SupportedDateFormats(),
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None
);
return true;
}
catch
{
return false;
}
}

//This is custom defined format function you can add more date format as per your requirement
like "dd/MM/YYYY", etc in the allFormats array
private static string[] SupportedDateFormats()
{
string[] allFormats = new string[] {
"MM/dd/yyyy",
"MM/dd/yy",
"M/d/yy",
"MM/d/yy",
"M/dd/yy",
"M/d/yyyy",
"MM/d/yyyy",
"M/dd/yyyy"};
return allFormats;
}

// Now you can use this function in your code like this
private void button1_Click(object sender, EventArgs e)
{
DateTime dtTemp;
dtTemp =DateTime.Today.Date;
lblIsvalidDate.Text =
ConvertStringToDate(
textBox2.Text,
ref dtTemp
).ToString();
lblCovertedDate = dtTemp.ToString();
}

Note:
If you want this code in VB.Net I would like to tell you very good site that will

Different between lifecycle of Windows services and Standard EXE

Different between lifecycle of Windows services and Standard EXE

"Windows services" lifecycle is managed by "Service Control Manager" which is responsible for starting and stopping the service and the applications do not have a user interface or produce any visual output. Windows services are ideal for use in server environments or need long-running applications/functionality. Any user messages are typically written to the Windows Event Log. Windows Services can be automatically started when the computer is booted.

"Standard executable" doesn't require Control Manager and is directly related to the visual output

Happy coding ....
Arti Gupta

Nucleus Software , Noida

Tuesday, April 18, 2006

Strong type Vs Weak type and Debug build Vs Release build

Strong type Vs Weak type and Debug build Vs Release build
Simple way to remember different between these type and build
Strong type: Checking the types of variables at compile time
Weak type: Checking the types of variables at run time.
Strong type is checking the types of variables as soon as possible, usually at compile time. While weak typing is delaying checking the types of the system as late as possible, usually to run-time. Which is preferred depends on what you want. For scripts & quick stuff you’ll usually want weak typing, because you want to write as much less code as possible. In big programs, strong typing can reduce errors at compile time.
Debug build: Code can debugged, containing debug symbol (.pdb file)
Release build: Code can not debugged, it contain debug symbol
Debug build contain debug symbols and can be debugged while release build doesn’t contain debug symbols, doesn’t have [Conational (”DEBUG”)] methods calls compiled, can’t be debugged, less checking, etc. There should be a speed difference, because of disabling debug methods, reducing code size etc but that is not a guarantee (at least not a significant one)

Ritesh Kesharwani

Infosys,Pune




Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates starting at 1¢/min.

Wednesday, April 12, 2006

How to Add Custom Tag in Web.Config Page (ASP.NET)

How to Add Custom Tag in Web.Config Page (ASP.NET)
To add a custom tag to Web.config you need to first explicitly specify the new tag name in Web.config via the <configSections> tag
For Example:

<configuration>

<configSections>

<section name="MyAppSettings"
type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />

</configSections>
...
</configuration>

This <section ... /> tag indicates that we are going to be adding New tag named MyAppSettings.
Now we can add a <MyAppSettings> tag to our Web.config file and
add <add ... /> tags to add application-wide parameters,
For Example:

<configuration>

<configSections>

<section name="MyAppSettings"
type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</configSections>

<MyAppSettings>
<add key="connString" value="connection string" />
</MyAppSettings>
...
</configuration>
How to Read Custom Tag

To read this custom value from an ASP.NET Web page we use the following syntax:

ConfigurationSettings.GetConfig("MyAppSettings")("connString")


Ritesh Kumar Kesharwani
Infosys,Pune

Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.
Great rates starting at 1¢/min.

Wednesday, March 01, 2006

Method Hiding and Use of Virtual Keyword in C#.NET

1) Method Hiding

class Base
{
public Base() { Console.Write("Base constructor \n");}
public void show() {Console.Write("Base Show \n");}
}
class Child : Base
{
public Child() { Console.Write("Child constructor \n");}
public void show() {Console.Write("Child Show \n");}
// It will give you the compilation Warning
// Put NEW keyword for Intentionally "Method Hiding" No Warning will come
// public new void show() {Console.Write("Child Show \n");}
}
class Class1
{
static void Main(string[] args)
{
Base b1 = new Base();
b1.show(); // 1) Base constructor 2) Base Show
Child c1 = new Child();
c1.show(); // 1) Base constructor 2) Child constructor 3) Child Show
Base b11 = new Child();
b11.show(); // 1) Base constructor 2) Child constructor 3) Base Show
//Child c11 = new Base();
// give compilation Error Can't explict conversion
//Child c11 = (Child) new Base();
// if you do explicit type conversion then run time error say "Invalid Conversion"
//c11.show();
Console.ReadLine();
}
}
1) Virtual Keyword
class Base
{
public Base() { Console.Write("Base constructor \n");}
public virtual void show() {Console.Write("Base Show \n");}
}
class Child : Base
{
public Child() { Console.Write("Child constructor \n");}
public override void show() {Console.Write("Child Show \n");}
}
class Class1
{
static void Main(string[] args)
{
Base b1 = new Base();
b1.show(); // 1) Base constructor 2) Base Show
Child c1 = new Child();
c1.show(); // 1) Base constructor 2) Child constructor 3) Child Show
Base b11 = new Child();
b11.show(); // 1) Base constructor 2) Child constructor 3) Child Show
//Child c11 = new Base();
// give compilation Error Can't explict conversion
//Child c11 = (Child) new Base();
// if you do explicit type conversion then run time error say "Invalid Conversion"
//c11.show();
Console.ReadLine();
}
}


Ritesh Kumar Kesharwani

A D I T I , B A N G A L O R E
Software Professional





Relax. Yahoo! Mail virus scanning helps detect nasty viruses!