How do I populate the DataColumn.DefaultValue property from SQLServer?

G

Guest

How do I populate the DataColumn.DefaultValue property from SQLServer
i.e
I have a table in SQLServer. One of its columns has a default value. This column is populated with the default value when I insert a record and don't specifically set a value for the column
When I create a DataTable from the table's schema the corresponing DataColumn's DefaultValue property does not get set. So when I do DataTable.Rows.Add(NewDataRow) with a DataRow that does not specifically set a value for the column the column remains blank.
 
G

Greg

You will need to set this yourself when the DataTable is constructed.

myColumn.DefaultValue = "defaultValue" or something like this.


Dick said:
How do I populate the DataColumn.DefaultValue property from SQLServer?
i.e.
I have a table in SQLServer. One of its columns has a default value. This
column is populated with the default value when I insert a record and don't
specifically set a value for the column.
When I create a DataTable from the table's schema the corresponing
DataColumn's DefaultValue property does not get set. So when I do
DataTable.Rows.Add(NewDataRow) with a DataRow that does not specifically set
a value for the column the column remains blank.
 
G

Greg

I should add, there are properties on DataAdapter that you can set to
retrieve more or less schema info on your Select. MissingSchemaAction and
FillSchema will be your friends here. Pretty sure neither are nice enough
to return the Default Value though.


Dick said:
I don't want to hardcode the column's default value in my code. I want to
set it to the value already assigned to the column in the database. i.e. in
the same way that I don't hardcode the table name, the column names, their
data types, etc., I don't want to hard code their default values either.
Because that would duplicate my business rules in both the database and in
my code!
So, is it possible to effect the way the table's schema is retrieved from
SQLServer such that the default value information is retrieved? Or is there
another way?
 
M

[MSFT]

Hi Richard,

I agree with Greg that we cannot retrieve the default value property with
DataAdapter from SQL server. We can only set the default value in a strong
typed dataset's schema manaully. For example:

<xs:element name="age" type="xs:int" minOccurs="0" default="34" />

When we create a dataset from this schema, it will has a default value for
column "age".

Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top