473,479 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding new row with default values.

Adding new row with default values.

In order to insert programmatically a new row into a database table, without
direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,
DataTable.Rows.Add and DataAdapter.Update member functions. Before adding
the new row object to the Rows collection, all of the row's fields that do
not accept NULL must be assigned, otherwise an exception is thrown. However,
I do not want to assign values to some of the fields, because they already
have default values, defined using "Design Table" dialog. So, how can I tell
the system "Please use default values for unassigned fields while inserting
this new row"?

Thanks.

Viorel.
Nov 17 '05 #1
2 5645
Viroel,

There is nothing in the framework to do this. What you will have to do
is query the server for the default values of the table, and then set these
yourself.

You can get the information with this query from the database:

select
scol.name, st.name as type, sc.text as [default]
from
sysobjects as so
inner join syscolumns as scol on so.id = scol.id
inner join systypes as st on scol.type = st.type
inner join syscomments as sc on scol.cdefault = sc.id
where
so.xtype = 'U' and
so.name = <table name>

You would have to replace <table name> with the name of the table you
want to get the defaults for. This will give you a result set that would
have the defaults the columns that had them. Once you have that, I would
generate a dynamic query which would perform the cast to the datatype of the
column, and return a single row with those default values. You can then
store this and use it to set the default values of new rows in your data
layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Viorel" <vi****@compudava.com> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Adding new row with default values.

In order to insert programmatically a new row into a database table,
without
direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,
DataTable.Rows.Add and DataAdapter.Update member functions. Before adding
the new row object to the Rows collection, all of the row's fields that do
not accept NULL must be assigned, otherwise an exception is thrown.
However,
I do not want to assign values to some of the fields, because they already
have default values, defined using "Design Table" dialog. So, how can I
tell
the system "Please use default values for unassigned fields while
inserting
this new row"?

Thanks.

Viorel.

Nov 17 '05 #2
Thanks for help.

The solution based on acquiring default values from the server seems for me
rather intricate. I am not sure if it will work in case of default values
defined in a form of SQL expressions (like "NewID()").

The "DefaultValue" member of the DataColumn object also can be considered.

(I think it should be an easier way. As I know, a typical DataAdapter
effectively performs the insert operation using "INSERT INTO" SQL statement.
The library can have a special value, "DbDefault", similar to DbNull,
assignable to any field of new DataRow object. This reserved value will
instruct the library to use the "DEFAULT" SQL keyword in a generated INSERT
statement. In addition, it should be a flag for instructing the library to
use automatically default values for insert operations).

Viorel.

-------------------------------------

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uc**************@TK2MSFTNGP10.phx.gbl...
Viroel,

There is nothing in the framework to do this. What you will have to do is query the server for the default values of the table, and then set these yourself.

You can get the information with this query from the database:

select
scol.name, st.name as type, sc.text as [default]
from
sysobjects as so
inner join syscolumns as scol on so.id = scol.id
inner join systypes as st on scol.type = st.type
inner join syscomments as sc on scol.cdefault = sc.id
where
so.xtype = 'U' and
so.name = <table name>

You would have to replace <table name> with the name of the table you
want to get the defaults for. This will give you a result set that would
have the defaults the columns that had them. Once you have that, I would
generate a dynamic query which would perform the cast to the datatype of the column, and return a single row with those default values. You can then
store this and use it to set the default values of new rows in your data
layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Viorel" <vi****@compudava.com> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Adding new row with default values.

In order to insert programmatically a new row into a database table,
without
direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow, DataTable.Rows.Add and DataAdapter.Update member functions. Before adding the new row object to the Rows collection, all of the row's fields that do not accept NULL must be assigned, otherwise an exception is thrown.
However,
I do not want to assign values to some of the fields, because they already have default values, defined using "Design Table" dialog. So, how can I
tell
the system "Please use default values for unassigned fields while
inserting
this new row"?

Thanks.

Viorel.


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5901
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
5
2774
by: Paul | last post by:
Hi I have a table that currently has 466 columns and about 700,000 records. Adding a new DEFAULT column to this table takes a long time. It it a lot faster to recreate the table with the new...
2
7658
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
5760
by: brenda.stow | last post by:
error msg " An error occured while referencing the object. You tried to run a visual basic procedure that improperly references a property or method of an object" This msg occurs everytime I add a...
10
2312
by: Trevor | last post by:
Hey, I am trying to do this tutorial on the microsoft site : http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dndotnet/html/usingadonet.asp I can get everything to work up to...
6
4395
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
6
6386
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
5
1754
by: rdemyan via AccessMonster.com | last post by:
I have a table with about 80 fields. I'm using an import process to populate the table. It works fine, except for the following: Users generally don't specify values for a lot of numerical...
1
1999
damonreid
by: damonreid | last post by:
Access 2003 Microsoft Windows XP Pro Hey, I am currently pulling my hair out here. I have a Form for adding new projects to a database, the only problem is that when I close the form it doesn't...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.