Visual Studio .NET - Forview driving me crazy! Can't do Nullable types

Asked By Joel Hulen
28-Jul-06 01:58 AM
Here is a challenging question, as I have not found any suitable information on this after two days of searching:

Background:
I have a custom Business Class library that includes custom objects with nullable int and DateTime properties. In order to update this data, I have created a FormView control bound to an ObjectDataSource that retrieves a generic List collection of my business objects. The reflection that is intrinsically called by the FormView/ObjectDataSource combination dutifully builds my basic Select, Update, and Insert templates. Now, when you edit the various (TextBoxes by default) input fields and then call the Update method referenced in the ODS (ObjectDataSource), the ODS attempts to convert the data contained in the input fields to the corresponding Type of the property bound to said input field. This is where we run into our little problem...

Even though I have a nullable Integer property type (let's call it int? ClassNumber), if the corresponding TextBox is empty (since it's not required), an exception is thrown by the ODS since it tries to convert an empty string value to an integer value before trying to set the object's Property. Hah! If ODS was able to determine that the integer is in fact nullable, it should pass in a null value instead! But alas, it throws a System.IndexOutOfRangeException: "Index was outside the bounds of the array" error. And further down the stack: "Exception: is not a valid value for Int32". Hmmm. Since this is a FormView control, we don't have the ability to use a BoundField control with 2 very useful properties: NullDisplayText and ConvertEmptyStringToNull. It would be nice to tell ODS to enable sending null values from any control we want. I wonder, can a TextBox control be extended to implement those two values? But I digress...

I suppose I could use the UpdateButton_Click event handler to evaluate the TextBox (or whatever) input controls in question and if the value is empty, input a default value (like zero), but this has Business Rule implications as well as invalidating the reason for using nullable types in the first place. Since I am creating my global Class Library as flexible as possible for use in many different web/desktop/whatever applications, I don't want to start implementing a hundred specialized case handlers, like if the int? value == 0, set it to null, because what if in other cases, 0 (or -1 for that matter) is perfectly acceptable? Once again, the reason for nullable types. Since I know that other developers may very well use a FormView with an ObjectDataSource, it might be handy to provide a custom (extended) FormView, textbox, or ODS control that can handle null values. Unless I am totally missing something else. DetailsView isn't template-able for Updating and I don't like updating from a GridView if there are a lot of fields. DataView is a pain, too.

Does anyone have any better suggestions?

Thanks,
Joel

Solved...  Solved...

14-Jul-07 06:17 AM
I came across your problem and was able to get it working with the simple use of ObjectDataSource parameters.  I posted about it more on my blog http://www.livingincode.com/web/blog/post/FormView-and-ObjectDataSource-with-nullable-types.aspx.

How to null Nullables when ODS won't null.  How to null Nullables when ODS won't null.

05-May-08 02:34 PM
To start, I really agree that ODS not handling nullables is a pain. However a rather simple workaround is
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
            e.Values["FieldThatShouldBeNull"] = null;
 }

As the wizard generated code shows this method hooks up to the FormView.ItemInserting event. After this method, the actual insert method is called. When the object get there all the fields that can be nullable will be nulled.

Hope this helps 2 years later.

Vir
Create New Account
help
Interview Questions for .NET Framework This article is specially for the users those are in development or want to be a .net developer • To test a Web Service you must create a windows application or web application to consume this service? It is True / False? FALSE How many classes can a single.NET DLL contain? Answer1: As many Answer2: One or more What are good ADO.NET object(s) to replace the ADO Recordset object? The differences includes In ADO, the in memory representation of data is the recordset. In ADO.net, it is the dataset A recordset looks like a single table in ADO In contrast, a dataset is a collection of one or more tables in ADO.net ADO is designed primarily for connected access ADO net the disconnected access to the database is used In ADO you communicate with the database by making calls to an OLE DB provider. In ADO.NET you communicate with the database through a data adapter (an OleDbDataAdapter, SqlDataAdapter, OdbcDataAdapter, or OracleDataAdapter
Frequently asked Interview Questions in ADO.Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B B) What is concept of Boxing and Unboxing ? (B) What is the difference between VB.NET and C#? (I) what is the difference between System exceptions and Application exceptions? (I)What is CODE Access security? (I)What is a satellite assembly? (A) How to prevent my .NET DLL to be decompiled? (I) what is the difference between Convert.toString and .toString () method assembly in GAC how do we make a choice? (A)What is CodeDom? Chapter 2: NET Interoperability (I) How can we use COM Components in .NET? (I) We have developed the COM wrapper do we have to still register the COM
with an applications consisting of mixed native and managed code writted in C++ / CLI in Visual Studio 2005. The application runs fine in "Debug mode", however when I compile the application in around a native class. I can also see the following in the output window of Visual Studio 2008: '3DWorkbench.exe' (Managed): Loaded 'c: \ svn \ auto3d \ 3dworkbench \ release \ GcpsDotNetApi.dll', Symbols loaded. First global, static variables (e.g. STL lists) present in "GcpsDotNetApi.dll". After searching on the net for a while, I found a workaround telling me to force the symbol define "_ _DllMainCRTStartup Measurement Systems and Data Analysis SINTEF ICT [http: / / www.sintef.com / omd] VC Language Discussions Visual Studio 2008 (1) Visual Studio 2005 (1) DllMainCRTStartup (1) RtlpNtMakeTemporaryKey (1) RtlInitializeSListHead (1) CrtIsValidHeapPointer (1) VB (1) GcpsDotNetApi (1
visual studio installation problem Actually, my OS is Windows Xp with service pack2.I added service pack3 to install visual studio2010.after that i tryed to installed, but am getting SETUP FAILED due to "Windows XP is not installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates Microsoft Visual F# 2.0 Runtime was not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates Microsoft Visual Studio Macro Tools was not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates .NET Framework 4 Multi-Targeting Pack was not attempted to be installed. [08 / 10 / 11, 14:26:01] VS70pgui: [2] DepCheck indicates Microsoft Visual Studio 2010 Professional - ENU was not attempted to be installed. [08 / 10 / 11, 14:26
If not .Net then what? .NET Framework In a thread about wrapping .Net applications using Thinstall and Xenocode, it was pointed out that there may be better programming run. I would like to use a drag and drop UI development tool like the .Net IDE (or the old VB6) to make development as easy as possible. I am a so reliably. More than one person responding to the previous thread held the opinion that .Net was great for corporate environments where all PCs are strictly regulated, but may not be to use to develop the type of applications that I am trying to develop? jim .NET Discussions Visual Studio .NET (1) Ob6dnVPdnNdTgujanZ2dnUVZ8q2dnZ2d (1) RNidna8kFOAgduvanZ2dnUVZ8uSdnZ2d (1) SQL Server (1) Windows XP (1) UfKdnf2ncKub4ujanZ2dnUVZ8qqlnZ2d (1) WinCE