Defaultvalue from database in dataset columns

M

Maurice Mertens

Hi,

I have an Access database in which I defined default values for certain
columns. On my windows forms I've got controls bound to the dataset, The
problem is that when I add a new row to the dataset my controls don't have
the defaultvalue entered in the database.

Is it possible to retrieve the defaultvalue for the column from the
database?


--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
W

William Ryan eMVP

The Datacolumn object has a defaultvalue property.. Say that Column1 in your
Db named FirstCol has a defaultvalue of "1" in Access..

assuming FirstCol was the first field in your SQL Query..

myDataSet.Tables["MyTable"].Column[0].DefaultValue = "1"

You can do that for each one locally so it matches your defaults in the db.
 
M

Maurice Mertens

Hi William,

Indeed it is possible to set them manually but isn't it possible for .NET
to set the defaultvalue of the control on the form automatically with the
defaultvalue from the database.

This because when I change the defaultvalue in de database I don't have
to edit my code.





The Datacolumn object has a defaultvalue property.. Say that Column1
in your Db named FirstCol has a defaultvalue of "1" in Access..

assuming FirstCol was the first field in your SQL Query..

myDataSet.Tables["MyTable"].Column[0].DefaultValue = "1"

You can do that for each one locally so it matches your defaults in
the db. "Maurice Mertens said:
Hi,

I have an Access database in which I defined default values for
certain columns. On my windows forms I've got controls bound to the
dataset, The problem is that when I add a new row to the dataset my
controls don't have the defaultvalue entered in the database.

Is it possible to retrieve the defaultvalue for the column from the
database?


--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502





--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
W

William Ryan eMVP

I may be confused. You are adding new records locally to a datatable and
you want the a given default value to appear when you first add a new row.
For each column that has a default value in the DB, (if your query is Select
first, second, third, forth from MyTable) and first, and forth have a
default of "Default", then do this...

myDatatable.Columns[0].DefaultValue = "Default";
myDatatable.Columns[3].DefaultValue = "Default";

