| |
This email has been checked for viruses by Avast antivirus software.
www.avast.com |
Tuesday, 30 December 2014
Script to generate fiscal calendar
Sunday, 14 December 2014
Change color of validation error on NetTier
// Required field?
if ( Required && !ReadOnly )
{
requiredValidator.Enabled = false;
requiredValidator.ControlToValidate = this.ID;
requiredValidator.ID = this.ID + "_RQ";
requiredValidator.Text = ErrorText;
requiredValidator.ErrorMessage = RequiredErrorMessage;
requiredValidator.ForeColor = Color.Red;
requiredValidator.EnableClientScript = EnableClientScript;
requiredValidator.ValidationGroup = this.ValidationGroup;
this.Controls.Add( requiredValidator );
}
And you are good to go.
Monday, 20 October 2014
SQL Server: Introduction This blog is about how to configure/s...
Sunday, 25 May 2014
IE 10 plus issue with .NET Framework 4.0
Thursday, 27 February 2014
Nettier Codesmith v6.5 update enterprise library from 414 to 505
Get all files in
C:\Users\.......\Documents\CodeSmith Generator\Samples\v6.5\Templates\Frameworks\NetTiers\References\EntLibv5_0
With the updated version
Update file CommonSqlCode.cs found in
\Documents\CodeSmith Generator\Samples\v6.5\Templates\Frameworks\NetTiers\TemplateLib
Replace 414 by 505
Wednesday, 5 February 2014
chrome doesn't support MaintainScrollPositionOnPostBack Solution
To support the scroll position capability in Chrome, you need to follow the steps given below:
1. Add the following line of code in the Page_Load of the page for which you want to maintain the scroll position.
Collapse | Copy Code
this.MaintainScrollPositionOnPostBack = true;
2. Right click on the project.
3. Click on “Add” -> “Add New Item”.
4. In the “Add New Item” window, select “Browser File” and click “Add”.
5. Application will ask you to place this file in “App_Browsers” folder, click “Yes”
6. Now add the capability of maintaining the scroll position as follows:
Collapse | Copy Code
<browsers>
<browser refID="Safari1Plus">
<capabilities>
<capability name="supportsMaintainScrollPositionOnPostback"
value="true" />
</capabilities>
</browser>
</browsers>
Thursday, 30 January 2014
Check if asp Validation pass through clientside
Get KeyValue data from Devexpress Grid
Wednesday, 29 January 2014
objectDatasource binding using storeprocedure
Monday, 27 January 2014
NetTier Exception Error
{
try
{
return ExceptionPolicy.HandleException(e, policyName);
}
catch (System.Configuration.ConfigurationErrorsException)
{
return true;
}
}
{
try
{
Exception ex;
return ExceptionPolicy.HandleException(e, policyName, out ex);
}
catch (System.Configuration.ConfigurationErrorsException)
{
return true;
}
}
Thursday, 16 January 2014
sharepoint Search has encountered a problem
Monday, 13 January 2014
.NET Does and don't for Exception Handling
Author: Daniel Turini
Introduction
"My software never fails". Can you believe it? I'm almost hearing you all, screaming that I'm a liar. "Software that never fails is something near to impossible!"Contrary to common belief, creating reliable, robust software is not something near to impossible. Notice that I'm not referring to bug-free software, intended to control nuclear power plants. I'm referring to common business software, which can run unattended on a server, or even a desktop machine, for long periods of time (weeks or months) and work predictably without any significant glitch. By predictably, I mean that it has a low failure rate, you can easily understand failure conditions to fix it quickly, and it never damages data in response of an external failure.
In other words, software that is stable.
Having a bug in your software is forgivable, and even expected. What's unforgivable is having a recurring bug you can't fix because you don't have enough information.
To understand better what I'm saying, I've seen countless business software that, in an out of disk space error in the DBMS, reports something like this:
"Could not update customer details. Contact the system administrator and try again later".
While this message may be an adequate of reporting an unknown resource failure to a business user, all too often this is the whole debugging information that is available to debug the error cause. Nothing was logged, and understanding what is happening will be time-consuming and often programmers will guess a lot of possible causes until they find the real error cause.
Notice that in this article, I will concentrate only in how to make a better use of .NET exceptions: I won't discuss how to properly report error messages, because I believe this belongs to UI domain, and it depends heavily on the interface being developed and the target audience; a blog text editor targeting teenagers should report error messages in a way completely different than a socket server, which will only be used directly by programmers.
Plan for the worst
Check it early
CustomerID is doing on the ProductID column on the InvoiceItems table after a few months isn't fun neither easy. If you used classes for storing customer data, instead of using primitives (e.g., int, string, etc), chances are that the compiler would never allow you to do such a thing.
Don't trust external data
The only reliable devices are: the video, the mouse and keyboard.
- Not enough security privileges
- The information is not there
- The information is incomplete
- The information is complete, but invalid
Writes can fail, too
- Not enough security privileges
- The device isn't there
- There's not enough space
- The device has a physical fault
Code Safely
Don't throw new Exception()
Exception is a too broad class, and it's hard to catch without side-effects. Derive your own exception class, but derive it from ApplicationException. This way you could set a specialized exception handler for exceptions thrown by the framework and another for exceptions thrown by yourself. System.ApplicationException as a base class in MSDN docs, this is no longer considered a good practice, as noted by Brad Adams, as you can see in his blog. The idea is creating exception class hierarchies that are as shallow and wide as possible, as you often do with class hierarchies. The reason I didn't change the article immediately was because I needed to do more research before I introduced it here. After all this research, I could not decide yet whether shallow class hierarchies are a good idea in Exception handling or not, so I decided to leave both opinions here. But, no matter what you do, don't throw new Exception() and derive your own Exception class when needed.
Don't put important exception information on the Message field
Put a single catch (Exception ex) per thread
Generic Exceptions caught should be published
Log Exception.ToString(); never log only Exception.Message!
Exception.ToString(), and never Exception.Message. Exception.ToString() will give you a stack trace, the inner exception and the message. Often, this information is priceless and if you only log Exception.Message, you'll only have something like "Object reference not set to an instance of an object".
Don't catch (Exception) more than once per thread
There are rare exceptions (no pun intended) to this rule. If you need to catch an exception, always use the most specific exception for the code you're writing.I always see beginners thinking that good code is code that doesn't throw exceptions. This is not true. Good code throws exceptions as needed, and handles only the exceptions it knows how to handle.
As a sample of this rule, look at the following code. I bet that the guy who wrote it will kill me when he read this, but it was taken from a real-world example. Actually, the real-world code was a bit more complicated - I simplified it a lot for didactic reasons.
The first class (
MyClass) is on an assembly, and the second class (GenericLibrary) is on another assembly, a library full of generic code. On the development machine the code ran right, but on the QA machines, the code always returned "Invalid number", even if the entered number was valid.public class MyClass
{
public static string ValidateNumber(string userInput)
{
try
{
int val = GenericLibrary.ConvertToInt(userInput);
return "Valid number";
}
catch (Exception)
{
return "Invalid number";
}
}
}
public class GenericLibrary
{
public static int ConvertToInt(string userInput)
{
return Convert.ToInt32(userInput);
}
}
The problem was the too generic exception handler. According to the MSDN documentation, Convert.ToInt32 only throws ArgumentException, FormatException and OverflowException. So, those are the only exceptions that should be handled.The problem was on our setup, which didn't include the second assembly (
GenericLibrary). Now, we had a FileNotFoundException when the ConvertToInt was called, and the code assumed that it was because the number was invalid.The next time you write "catch
(Exception ex)", try to describe how your code would behave when an OutOfMemoryException is thrown.
Don't ever swallow exceptions
Cleanup code should be put in finally blocks
Ideally, since you're not handling a lot of generic exceptions and have a central exception handler, your code should have a lot more finally blocks than catch blocks. Never do cleanup code, e.g., closing streams, restoring state (as the mouse cursor), outside of a finally block. Make it a habit.One thing that people often overlook is how a try/finally block can make your code both more readable and more robust. It's a great tool for cleanup code.
As a sample, suppose you need to read some temporary information from a file and return it as a string. No matter what happens, you need to delete this file, because it's temporary. This kind of return & cleanup begs for a try/finally block.
Let's see the simplest possible code without using try/finally:
string ReadTempFile(string FileName)
{
string fileContents;
using (StreamReader sr = new StreamReader(FileName))
{
fileContents = sr.ReadToEnd();
}
File.Delete(FileName);
return fileContents;
}
ReadToEnd method: it leaves a temporary file on the disk. So, I've actually saw some people trying to solve it coding as this: string ReadTempFile(string FileName)
{
try
{
string fileContents;
using (StreamReader sr = new StreamReader(FileName))
{
fileContents = sr.ReadToEnd();
}
File.Delete(FileName);
return fileContents;
}
catch (Exception)
{
File.Delete(FileName);
throw;
}
}
The code is becoming complex and it's starting to duplicate code.Now, see how much cleaner and robust is the try/finally solution:
string ReadTempFile(string FileName)
{
try
{
using (StreamReader sr = new StreamReader(FileName))
{
return sr.ReadToEnd();
}
}
finally
{
File.Delete(FileName);
}
}
Use "using" everywhere
Dispose() on an object is not enough. The using keyword will prevent resource leaks even on the presence of an exception.
Don't return special values on error conditions
- Exceptions makes the common case faster, because when you return special values from methods, each method return needs to be checked and this consumes at least one processor register more, leading to slower code
- Special values can, and will be ignored
- Special values don't carry stack traces, and rich error details
- All too often there's no suitable value to return from a function that can represent an error condition. What value would you return from the following function to represent a "division by zero" error?
public int divide(int x, int y)
{
return x / y;
}
Don't use exceptions to indicate absence of a resource
Microsoft recommends that you should use return special values on extremely common situations. I know I just wrote the opposite and I also don't like it, but life is easier when most APIs are consistent, so I recommend you to adhere to this style with caution.I looked at the .NET framework, and I noticed that the almost only APIs that use this style are the APIs that return some resource (e.g.,
Assembly.GetManifestStream method). All those APIs return null in case of the absence of some resource.
Don't use exception handling as means of returning information from a method
Use exceptions for errors that should not be ignored
Login method. If Login fails, or is not called, every other method call will fail. I chose to throw an exception from the Login method if it fails, instead of simply returning false, so the calling program cannot ignore it.
Don't clear the stack trace when re-throwing an exception
try
{
// Some code that throws an exception
}
catch (Exception ex)
{
// some code that handles the exception
throw ex;
}
Why is this wrong? Because, when you examine the stack trace, the point of the exception will be the line of the "throw ex;", hiding the real error location. Try it. try
{
// Some code that throws an exception
}
catch (Exception ex)
{
// some code that handles the exception
throw;
}
What has changed? Instead of "throw ex;", which will throw a new exception and clear the stack trace, we have simply "throw;". If you don't specify the exception, the throw statement will simply rethrow the very same exception the catch statement caught. This will keep your stack trace intact, but still allows you to put code in your catch blocks.
Avoid changing exceptions without adding semantic value
Only change an exception if you need to add some semantic value to it - e.g., you're doing a DBMS connection driver, so the user doesn't care about the specific socket error and wants only to know that the connection failed. If you ever need to do it, please, keep the original exception on the InnerException member. Don't forget that your exception handling code may have a bug too, and if you have InnerException, you may be able to find it easier.
Exceptions should be marked [Serializable]
When in doubt, don't Assert, throw an Exception
Don't forget that Debug.Assert is removed from release code. When checking and doing validation, it's often better to throw an Exception than to put an assertion in your code.Save assertions for unit tests, for internal loop invariants, and for checks that should never fail due to runtime conditions (a very rare situation, if you think about it).
Each exception class should have at least the three original constructors
Doing it is easy (just copy & paste the definitions from other exception classes) and failing to do that won't allow users of your classes to follow some of these guidelines.Which constructors I am referring to? The last three constructors described on this page.
Be careful when using the AppDomain.UnhandledException event
Revision note: I was pointed by Phillip Haack in my blog of this important omission. Other common source of mistakes is the Application.ThreadException event. There are lots of caveats when using them:- The exception notification occurs too late: when you receive the notification your application will not be able to respond to the exception anymore.
- The application will finish if the exception occurred on the main thread (actually, any thread that started from unmanaged code).
- It's hard to create generic code that works consistently. Quoting MSDN: "This event occurs only for the application domain that is created by the system when an application is started. If an application creates additional application domains, specifying a delegate for this event in those applications domains has no effect."
- When the code is running those events, you won't have access to any useful information other than the exception itself. You won't be able to close database connections, rollback transactions, nor anything useful. For beginners, the temptation of using global variables will be huge.
Don't reinvent the wheel
VB.NET
Emulate C# "using" statement
Dim sw As StreamWriter = Nothing
Try
sw = New StreamWriter("C:\crivo.txt")
' Do something with sw
Finally
If Not sw is Nothing Then
sw.Dispose()
End if
End FinallyIf you're doing something different when calling
Dispose, probably you're doing something wrong, and your code can fail and/or leak resources.
Don't use Unstructured Error Handling
On Error Goto. Prof. Djikstra did it very well in 1974 when he wrote "Go To statement considered harmful". It was more than 30 years ago! Please, remove all traces of Unstructured Error Handling from your application as soon as possible. On Error Goto statements will bite you, I'll assure you.
Conclusion
I hope that this article helps someone to code better. More than a closed list of practices, I hope that this article be a starting point for a discussion of how to deal with exceptions in our code, and how to make our programs more robust.I can't believe that I wrote all of this without any mistake or controversial opinion. I'd love to hear your opinion and suggestions about this topic.
