Remove DBNull

If you are getting data from SQL server, it always has a chance that you will get the Null in your table cell. But the meaning of "Null" in database is different than it in the .Net. So how to remove the Null from the resultset getting from the database?
1.Use IsNull() function in your stored procedures.
Select Name, IsNull(Age, 18) From Members
2.Use the following simple method
public DataTable RemoveDBNull(DataTable dt)
  for (int i = 0; i < dt.Rows.Count; i++)
    for (int j = 0; j < dt.Columns.Count; j++)
      if (dt.Rows[i][j] == DBNull.Value)
        dt.Rows[i][j] = 0;
  return dt;

No comments:

Post a Comment