Archive for the ‘C#’ Category

How to add .snk file to Project from Common folder

In the current application, I have nearly 200 projects and needs to be strong named all of them to ensure the best to the project.

I have used the below steps to ensure that the team needs to use only one .snk file across the application projects. In general, if you strong name the appliation in a single project, the copy of the .snk file will be created in each project.

With the below, you don’t need to have multiple copies and multiple public key tokens for the same version of the project.

Assumption: You already have the .snk file created

  1. Create a project in Visual Studio.
  2. Right click on the project name and select existing item.
  3. Image
  4. Add an existing .snk file to the project as a link.
  5. Image
  6. Refer the added .snk file from the signing tab.
  7. Image
  8. Build the project.
  9. You need to repeat the same steps for all the projects in your application.

You are good to go..!!

Hope this helps. Happy coding.. ūüôā

~ Gangadhar Kotu


Arithmetic Operation Resulted In An Overflow

When I was executing the Stored Procs in Oracle from C# (using Oracle.DataAccess.dll – v or 11gR2 client as well), I’m receiving the below error.

“Arithmetic Operation Resulted In An Overflow.”

Couldn’t figure it out why it is happening all of sudden.Verified all the code and nothing has been changed but observed that the data in the Database has got updated.

Tried to figure it out why the data is introducing new errors and understood that the OracleClient is using Int32 as it’s parameter to convert the data from Cursor to DataTable.

Possible Solutions:

1. Data in the column coming more than Int32 value.
2. Length of the number Data in the column is more than 28 digits (excluding the decimal point), means 99999.999 is 8 digits.
3. Length of the decimal values are more than 16 digits. (eg., 123.12345678901234567)

FYI, stack trace:

at Oracle.DataAccess.Types.DecimalConv.GetDecimal(IntPtr numCtx)
at Oracle.DataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
at Oracle.DataAccess.Client.OracleDataReader.GetValue(Int32 i)
at Oracle.DataAccess.Client.OracleDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
at System.Data.DataTable.Load(IDataReader reader)
at DataHelper.RetrieveOutputCursor(OracleCommand cmd, Hashtable htResults) in C:\DataHelper.cs:line 294
at DataHelper.ExecuteProcedureReturnDataSet(String procName, Hashtable spParams) in C:\DataHelper.cs:line 208

Check it out for this possibility as well. ūüôā

– Gangadhar Kotu

Unable to start debugging in Silverlight 3 for VS 2008

Hi All,

Following is the error I got when try to debug my sample silverlight 3 application.

“unable to start debugging.the silverlight managed debugging package isn’t installed”

At first time debugging works fine. I upgraded Silverlight 3 runtime into Silverlight 4. Then I got the above error while debugging.

That error indicates that I have the end-user runtime installed, not the developer runtime. The dev runtime is available here:

Then I have downloaded the Developer runtime for Silverlight 3. That fixes my problem of debugging but still I’m receiving the another error.

Unhandled Error in Silverlight Application
Code: 2104   
Category: InitializeError      
Message: Could not download the Silverlight application. Check web server settings.

To correct the above issue, check the below IIS settings if you are running the app from IIS and ensure to have the .xap file in the ClientBin folder of the web application.

.xaml    application/xaml+xml
.xap    application/x-silverlight-app
.xbap    application/x-ms-xbap

~ Gangadhar Kotu

OracleCommandBuilder.DeriveParameters doesn’t return Stored Procedure definition

We were in a situation to migrate the Oracle database from one data center to another data center. The migration activities are completed and felt it is succeeded.

We observed the existing functionality which uses OracleCommandBuilder.DeriveParameters method is not return any parameter information of the Stored Procedure we are looking for. Hence tried with multiple functionality. No luck and spend a day on this as well.

Felt like some privileges issue and contacted the DBA teams as well. Later found that the Oracle packages were in INVALID state due to migration. So, Compiled the packages and the status becomes VALID.

There you go, the functionality started working.

In this case, the packages were not in a compiled (valid) state. Once we execute the Oracle package, everything went fine.

This is one possible solution if you are facing the similar issue.

Good luck..!!

Gangadhar Kotu

Best Practices for Handling Exceptions

A well-designed set of error handling code blocks can make a program more robust and less prone to crashing because the application handles such errors. The following list contains suggestions on best practices for handling exceptions:
  1. Know when to set up a try/catch block. For example, you can programmatically check for a condition that is likely to occur without using exception handling. In other situations, using exception handling to catch an error condition is appropriate.
  2. Use try/finally blocks around code that can potentially generate an exception and centralize your catch statements in one location. In this way, the try statement generates the exception, the finally statement closes or deallocates resources, and the catch statement handles the exception from a central location.
  3. Always order exceptions in catch blocks from the most specific to the least specific. This technique handles the specific exception before it is passed to a more general catch block.
  4. End exception class names with the word “Exception”. For example:
    public class MyFileNotFoundException : Exception {
  5. In C#, use at least the three common constructors when creating your own exception classes. For an example, see How to: Create User-Defined Exceptions.
  6. In most cases, use the predefined exceptions types. Define new exception types only for programmatic scenarios. Introduce a new exception class to enable a programmer to take a different action in code based on the exception class.
  7. For most applications, derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.
  8. Include a localized description string in every exception. When the user sees an error message, it is derived from the description string of the exception that was thrown, rather than from the exception class.
  9. Use grammatically correct error messages, including ending punctuation. Each sentence in a description string of an exception should end in a period.
  10. Provide Exception properties for programmatic access. Include extra information in an exception (in addition to the description string) only when there is a programmatic scenario where the additional information is useful.
  11. Return null for extremely common error cases. For example, Open returns null if the file is not found, but throws an exception if the file is locked.
  12. Design classes so that an exception is never thrown in normal use. For example, a FileStream class exposes another way of determining whether the end of the file has been reached. This avoids the exception that is thrown if you read past the end of the file. The following example shows how to read to the end of the file.class FileRead {
    public void Open(FileStream fileToRead)
    // This if statement is optional
    // as it is very unlikely that
    // the stream would ever be null.
    if (fileToRead == null)
    throw new System.ArgumentNullException();

    int b;

    // Set the stream position to the beginning of the file.
    fileToRead.Seek(0, SeekOrigin.Begin);

    // Read each byte to the end of the file.
    for (int i = 0; i < fileToRead.Length; i++)
    b = fileToRead.ReadByte();
    // Or do something else with the byte.

  13. Throw an InvalidOperationException if a property set or method call is not appropriate given the object’s current state.
  14. Throw an ArgumentException or a class derived from ArgumentException if invalid parameters are passed.
  15. The stack trace begins at the statement where the exception is thrown and ends at the catch statement that catches the exception. Be aware of this fact when deciding where to place a throw statement.
  16. Use exception builder methods. It is common for a class to throw the same exception from different places in its implementation. To avoid excessive code, use helper methods that create the exception and return it.
Source: MSDN, CodeProject
~ Gangadhar Kotu