2

I am using an SqlDataAdapter to save some values from a table to a database:

private BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
...
dataAdapter.Update((DataTable) bindingSource1.DataSource);

but it is saving empty values as null in the database. Is there anyway to make it save them as empty strings instead?

2
  • 1
    Why would you want to do that? An empty string means that something was actually assigned to the column, while a NULL means it wasn't. By assigning a string, you're breaking basic database operations (standards), and making your data inaccurate (you can't tell if a value was accidentally missed or intentionally cleared). Unless you have an actual business need to do this (and it's not just to be lazy in not having to properly deal with NULL values), this is a bad idea, IMO. Commented Mar 16, 2011 at 11:20
  • I am maintaining an existing system and it needs to be done that way. So I don't have much choice in the matter. Commented Mar 16, 2011 at 11:29

2 Answers 2

3

After some research everyone seems to be doing manually so I wrote a method that does that.

  public static DataTable RemoveNulls(DataTable dt)
    {
        for (int a = 0; a < dt.Rows.Count; a++)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Rows[a][i] == DBNull.Value)
                {
                    dt.Rows[a][i] = "";
                }
            }
        }

        return dt;
    }

and some related links

http://madskristensen.net/post/Remove-nulls-from-a-DataTable.aspx

http://forums.asp.net/t/307989.aspx/1?remove+null+rows+in+data+table

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

Comments

1

Have you tried filling the offending fields with String.Empty?

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.