CodeSmith Community
Your Code. Your Way. Faster!

Help - System.ArgumentException: The entity object cannot be null.

Latest post 05-11-2008 10:32 PM by shailensukul. 6 replies.
  • 10-17-2006 1:26 PM

    • jeff
    • Not Ranked
    • Joined on 10-17-2006
    • Posts 6
    • Points 235

    Help - System.ArgumentException: The entity object cannot be null.

    I followed the getting started guide, and did the simple grid against a sqlserv2000 northwind db.

    (see attached "1.gfi") 

    When I hit the Edit, then Update links, I get this error:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    The entity object cannot be null. If using a data bound control, the DataKeyNames property must be configured (i.e. GridView.DataKeyNames or FormView.DataKeyNames).
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: The entity object cannot be null. If using a data bound control, the DataKeyNames property must be configured (i.e. GridView.DataKeyNames or FormView.DataKeyNames).

    Source Error:


    Line 1946:    if ( keys != null && keys.Count < 1 )
    Line 1947:    {
    Line 1948:     throw new ArgumentException("The entity object cannot be null. If using a data bound control, the DataKeyNames property must be configured (i.e. GridView.DataKeyNames or FormView.DataKeyNames).");
    Line 1949:    }
    Line 1950:    
     

    Source File: C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs    Line: 1948

    Stack Trace:


    [ArgumentException: The entity object cannot be null. If using a data bound control, the DataKeyNames property must be configured (i.e. GridView.DataKeyNames or FormView.DataKeyNames).]
       Northwind.Web.Data.BaseDataSourceView`2.ValidateEntity(Entity entity, IDictionary keys) in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs:1948
       Northwind.Web.Data.BaseDataSourceView`2.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs:1564
       System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +179
       System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1140
       System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +835
       System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +162
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +118
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +107
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3837

     --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

     CODE:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Northwind.Website._Default" %>

    @ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Northwind.Website._Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    head runat="server">

    <title>Home Page</title>

    </head>

    <body>

    <title>Home Page</title>

    </head>

    <body>

    head>

    <body>

    body>

    <form id="form1" runat="server" >

    <form id="form1" runat="server" >

    <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True" />

    <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True" />

    <div>

    <div>

    <asp:GridView

    ID="GridView1"

    runat="server"

    <asp:GridView

    ID="GridView1"

    runat="server"

    ID="GridView1"

    runat="server"

    runat="server"

    DataSourceID="EmployeeDS"

    Font-Names="Verdana"

    Font-Size="Smaller"

    AutoGenerateColumns="False">

    DataSourceID="EmployeeDS"

    Font-Names="Verdana"

    Font-Size="Smaller"

    AutoGenerateColumns="False">

    Font-Names="Verdana"

    Font-Size="Smaller"

    AutoGenerateColumns="False">

    Font-Size="Smaller"

    AutoGenerateColumns="False">

    AutoGenerateColumns="False">

    <Columns>

    <Columns>

    <asp:CommandField ShowSelectButton="True" />

    <asp:CommandField ShowSelectButton="True" />

    <asp:CommandField ShowEditButton="True" />

    <asp:CommandField ShowEditButton="True" />

    <asp:CommandField ShowDeleteButton="True" />

    <asp:CommandField ShowDeleteButton="True" />

    <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"

    <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"

    ReadOnly="True" SortExpression="EmployeeID" />

    ReadOnly="True" SortExpression="EmployeeID" />

    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />

    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />

    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />

    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />

    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />

    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />

    <asp:BoundField DataField="EntityState" HeaderText="EntityState" SortExpression="EntityState" />

    <asp:BoundField DataField="EntityState" HeaderText="EntityState" SortExpression="EntityState" />

    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

    <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />

    <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />

    <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

    <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

    </Columns>

    </Columns>

    </asp:GridView>

    </asp:GridView>

     

    <data:EmployeesDataSource

    id="EmployeeDS"

    <data:EmployeesDataSource

    id="EmployeeDS"

    id="EmployeeDS"

    SelectMethod="GetAll"

    SelectMethod="GetAll"

    EnablePaging="true"

    EnableSorting="true"

    EnablePaging="true"

    EnableSorting="true"

    EnableSorting="true"

    runat="server" >

    runat="server" >

    </data:EmployeesDataSource>

    </data:EmployeesDataSource>

    </div>

    </div>

    </form>

    </form>

    <script type="text/xml-script">

    <script type="text/xml-script">

    <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">

    <references>

    </references>

    <components>

    </components>

    </page>

    </script>

    </body>

    </html>

    "http://schemas.microsoft.com/xml-script/2005">

    <references>

    </references>

    <components>

    </components>

    </page>

    </script>

    </body>

    </html>

    </script>

    </body>

    </html>

    body>

    </html>

    html>

    • Post Points: 65
  • 10-17-2006 3:01 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Help - System.ArgumentException: The entity object cannot be null.

    The error is stating that you need to add the DataKeyNames property to your GridView.  i.e.

    DataKeyNames="EmployeeId"

    where EmployeeId is the PK property of your entity object.

    Hope that helps.
     


    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 10-23-2006 2:45 PM In reply to

    • jeff
    • Not Ranked
    • Joined on 10-17-2006
    • Posts 6
    • Points 235

    Re: Help - System.ArgumentException: The entity object cannot be null.

    See attached.  The moment I set the DataKeyNames, the grid errors.

     Are u sure this isn't a bug to the underlying typed dataset? --> http://forums.asp.net/1048212/ShowPost.aspx .

     Now, when I edit and then Update:

    Server Error in '/Northwind.Website' Application.

    Invalid cast from 'System.String' to 'Northwind.Entities.EntityState'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Northwind.Entities.EntityState'.

    Source Error:

    Line 540:				else
    Line 541:				{
    Line 542:					newValue = Convert.ChangeType(value, conversionType);
    Line 543:				}
    Line 544:			}

    Source File: C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs    Line: 542

    Stack Trace:

    [InvalidCastException: Invalid cast from 'System.String' to 'Northwind.Entities.EntityState'.]
       System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +864
       System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) +8
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +433
       System.Convert.ChangeType(Object value, Type conversionType) +38
       Northwind.Entities.EntityUtil.ChangeType(Object value, Type conversionType, Boolean convertBlankToNull) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:542
       Northwind.Entities.EntityUtil.SetPropertyValue(Object item, String propertyName, Object propertyValue, PropertyInfo& property, Boolean convertBlankToNull) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:377
       Northwind.Entities.EntityUtil.SetPropertyValue(Object item, String propertyName, Object propertyValue, Boolean convertBlankToNull) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:348
       Northwind.Entities.EntityUtil.SetPropertyValue(Object item, String propertyName, Object propertyValue) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:335
       Northwind.Entities.EntityUtil.SetEntityValues(Object entity, IDictionary values) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:424
       Northwind.Web.Data.BaseDataSourceView`2.SetPropertyValues(Entity entity, IDictionary values) in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs:1964
       Northwind.Web.Data.BaseDataSourceView`2.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs:1577
       System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +179
       System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1140
       System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +835
       System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +162
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +118
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +107
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

    Server Error in '/Northwind.Website' Application.

    Invalid cast from 'System.String' to 'Northwind.Entities.EntityState'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Northwind.Entities.EntityState'.

    Source Error:

    Line 540:				else
    Line 541:				{
    Line 542:					newValue = Convert.ChangeType(value, conversionType);
    Line 543:				}
    Line 544:			}

    Source File: C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs    Line: 542

    Stack Trace:

    [InvalidCastException: Invalid cast from 'System.String' to 'Northwind.Entities.EntityState'.]
       System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +864
       System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) +8
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +433
       System.Convert.ChangeType(Object value, Type conversionType) +38
       Northwind.Entities.EntityUtil.ChangeType(Object value, Type conversionType, Boolean convertBlankToNull) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:542
       Northwind.Entities.EntityUtil.SetPropertyValue(Object item, String propertyName, Object propertyValue, PropertyInfo& property, Boolean convertBlankToNull) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:377
       Northwind.Entities.EntityUtil.SetPropertyValue(Object item, String propertyName, Object propertyValue, Boolean convertBlankToNull) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:348
       Northwind.Entities.EntityUtil.SetPropertyValue(Object item, String propertyName, Object propertyValue) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:335
       Northwind.Entities.EntityUtil.SetEntityValues(Object entity, IDictionary values) in C:\NetTiers\Northwind\Northwind.Entities\EntityUtil.cs:424
       Northwind.Web.Data.BaseDataSourceView`2.SetPropertyValues(Entity entity, IDictionary values) in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs:1964
       Northwind.Web.Data.BaseDataSourceView`2.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs:1577
       System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +179
       System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1140
       System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +835
       System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +162
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +118
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +107
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210


    • Post Points: 35
  • 10-24-2006 1:35 AM In reply to

    • mcquiggd
    • Top 25 Contributor
    • Joined on 07-11-2006
    • Amsterdam
    • Posts 174
    • Points 4,375

    Re: Help - System.ArgumentException: The entity object cannot be null.

    Hi there, 

    that seems to be caused by what tends to be referred to as 'Naming Collision' or 'Type Confusion'.

    Basically, my thoughts are these : you have a Column called 'EntityState'. It just so happens that 'EntityState' is also used internally by the .NetTiers architecture - both as a variable and an Enum, which is declared in the Entities Project. Presumably your Column 'EntityState' is a VarChar or similar, which is mapped to a .Net String Type. Hence the conversion error from String to NorthWind.Entities.EntityState... it's trying to convert a String to an Enum. The same sort of behaviour will occur if you use C# reserved words as column names... the most common one that causes problems is 'Type'. Sometimes the Solution won't compile due to this.

    To test if this is the case, backup your database, rename the column, and regenerate.into a different directory. At the moment there is no way to 'alias' column names as you can do with tables, so it really is down to renaming the column. Perhaps EntityStatus would be suitable instead?

     

     

    David (Could people PLEASE click the 'Mark As Answer' button on the message that solved their problems - it will help others with similar questions and could potentially be included in an FAQ to save time for all of us)
    • Post Points: 35
  • 10-24-2006 7:16 AM In reply to

    • jeff
    • Not Ranked
    • Joined on 10-17-2006
    • Posts 6
    • Points 235

    Re: Help - System.ArgumentException: The entity object cannot be null.

    Hi,  

    The EntityState is a column of the datasource object itself, but not found anywhere within the Northwind database.  When I removed that "column" from being used/displayed in the grid all was well. 

    So the problem is/was the "EntityState" "column" of the datasource is available for the grid, but not handled well, if it is used?

     -Jeff

    • Post Points: 35
  • 10-24-2006 9:00 AM In reply to

    • mcquiggd
    • Top 25 Contributor
    • Joined on 07-11-2006
    • Amsterdam
    • Posts 174
    • Points 4,375

    Re: Help - System.ArgumentException: The entity object cannot be null.

    Ah, well, now we are getting somewhere Big Smile


    Don't use a BoundColumn for 'EntityState' - change it to a Template Column as follows (The Label is optional - you could use a TextBox if you so wished but the Label makes more sense):

     <asp:TemplateField HeaderText="EntityState" SortExpression="EntityState">

    <ItemTemplate>

    <asp:Label ID="EntityState" runat="server" Text='<%# Eval("EntityState") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

    The key bit is to use Eval rather than Bind, so the value is not supplied during an Update call, and will not cause the conversion routine to be called.

    I just tested this in my own app and it replicated your original problem; changing the BoundField to the TemplateField above worked fine.

    David (Could people PLEASE click the 'Mark As Answer' button on the message that solved their problems - it will help others with similar questions and could potentially be included in an FAQ to save time for all of us)
    • Post Points: 5
  • 05-11-2008 10:32 PM In reply to

    • shailensukul
    • Top 500 Contributor
    • Joined on 10-30-2006
    • Melbourne, Australia
    • Posts 11
    • Points 140

    Re: Help - System.ArgumentException: The entity object cannot be null.

    I got a similar error except that DataKeyNames was not mentioned.
    I did not use the reserved EntityId key name as suggested by another poster.

     After unsuccessfully trying to resolve this, I put a breakpoint in C:\NetTiers\Northwind\Northwind.Web\Data\BaseDataSource.cs on the offending line of code. When the breakpoint was hit, the code started working!

    Maybe the compiled version of the Data llibrary was out of date?

    So for others experiencing similar problems try this if nothing else works.

     Regards

    Shailen Sukul 

    With Regards Shailen Sukul (Software Architect/Developer) BSc Mcpd Mcts (Web, Win, Dis Apps) Mcsd.Net Mcsd Mcad http://www.ashlen.com.au
    • Post Points: 5
Page 1 of 1 (7 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems