2

I have a situation where I am using a detailsview with the SQLDataSource to update a record in an SQL Server Database.

My issue is that textfields that are not completed are converted to NULL in the database. This causes issue as the website using third party web services that can not handle to NULL DB value.

Is there a way that if the textfield is left empty that when the update or insert occurs, that the data is converted to "" (blank string) instead of the NULL value?

2 Answers 2

5

Since you are using SQLDataSource, when you insert or update a record, there is a ConvertEmptyStringToNull property of the Insert/update parameters. Set this to false, and set the string to empty, if no value is provided to that control. Look at the example below:

<asp:SqlDataSource ID="SqlDataSource1" runat="server">
        <InsertParameters>
            <asp:ControlParameter ConvertEmptyStringToNull="false" />
        </InsertParameters>
        <UpdateParameters>
            <asp:ControlParameter ConvertEmptyStringToNull="false" />
        </UpdateParameters>
    </asp:SqlDataSource>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I have already added "ConvertEmptyStringToNull" to the SQLDataSource, however it still isnserts the value NULL in the db if the field is empty.
You set it false ? if still entering Null then its strange.
Thanks Muhammad, your solution did work. I was adding the convertemptystringtonull parameter to the details view not the sqldatasource.
0

You could set a default value of "" on the database table column. Or in your SQL select statements you could do something like

SELECT IsNull(ColName,"")
FROM SomeTable

The

2 Comments

I don't think that would work. I would have to write the IsNull SQL for every column returned. This is my SELECT statement: SELECT [subscriberID], [title], [firstName], [surname], [company], [state], [town], [address], [passwordHint], [email], [password], [postcode], [country], [phoneBH], [PhoneAH], [mobile], [dateOfBirth], [notes], [checked], [guid], [nickName], [avatar] FROM [subscribers] WHERE ([subscriberID] = @subscriberID).
I would have to use the IsNull SQL for every column returned. Any of those columns could be NULL and I need to convert them to an empty string when updating. I have to do this because I do not have full access to all the code. I am just adding on to an already working application.

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.