1

I have a textbox in a formview that gets its data from a linqdatasource which is used to enter a date:

<SP:TextBox ID = "StartDate" 
            Type = "Date" 
            Runat = "server" 
            Text = '<%# Bind("StartDate", "{0:yyyy-MM-dd}") %>' />

It works fine, except when StartDate is a null value in the database. Then the StartDate textbox is empty which is correct, but when the empty field is saved back to the database the date 0001-01-01 is stored instead of a null value which is wrong. How can I detect this scenario and turn the empty string into a null value before it is saved?

2 Answers 2

4

The solution is to add parameters for the nullable columns and set ConvertEmptyStringToNull to true:

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
    ContextTypeName="NullUpdateRepo.DatabaseLinqDataContext" EnableUpdate="True"
    TableName="SampleTables" OnSelected="OnSelected" OnContextCreated="OnContextCreated">
    <UpdateParameters>
        <asp:Parameter Name="firstname" Type="String" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="lastname" Type="String" ConvertEmptyStringToNull="true" />
    </UpdateParameters>
</asp:LinqDataSource> 

http://connect.microsoft.com/VisualStudio/feedback/details/296161/linqdatasource-and-null-column-values

Sign up to request clarification or add additional context in comments.

Comments

0

In the BackEnd While Inserting Date value give like this:

   CASE WHEN StartDate='0001-01-01' THEN NULL ELSE StartDate END

It will store NULL in the Table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.