Gridview RowUpdating NewValues cannot save null
<p>I've been struggling for a while with this problem. In my GridView I am trying to set specific values for columns that are contained in an EntityDataSource. In my model I specifically set the nullable property to true and the column in the database is Nullable, but when I try to save a null value, the system will not save it. Here are snippets of my code:</p>

<p>entityDataSource:</p>

<pre><code>&lt;asp:EntityDataSource ID="edServiceKeys" runat="server" ConnectionString="name=APIEntities" DefaultContainerName="APIEntities" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="ServiceKeys"&gt;&lt;/asp:EntityDataSource&gt;
</code></pre>

<p>GridView:</p>

<pre><code>&lt;asp:GridView ID="dvServiceKeys" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="ServiceKeyId" AutoGenerateColumns="False" DataSourceID="edServiceKeys" OnRowDataBound="dvServiceKeys_RowDataBound" OnRowUpdating="dvServiceKeys_RowUpdating"&gt;
            &lt;Columns&gt;

                &lt;asp:BoundField DataField="Key" HeaderText="Key" SortExpression="Key" /&gt;
                &lt;asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /&gt;
                &lt;asp:TemplateField HeaderText="CreditUnionId" SortExpression="CreditUnionId"&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:DropDownList ID="lstCreditUnionId" AppendDataBoundItems="true" runat="server" DataSourceID="edCreditUnions" DataTextField="Credit_Union_Name" DataValueField="Credit_Union_ID"&gt;
                            &lt;asp:ListItem Value="-1" Text="None"&gt;&lt;/asp:ListItem&gt;
                        &lt;/asp:DropDownList&gt;
                        &lt;asp:HiddenField ID="hfCreditUnionId" Value='&lt;%# Eval("CreditUnionId") %&gt;' runat="server" /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:DropDownList ID="lstRoCreditUnionId" AppendDataBoundItems="true" runat="server" DataSourceID="edCreditUnions" DataTextField="Credit_Union_Name" DataValueField="Credit_Union_ID" Enabled="False"&gt;
                            &lt;asp:ListItem Value="-1" Text="None"&gt;&lt;/asp:ListItem&gt;
                        &lt;/asp:DropDownList&gt;
                        &lt;asp:HiddenField ID="hfRoCreditUnionId" Value='&lt;%# Eval("CreditUnionId") %&gt;' runat="server" /&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Central_ID" SortExpression="Central_ID"&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:DropDownList ID="lstCentralId" AppendDataBoundItems="true" runat="server" DataSourceID="edCentralIds" DataTextField="Central_Name" DataValueField="Central_ID"&gt;
                            &lt;asp:ListItem Value="-1" Text="None"&gt;&lt;/asp:ListItem&gt;
                        &lt;/asp:DropDownList&gt;
                        &lt;asp:HiddenField ID="hfCentralId" Value='&lt;%# Eval("Central_ID") %&gt;' runat="server" /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:DropDownList ID="lstRoCentralId" AppendDataBoundItems="true" runat="server" DataSourceID="edCentralIds" DataTextField="Central_Name" DataValueField="Central_ID" Enabled="false"&gt;
                            &lt;asp:ListItem Value="-1" Text="None"&gt;&lt;/asp:ListItem&gt;
                        &lt;/asp:DropDownList&gt;
                        &lt;asp:HiddenField ID="hfRoCentralId" Value='&lt;%# Eval("Central_ID") %&gt;' runat="server" /&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:CheckBoxField DataField="DingFree" HeaderText="DingFree" SortExpression="DingFree" /&gt;
                &lt;asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /&gt;
            &lt;/Columns&gt;
        &lt;/asp:GridView&gt;
</code></pre>

<p>listener:</p>

<pre><code>protected void dvServiceKeys_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = dvServiceKeys.EditIndex;
        GridViewRow row = dvServiceKeys.Rows[index];
        DropDownList creditUnion = (DropDownList)row.FindControl("lstCreditUnionId");
        DropDownList central = (DropDownList)row.FindControl("lstCentralId");

        if (creditUnion.SelectedValue == "-1")
            e.NewValues["CreditUnionId"] = null;
        else
            e.NewValues["CreditUnionId"] = creditUnion.SelectedValue;

        if (central.SelectedValue == "-1")
            e.NewValues["Central_ID"] = null;
        else
            e.NewValues["Central_ID"] = central.SelectedValue;
    }
</code></pre>

<p>I've tried setting the NewValues to the following:</p>

<pre><code>e.NewValues["Central_ID"] = null;
e.NewValues["Central_ID"] = dbNull.Value;
e.NewValues["Central_ID"] = "";
e.NewValues["Central_ID"] = string.empty;

int? nullInt = null;
e.NewValues["Central_ID"] = nullInt;

Nullable&lt;Int32&gt; nullInt = null;
e.NewValues["Central_ID"] = nullInt;
</code></pre>

<p>I am able to save any value to that column EXCEPT for null. This is a column of type int in the database, or type int? in the code. Also, this is a duplicate question to </p>

<p><a href="http://stackoverflow.com/questions/13447015/how-to-set-field-values-equal-to-null-in-rowupdating">How to set field values equal to null in RowUpdating(...)</a></p>

<p>but unfortunately his solution was not my solution. I've found others, but everyone seems to have found a simple solution, but for my case, none of the solutions seemed to work.</p>
