Reputation: 15061
I have created a SQL table in Visual studio and set a default value on a field.
The table is connected to my C# project (in the same solution) and pulls through fine, however the default values are not appearing when a new row is created via a windows form.
Info from comment questions:
Upvotes: 3
Views: 1569
Reputation: 37368
Based on this W3Schools Tutorials
The default value will be added to all new records IF no other value is specified.
So the Default value will not show until you save the row to the database, you can add it programatically, but the Default value constraint is used to assign a value if there is no values provided in this column
You can save these default values in the application Settings and use it when new row is added
textBox1.Text = Properties.Settings.Default.TextBoxDefaultValue;
If you are facing an issue when defining default value in Visual Studio try adding a Default Constraint using SQL query:
ALTER TABLE XXX
ADD CONSTRAINT def_Retired
DEFAULT 'N' FOR Retired;
UPDATE 1
On the form designer, click on the BindingNavigator, In the Properties window set the AddNewItem
property to (none)
Try adding the following code to the bindingNavigatorAddNewItem_Click
event
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) {
vehiclesBindingSource.AddNew();
Textbox1.Text = Properties.Settings.Default.Text1default;
};
Upvotes: 3
Reputation: 14251
It doesn't appear on the form when I click the + for a new row.
The DataGridView
does not know anything about default values in the database table. Accordingly, you must specify the default values in the DataGridView
.
Or if you are using data binding, you can specify default values in the DataTable
.
When a row is added from the database the defaults are there.
It's right, because in the database they are set.
Added a new row via the form, left the fields with default values blank, saved, re opened and no default values.
If you insert data into the database in the following way:
insert into SomeTable ('Retired') values ('');
then no default value will be used. With high probability I can assume that at the moment you passes empty values from DataGridView, if they are not set. This is wrong!
If you insert data into the database in the following way:
insert into SomeTable ('Last Mileage') values (0);
then the default value for Retired
column will be used. It should be so.
Therefore, you must correctly design the Insert
statement, completely omitting those columns for which should be used the default value in the database itself.
Summing up. You either have to fill the default values in the DataGridView
/DataTable
or dynamically construct the Insert
statement, completely omitting the not specified values.
Upvotes: 2
Reputation: 8043
Default Constraint Works only if you are not specifying the Column in the Insert List.
Suppose I have a Table Like This
CREATE TABLE MyInfo
(
Id INT IDENTITY(1,1),
Nm VARCHAR(50),
MyDate DATE DEFAULT(GETDATE())
)
I'm specifying the Values Like this
INSERT INTO MyInfo
(
Nm,
MyDate
)
SELECT
Nm = 'A',
MyDate = '02/23/93'
UNION
SELECT
Nm = 'B',
MyDate = NULL
And The Result will be Like this
Now I insert a row without specifying MyDate
Column
INSERT INTO MyInfo
(
Nm
)
VALUES('C')
And the MyDate was populated bu Default values
Default Constraint is meant to assign the value if the column is not specified in the insert list. If what you need is to set a Default value if The Inserted value IS NULL or empty, Try using triggers
Upvotes: 1
Reputation: 103
You are assigning default values in forms that are dynamically generated. You may need to evaluate whether the components of your client side/server side scripting were also made to accommodate this dynamic behavior in parallel.
Upvotes: 1