asp.net - Set default value for NULL in datatable -
I have the following code, which I have created in my project as an extension for datat. It really worked just to be wondering if any customization can be done. Thank you. =)
I understand that this question should be related in some way. Just ask through.
& lt; Extension () & gt; Public Function HasNull (DataTable as ByVal DataTable) Boolean as each column DataColumn as dataTable.Columns then dataTable.Rows.OfType (of DataRow) (). If any (function (r) r.IsNull (column)) again in return for this true termination if the next returned unreal end function & lt; Extension () & gt; Public function SetDefaultForNull (ByVal DataTable each column in each row DataRow dataTable.Rows as as DataTable) DataColumn as dataTable.Columns retarded value as DataTable to as object = row (col) so IsDBNull (value ) Then select the DynamicType.ToString as the dim datatype string = colage. Case Datapart Case "System.DateTime" value = New Date Time Episode "System.Decimal", "System.Int16", "System.Int32", "System .t64 "value = 0 Episode" System.String "Value = String.Empty Case" System.Boolean "value = False Case Else Value = 0 End Select row (col) = value end If you return to the next data datatable and function
So I will keep my comment here, what is this for:
According to the first method: I have to handle the case Input table nothing , either < Return the code> logic afternoon exposure or true / incorrect . Another minor improvement apart: I will loop the rows instead of columns. Consider that all the columns are non-final except that they are non-empty. You loop all the rows for all the columns before you find a null. If you loop those rows that you had determined that in the first line already (if the last column is filled with tap).
Then something like this:
& lt; Extension () & gt; Public function HasNull (so as DataTable DataTable) is nothing as Boolean If DataTable then throw new ArgumentNullException ( "DataTable should start", "DataTable") finished down allColumns = dataTable.Columns.Cast (DataColumn ). To do list (Boolean = Dataable.exe number), as the dim Hanilfield. Anyone (function) allColumns.Any (function (c) row.IsNull (c)) return hasNullField termination function instead of using this extension to get the default value For all types of:
& lt; Extension () & gt; Public function GetDefaultValue (T t.IsValueType as kind as) object comeback Activator.CreateInstance (T) or return to some other end function of the end The second method only It can be implemented in this way, by the way (make it sub ): Dim table as new table. Columns Add ( "ID", GetType (Int32)) table.Columns.Add ( "name", GetType (string)) table.Columns.Add ( "Date", GetType (DATETIME)) table.Rows.Add (1, " Test ", DateTime.Now) table.Rows .Add (DBNull.Value, nothing, nothing is) table.Rows.Add (nothing, DBNull.Value, DBNull.Value) if there are Result: 1 test 25.07.2014 15:05:29 0 01.01.0001 00:00 : 00 0 01.01.0001 00:00:00 Edit : If you actually enter null strings with empty strings If you want to change, then you GetDefaultValue : < / P> > pre & lt; Extension () & gt; Public function GetDefaultValue (T as follows, replaceNullStringsWithEmpty in kind as Boolean) so t.IsValueType comeback Activator.CreateInstance (T) elseif replaceNullStringsWithEmpty andalso t = GetType (string) back then "" Anything else return End if end function However, I do not like the exception of one type, later to differentiate between null and "" It is also impossible that I will do such things as the last step where you display the wire Want to
Comments
Post a Comment