Lookup defined default value for columns from database schemahttp://forums.asp.net/t/1434165.aspx/1?Lookup+defined+default+value+for+columns+from+database+schemaFri, 04 Dec 2009 20:41:04 -050014341653225318http://forums.asp.net/p/1434165/3225318.aspx/1?Lookup+defined+default+value+for+columns+from+database+schemaLookup defined default value for columns from database schema <p>For many&nbsp;columns in our database we have defined default values, and we would like to lookup those default values to display them in textboxes etc. in DetailsViews in insert mode.</p> <p>The DataTableReader.GetSchemaTable method should return a datatable with, among other things, a column named 'DefaultValue' (see <a href="http://msdn.microsoft.com/en-us/library/system.data.datatablereader.getschematable.aspx" rel="nofollow" target="_blank"> <font color="#777777">http://msdn.microsoft.com/en-us/library/system.data.datatablereader.getschematable.aspx</font></a>).<br> <br> I've tried that, but the value reported for DefaultValue is always System.DBNull, even if I know for sure that I should get a &quot;real&quot; default value as defined in the database (like 0 or sometimes 99 for numeric fields).<br> <br> Any ideas how to solve this?</p> <p>Some of my VB.NET source code:</p> <pre class="prettyprint">Public Function GetColumnDefaultValue(ByVal p_sTable As String, ByVal p_sColumn As String) Trace.WriteLine(&quot;wsDataProvider.GetColumnDefaultValue: &quot; &amp; p_sTable &amp; &quot;.&quot; &amp; p_sColumn) Dim l_vDefault As Object = Nothing, l_bError As Boolean = False If (IsColumn(p_sTable, p_sColumn)) Then Dim l_sSql As String If (UsesAccessDB()) Then l_sSql = &quot;select top 1 `&quot; &amp; p_sColumn &amp; &quot;` from `&quot; &amp; p_sTable &amp; &quot;`&quot; Else l_sSql = &quot;select `&quot; &amp; p_sColumn &amp; &quot;` from `&quot; &amp; p_sTable &amp; &quot;` limit 1&quot; End If Try Dim l_oDataSet As DataSet = FillDataSet(l_sSql, &quot;DataColumns&quot;) Dim l_oDataTableReader As DataTableReader = l_oDataSet.CreateDataReader(l_oDataSet.Tables(0)) Dim l_oSchema As DataTable = l_oDataTableReader.GetSchemaTable() l_vDefault = l_oSchema.Rows(0)(&quot;DefaultValue&quot;) Catch ex As Exception System.Diagnostics.Debug.WriteLine(&quot;wsDataProvider.GetColumnDefaultValue: &quot; &amp; ex.Message) ' l_bError = True _oErrorHandler.LogError() End Try End If If l_bError = True Then _oErrorHandler.DisplayErrorPage() End If Return l_vDefault End Function</pre> 2009-06-11T11:15:34-04:003548854http://forums.asp.net/p/1434165/3548854.aspx/1?Re+Lookup+defined+default+value+for+columns+from+database+schemaRe: Lookup defined default value for columns from database schema <p>Yes, I've managed to solve this. I now use the following method on my DataProvider class:</p> <p></p> <pre class="prettyprint">Public Function GetColumnDefaultValue(ByVal p_sTable As String, ByVal p_sColumn As String) 'wsUtils.Trace(&quot;wsDataProvider.GetColumnDefaultValue: &quot; &amp; p_sTable &amp; &quot;.&quot; &amp; p_sColumn) Dim l_vDefault As Object = Nothing, l_bError As Boolean = False Dim l_oErrorHandler As New wsErrorHandler If (IsColumn(p_sTable, p_sColumn)) Then If UsesMySQL() Then Try Dim l_sDBname As String = ConfigurationManager.AppSettings(&quot;wsDBname&quot;) Dim l_sSql As String = &quot;SHOW COLUMNS FROM &quot; &amp; p_sTable &amp; &quot; FROM &quot; &amp; l_sDBname Dim l_oDTcolumns As DataTable = FillDataTable(l_sSql) If l_oDTcolumns IsNot Nothing AndAlso l_oDTcolumns.Rows.Count &gt; 0 Then ' loop through DB table columns for definition Dim l_sColumnName As String For Each l_oDTcolumn As DataRow In l_oDTcolumns.Rows l_sColumnName = l_oDTcolumn(0) If l_sColumnName.ToLower = p_sColumn.ToLower Then l_vDefault = l_oDTcolumn(4) Exit For End If Next ' clean up l_oDTcolumns.Dispose() End If Catch ex As Exception wsUtils.Warn(&quot;wsDataProvider.GetColumnDefaultValue: &quot; &amp; ex.Message) l_bError = True l_oErrorHandler.LogError(ex) End Try Else ' Access DB Try Dim l_oCatalog As New ADOX.Catalog l_oCatalog.ActiveConnection = GetConnection() Dim l_oColumn As ADOX.Column = l_oCatalog.Tables(p_sTable).Columns(p_sColumn) If l_oColumn IsNot Nothing Then l_vDefault = l_oColumn.Properties(&quot;Default&quot;).Value End If Catch ex As Exception wsUtils.Warn(&quot;wsDataProvider.GetColumnDefaultValue: &quot; &amp; ex.Message) l_bError = True l_oErrorHandler.LogError(ex) End Try End If End If If l_bError = True Then l_oErrorHandler.DisplayErrorPage() End If Return l_vDefault End Function</pre> <p><br> Maybe this is of some use to others as well. <img src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0"></p> <p></p> 2009-12-04T20:41:04-05:00