Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio SQL default value not showing on windows form when adding new record

I have created a SQL table in Visual studio and set a default value on a field.

enter image description here

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.

enter image description here

Info from comment questions:

  • It doesn't appear on the form when I click the + for a new row.
  • When a row is added from the database the defaults are there.
  • Added a new row via the form, left the fields with default values blank, saved, re opened and no default values.
like image 815
Matt Avatar asked Jul 13 '26 09:07

Matt


1 Answers

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;

};
like image 80
Hadi Avatar answered Jul 16 '26 03:07

Hadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!