Solved

Default column value of a table - C#

Posted on 2008-06-24
11
3,006 Views
Last Modified: 2013-12-16
Hi

How can i get default value of a table column in C#?

Thanks
Shiju
0
Comment
Question by:Shiju Sasidharan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 16

Expert Comment

by:ororiole
ID: 21858463
There is a property:
if col is a DataColumn
col.DefaultValue can be get or set.
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 21858869
for null value column its not retrieving
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 21858910
if the data table is a result of a select query it is not fetching the default value
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 16

Expert Comment

by:ororiole
ID: 21860332
This DefaultValue has nothing to do with the results of a query. Rather, when you create a new row in a C# data table, this will be the default value of the column.

The value returned from a sql query is controled by sql, not C#.
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 21865378
Is there any other way to get default values of a table column?
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
ID: 21871280
Instinctively speaking, this should be accomplished by the data adapter's FillSchema(), but just realised that MSDN says that this method configures other DataColumn properties, but not DefaultValue. I've never tried it myself to see if this is really so.
But if you are using MySQL, you can query default value from information_schema.
0
 
LVL 7

Expert Comment

by:nsanga
ID: 21873271
why don't you use a separate SQL to get default values.....
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21874224
Do you mean something like this?

   DataSet1.Tables[0].Columns[0].DefaultValue
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 21877612

TheLearnedOne,
yes i tried like that but without data set.
When we fill datatable using a select statement it doesnt fetch default value.

philipjonathan,
Your idea seems good. i will adopt that if there is no other alternative

 
0
 
LVL 16

Expert Comment

by:ororiole
ID: 21886600
shijusn:
I think you may be asking for something that is not readily available, at least in the way you seem to think. As I mentioned before "The value returned from a sql query is controled by sql, not C#" or rather, it is the values in the rows of the table. Whatever values are in a sql table, those are the values you are going to get.

To look at it another way, the "normal" default for a column in T-SQL is NULL, as long as the column accepts NULLs. you CAN put DEFAULT definitions on columns during a CREATE TABLE or ALTER TABLE statement in T-SQL. You didnt mention if you are using SQL Server, but lets run with that. Put simply, what that does is define a DEFAULT value that will be used during an INSERT statement, if no explict value is supplied. If no DEFAULT is defined, a NULL is inserted. IF the column does not accept NULLs in that case, you get an error during the INSERT statement.

So if you are doing a SELECT statement from C#, you may get a NULL, you may get a value that was inserted, or you may get the DEFAULT value that was inserted. You aren't going to know how it got there or what the DEFAULT was.

Finally, there is a way to find out what the default definitions ON THE SQL COLUMN are. A system table, sys.default_constraints, contains the DEFAULT definitions for the entire database. See this link for an example of how to query this table: http://msdn.microsoft.com/en-us/library/ms173758(SQL.100).aspx. If you want to try it, You will probably want to read up on the other links referenced to understand how DEFAULTS are used. This would be a good place to begin: http://msdn.microsoft.com/en-us/library/ms189909(SQL.100).aspx
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 21918953
Well i tried this
SELECT   COLUMN_NAME, COLUMN_DEFAULT FROM  INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = <TableName>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question