Now when you add a new value you are good to go. Unless I'm
misunderstanding, this should solve the problem. Default values don't
usually change in a db and they're not going to change during a user session
so setting them at the onset shouldn't be a big deal. Moreover, if you were
to leave the defaults out, and submit your update, they'd be added by the DB
when the insert happened and if you had the refresh dataset option on or
insert logic that refreshed for you, the new values would apper in your app.
Functionally, there'd be little difference. However, if you want the fields
in the app to have the default value before they go to the db (so the user
will see exactly what's going to be in the db) , then set the DefaultValue
property in the column to match its counterpart in the db

I admit though that I may be misunderstanding so if I am,please let me know
and I'll do my best.

Cheers,

BIll

Hi William,

Indeed it is possible to set them manually but isn't it possible for .NET
to set the defaultvalue of the control on the form automatically with the
defaultvalue from the database.

This because when I change the defaultvalue in de database I don't have
to edit my code.





The Datacolumn object has a defaultvalue property.. Say that Column1
in your Db named FirstCol has a defaultvalue of "1" in Access..

assuming FirstCol was the first field in your SQL Query..

myDataSet.Tables["MyTable"].Column[0].DefaultValue = "1"

You can do that for each one locally so it matches your defaults in
the db. "Maurice Mertens said:
Hi,

I have an Access database in which I defined default values for
certain columns. On my windows forms I've got controls bound to the
dataset, The problem is that when I add a new row to the dataset my
controls don't have the defaultvalue entered in the database.

Is it possible to retrieve the defaultvalue for the column from the
database?


--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502





--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
M

Maurice Mertens

Hi Bill,

actually I do have a database where the defaultvalue can change. In
Microsoft Access it was possible to change the defaultvalue in the db so
this new defaultvalue would also be the defaultvalue in the application
(Without editing the code in the application).

But it seems like this isn't possible in .NET.

In your example when the defaultvalue from column[0] in the database
changes to "NewDefault", the application will still show "Default" as the
defaultvalue for that column. The next step I have to make is edit the
code in the application (in other words: a version update, I don't want
that).

thx for helping me out



I may be confused. You are adding new records locally to a datatable
and you want the a given default value to appear when you first add a
new row. For each column that has a default value in the DB, (if your
query is Select first, second, third, forth from MyTable) and first,
and forth have a default of "Default", then do this...

myDatatable.Columns[0].DefaultValue = "Default";
myDatatable.Columns[3].DefaultValue = "Default";

Now when you add a new value you are good to go. Unless I'm
misunderstanding, this should solve the problem. Default values don't
usually change in a db and they're not going to change during a user
session so setting them at the onset shouldn't be a big deal.
Moreover, if you were to leave the defaults out, and submit your
update, they'd be added by the DB when the insert happened and if you
had the refresh dataset option on or insert logic that refreshed for
you, the new values would apper in your app. Functionally, there'd be
little difference. However, if you want the fields in the app to have
the default value before they go to the db (so the user will see
exactly what's going to be in the db) , then set the DefaultValue
property in the column to match its counterpart in the db

I admit though that I may be misunderstanding so if I am,please let me
know and I'll do my best.

Cheers,

BIll

--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
W

William Ryan eMVP

Maurice:

I didn't mean to imply that it never changes, but are you saying that the
default value or say the LastName column, where a defualt value might be
"Not Given" might change to Jones, then to Smith then to Ryan all in say one
session?
Maurice Mertens said:
Hi Bill,

actually I do have a database where the defaultvalue can change. In
Microsoft Access it was possible to change the defaultvalue in the db so
this new defaultvalue would also be the defaultvalue in the application
(Without editing the code in the application).

But it seems like this isn't possible in .NET.

In your example when the defaultvalue from column[0] in the database
changes to "NewDefault", the application will still show "Default" as the
defaultvalue for that column. The next step I have to make is edit the
code in the application (in other words: a version update, I don't want
that).

thx for helping me out



I may be confused. You are adding new records locally to a datatable
and you want the a given default value to appear when you first add a
new row. For each column that has a default value in the DB, (if your
query is Select first, second, third, forth from MyTable) and first,
and forth have a default of "Default", then do this...

myDatatable.Columns[0].DefaultValue = "Default";
myDatatable.Columns[3].DefaultValue = "Default";

Now when you add a new value you are good to go. Unless I'm
misunderstanding, this should solve the problem. Default values don't
usually change in a db and they're not going to change during a user
session so setting them at the onset shouldn't be a big deal.
Moreover, if you were to leave the defaults out, and submit your
update, they'd be added by the DB when the insert happened and if you
had the refresh dataset option on or insert logic that refreshed for
you, the new values would apper in your app. Functionally, there'd be
little difference. However, if you want the fields in the app to have
the default value before they go to the db (so the user will see
exactly what's going to be in the db) , then set the DefaultValue
property in the column to match its counterpart in the db

I admit though that I may be misunderstanding so if I am,please let me
know and I'll do my best.

Cheers,

BIll

--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
M

Maurice Mertens

Hi Bill,

I didn't understand the answer first untill I read it more thouroughly.
You said: "..set the dDefaultValue property in the column to match its
counterpart in the db"

How do I do that? I know you can set it manual:
ds.tables(0).columns(0).defaultvalue = "New York", but what if the
defaultvalue in the db changes from "New York" to "L.A.". I will then need
to re-edit my code.


Can the application find out what the defaultvalue in the db is?


Thx!!
 
W

William Ryan eMVP

AFAIK, you can't grab it from the DB, but what I meant was that you can
hard code it. If that won't work, how about this... you could create
another table that holds the columname and the default. I understand you
have a column who's default changes regularly, but I'm guessing it's only
one column or a very few. Regardless. you could query this table when the
app starts, then get those values and store them in a class as static
properties. Then you could dynamically update this. If it changes more
than once in a given user session, then you could even requery in the get
accessor of the class so you always have the latest but if they change this
much then you probably need a much different solution. Anyway, you can set
the default value based on the values in the shared/static class and you'll
be good to go.

If you don't mind me asking, what drives the situation that requires
changing the default value so frequently?

ANother thing, you could do is store this in the .config file. Instead of
changing it in the db, change it in the app (if the app is the only way the
data is accessed, this should be ok, otherwise it's not viable) config file
and then your app will handle it.

Will this work?
 

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