Issues with null values and DetailsView control

Posts   
 
    
pavkata
User
Posts: 2
Joined: 22-Jun-2007
# Posted on: 22-Jun-2007 17:28:01   
            <asp:TemplateField HeaderText="Title" SortExpression="Title">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox0" runat="server" CssClass="BigField" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqTitle" ControlToValidate="TextBox0" runat="server"
                        ErrorMessage="*" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Based On" SortExpression="">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddBasedOn" runat="server" SelectedValue='<%# Bind("EvaluationTemplateID") %>' DataSourceID="dsEvaluationTemplate" DataTextField="Title" DataValueField="ID" AppendDataBoundItems="True">
                        <asp:ListItem Value="" Text="*** Not based on template ***"/>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>

I have two issues:

  1. On save, if the textbox is empty the value won't be saved as null in the database. I figured a workaround in my code by implementing the ItemUpdating event which seems to do trick. Without the code below, the value in the DB will not change from original value... what am I doing wrong?
    protected void dvObject_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        //if value is null it does not get saved into the database for some reason
        foreach (string key in e.NewValues.Keys)
        {
            if (e.NewValues[key] == null)
                e.NewValues[key] = "";
        }
    }
  1. On save, if I select the empty value from the dropdown, e.NewValues["EvaluationTemplateID"] would be null (which is correct) but the null value does not get written to the DB and value remains unchanged from original value. what am I doing wrong?

As a side note, I did a SQL Profiler 2005 trace and only changed fields are saved and the dropdown value (null) gets skipped...

Thanks.

pavkata
User
Posts: 2
Joined: 22-Jun-2007
# Posted on: 22-Jun-2007 17:34:24   

Extra info: Adding ConvertEmptyStringToNull="true" does not help...

<asp:TemplateField HeaderText="Based On" SortExpression="" [b]ConvertEmptyStringToNull="true"[/b]>
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39912
Joined: 17-Aug-2003
# Posted on: 23-Jun-2007 11:31:17   

pavkata wrote:

            <asp:TemplateField HeaderText="Title" SortExpression="Title">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox0" runat="server" CssClass="BigField" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqTitle" ControlToValidate="TextBox0" runat="server"
                        ErrorMessage="*" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Based On" SortExpression="">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddBasedOn" runat="server" SelectedValue='<%# Bind("EvaluationTemplateID") %>' DataSourceID="dsEvaluationTemplate" DataTextField="Title" DataValueField="ID" AppendDataBoundItems="True">
                        <asp:ListItem Value="" Text="*** Not based on template ***"/>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>

I have two issues:

  1. On save, if the textbox is empty the value won't be saved as null in the database. I figured a workaround in my code by implementing the ItemUpdating event which seems to do trick. Without the code below, the value in the DB will not change from original value... what am I doing wrong?
    protected void dvObject_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        //if value is null it does not get saved into the database for some reason
        foreach (string key in e.NewValues.Keys)
        {
            if (e.NewValues[key] == null)
                e.NewValues[key] = "";
        }
    }

Do you have set up the FieldNamesKeepEmptyStringAsValue property for the datasource control, perhaps?

Also, I assume you use the latest build for the runtime libs?

  1. On save, if I select the empty value from the dropdown, e.NewValues["EvaluationTemplateID"] would be null (which is correct) but the null value does not get written to the DB and value remains unchanged from original value. what am I doing wrong?

The entity field EvaluationTemplateID, what's the value of that in the entity? Is it a nullable field?

Frans Bouma | Lead developer LLBLGen Pro