That’s pretty easy stuff if you know it already.
Take a look on example (TestApp.exe):
static int Main(string[] args)
{
int myCodeNumber = 1;
// Return some code number
return myCodeNumber;
}
and here it is how to catch it in Windows batch file (test.bat):
REM Run app which will return some code number
TestApp.exe
REM If returned code is not == 1 then "do something" else continue to NEXT section
if %errorlevel% NEQ 1 GOTO :NEXT
DO SOMETHING HERE
:NEXT
WebClient class is very handy and useful but has one big gap. There is no build in cookies handling between requests. Luckily somebody wrote briliant solution for this :) http://couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html
With above snippet the WebClient class will be enough for most common web tasks.
Below you find my rich (maybe to rich) commented example of use WebClient class (enhanced) to send data(POST, GET) and retrieve response (webpage source). There are also HttpWebRequest/HttpWebResponse classes example so you can compare both methods.
In my examples I ‘m going to connect to http://www.phpbb.com, login into this community site and after success pull some data from Phpbb User Configuration Panel.
Full code available to download: WebClientEnhancedFetchWebpage Example (84)
So in below example we will login into phpbb.com forum.

Read more…
Very often InnerException of Exception has some valuable details what happens. Quite often InnerException has an InnerException so more error messages to investigate.
Theoretically there can be unlimited number of Inner Exceptions so below are 2 small snippets to get all messages from them. Both snippets do the same but first in a “while method”, second in recursive way.
/// <summary>
/// Gets all inner exceptions messages.
/// </summary>
/// <param name="ex">The exception</param>
/// <returns>Messages from all inner exceptions.</returns>
public static string GetInnerExceptionMessages(Exception ex)
{
Exception inner = ex.InnerException;
string messages = String.Empty;
while (inner != null)
{
messages += inner.Message;
if (!messages.EndsWith("."))
{
messages += ".";
}
inner = inner.InnerException;
}
return messages;
}
And another method as recursive function from:
http://stackoverflow.com
public string GetInnerException(Exception ex)
{
if (ex.InnerException != null)
{
return string.Format("{0} > {1} ", ex.InnerException.Message,GetInnerException(ex.InnerException));
}
return string.Empty;
}
In my projects I often have to implement user interface in two languages: English and Polish. Although localization in .NET is quite easy, one thing was annoying to me: AjaxControlToolkit doesn’t support Polish language.
When my user choose Polish language on the webpage then he still see in Calendar control ‘Today’ word instead Polish version (‘Dzisiaj’). Same thing for several other controls, for example PasswordStrength control. So one day I decide to correct this once and for all. Below I will show you how to localize Ajax Control Toolkit.
To localize AjaxControlToolkit to our langauge (Culture) we will have to do few simple steps. In below examples I use library version for .NET 2.0 but the same steps are in case of Ajax Control Toolkit for .NET 3.5
First step, download library source from http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326
Unzip, and open in Visual Studio AjaxControlToolkit project which is located in directory AjaxControlToolkit in zip file.
Read more…
I have downloaded the newest version of well known free documentation tool called GhostDoc and by chance I notest that company which is a owner of GhostDoc made some very nice free e-book about .NET Coding Guidelines (for C#/VB).
There is about 100 pages of good coding practices. You can download e-book (pdf) here (enter as email whatever you want and download will start)
Below table of contents.
Update: There is also another very good document about code styling. Provided by IDesign company and used by great free VS add-in Code Style Enforcer. You can download this doc here
Read more…
During work with DataSource controls in Visual Studio one of the common question is how to add some custom data as parameter. There are several pre-definied parameters like SessionParameter, ControlParameter, ProfileParameter etc. but often we need to use something else.
So let’s for example create custom parameter for username which will be get from User.Identity.Name
A lot of people use _Selecting event of DataSource control to assign suitable data.
In this scenario you have to add empty parameter.
Next, add this code into _Selecting event:
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters[0] = User.Identity.Name;
}
That’s all, that’s fast but it’s not so elegant and reusable as method shown below.
More elegant way
Read more…
MSDN link:
http://msdn.microsoft.com/en-us/library/ms131092.aspx
And we must remember that .NET DateTime can represent a larger scope of date than the datetime type in SQL Server 2005.
frmOptions formOptions = new frmOptions();
// open form only if is not open already
bool isFormOpen = false;
// iterate through all open forms
foreach (Form frm in Application.OpenForms)
{
if (frm is frmOptions)
{
// open already so just bring it to the front
frm.BringToFront();
isFormOpen = true;
break;
}
}
if (!isFormOpen)
// not open so show it
formOptions.Show();
else
formOptions.Dispose();
When using Webclient to retrieve some file from the web you can face problem with caching your files.
For example: You have a xml file with version number inside. After downloading first time by Webclient class, you changed content of the xml file and start process again. Unfortunately you will receive old content downloaded earlier instead newest content of the file.
That happens because of Microsoft Internet Explorer (as far as I know) Cache settings used by Webclient class.
So to disallow for caching your requests you have to change Weblicent CachePolicy property. You can do it as shown below:
string xmlUrl = "http://myserver.com/xmlfile.xml";
WebClient client = new WebClient();
// prevent file caching by windows
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(
System.Net.Cache.RequestCacheLevel.NoCacheNoStore
);
// read content of file
Stream rssStream = client.OpenRead(xmlUrl);