Wednesday, October 27, 2010

The report data source object must be of the type System.Data.DataTable, System.Collections.IEnumerable, System.Windows.Forms.BindingSource, or System.Type.

Problem:
"The report data source object must be of the type System.Data.DataTable, System.Collections.IEnumerable, System.Windows.Forms.BindingSource, or System.Type." when you add an object to ReportDataSource.value

Solution:
If you are adding a business object then you should create a BindingList collection class, and then put the single business object into the collection and set that as the data source.

For example, lets say you have business class Product. Create a class public class ProductCollection : BindingList {}
note: BindingList is part of the System.ComponentModel namespace.

"Index was out of range" error when clicking on a column header in DataGridView

Problem
Clicking on a DataGridView column results in the error message "Index was out of range. Must be non-negative and less than the size of the collection."


Solution 
Make sure you are handling the CellContentClick (and other click events if appropriate) properly. Below is what I had
        private void gvBatches_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0) //then they clicked on the link
            {
                //code that uses e.RowIndex
            }
        }

What this does is when the user clicks on the first column it proceeds to use e.RowIndex regardless of the value, because I assumed that a Column Header would not fire this event, but that's not the case. The e.RowIndex for a column header is -1, so make sure to check if e.RowIndex == -1. For example, i fixed mine and it looks like this now:
        private void gvBatches_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1) //then they clicked on the link
            {
                //code that uses e.RowIndex
            }
        }

References
1. http://www.eggheadcafe.com/community/aspnet/14/10173997/index-out-of-range-error-when-clicking-the-column-header-in-datagridview.aspx 

Monday, October 25, 2010

Deep Copy in C#

Problem
If a modifiable object is ever accessed by multiple users, then you need to do a deep copy to make sure they aren't using the same object.

For example, if you cache an object.

Solution
Use this DeepCopy method:
public object DeepCopy(object obj)
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, obj);    

    object retval;
    ms.Seek(0, SeekOrigin.Begin);
    retval = bf.Deserialize(ms);
    ms.Close();
    return retval;
}


Note: You'll need to add 
"using System.IO;
using System.Runtime.Serialization.Formatters.Binary;"

to whatever file you put this in.
 


Reference  
1. http://weblogs.asp.net/gunnarpeipman/archive/2007/10/07/net-and-deep-copy.aspx 

You know you're on a software project [comic]

http://geekandpoke.typepad.com/geekandpoke/2010/10/advanced-yiw-ommiw.html

Wednesday, October 20, 2010

Copying a file to an external hard drive error "too large for the destination file"

Problem
If your hard drive's file system is not NTFS then your files have size limits and you'll receive a "too large for the destination file" error message.

Solution
Convert your hard drive file system to NTFS.  See this Microsoft article on how to do that: http://windows.microsoft.com/en-US/windows-vista/Convert-a-hard-disk-or-partition-to-NTFS-format

Note: When you follow these instructions it will ask you for your "volume label". To quickly retrieve this type "vol " into the command line. For example: if the hard drive you are converting is C: then type vol c:

References
1. http://forums.techguy.org/windows-vista/704717-too-large-destination-file-system.html
2. http://forums.cnet.com/7723-6122_102-217982.html

Monday, October 18, 2010

How to execute a batch from the current directory even when you run as admin

Problem
When you want a batch file to execute from the same directory that it's located and have to "Run as admin" you'll have a problem because it defaults to "%Windows%system32" instead of the current directory.

Solution
Add these two lines to the top of your batch file
@setlocal enableextensions
@cd /d "%~dp0

Tuesday, October 5, 2010

Type exists in both App_Web_type.#.dll and App_Web_#.dll

Error:
The type ''type" exists in both 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_type.cdcab7d2.7qilrjrp.dll' and 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_a0xipze6.dll'

Solution:
Add batch="false" to web.config node compilation, i.e.
<system.web>...
<compilation batch="false">

References:
http://forums.asp.net/t/1249970.aspx

Friday, October 1, 2010

SQL Server instance not showing up in GP utlities list

Solution:
We created the ODBC manually and pointed to "SQL Server" instead of "SQL Native Client" in the driver list. We fixed this by removing the ODBC with this and recreating one and pointing to SQL Native Client instead of SQL Server.

References:
1. https://community.dynamics.com/forums/t/35862.aspx
There was an error in this gadget