save Datatable values as empty string instead of null
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?
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
Have you tried filling the offending fields with String.Empty
?
精彩评论