Dataset Default Values (1 Viewer)

WindSailor

Registered User.
Local time
Today, 04:41
Joined
Oct 29, 2003
Messages
239
Ok... briefly.
I can get my default values pulled in and show in the dataset.
Just by inserting a DataGridView on the form and adding a datasource through the wizard. This will pull all of my default values WHEN I create new rows 'in the raw' using my database tools on my table. So I am not creating a new row on the form.

But... the bit fields that have the default value of 'False', do show False (from what I can tell, the check/boxes are blank) also give an error with something to do with background color in the bit fields. If I elilminate the bit fields I dont get the error and the rest of my default values come through OK. I will have to do some research on this.

I got this (VS 2005 Pro) last week from Amazon, and thought I could just jump in and go... well... silly me :eek:
 

WindSailor

Registered User.
Local time
Today, 04:41
Joined
Oct 29, 2003
Messages
239
Did you get it to go?

Make sure your table has a primary key, or it will not work.
In fact make sure all of your tables have primary keys.

You should be able to make it work by doing the following...

Create new Data Source (go through the wizards - to bind a datasource to your DataGridView etc.)
Right click on your dataset (at the bottom of the page) and choose 'Edit in Dataset Designer'
Click on your TableAdapter (in the middle of the screen - to give it focus), then right click on it again and choose 'Configure'
And follow the prompts.

For the code on your image to save it, try something like this...

Code:
Private Sub imgSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgSave.Click

   Try
     With Me
        .Validate()
        .EquipPoolBindingSource.EndEdit()
        .EquipPoolTableAdapter.Update(Me.DsEquipPool.EquipPool)
        .DsEquipPool.Clear
        .EquipPoolTableAdapter.Fill(Me.DsEquipPool.EquipPool)
     End With
   Catch ex As Exception
            Dim x101 As String
            x101 = ex.ToString
            MsgBox(x101, MsgBoxStyle.Information)
  End Try

End Sub

See what you end up with...

Edit---------

This is a lot different than VS2003...
By default there is 780 lines of code to generate a dataset with update, delete, and insert statements in VS2005 (for a two column table - go figure)...
 
Last edited:

Matty

...the Myth Buster
Local time
Today, 06:41
Joined
Jun 29, 2001
Messages
396
Rick,

I just tried this, and my default value still doesn't fill in. Maybe I'm missing something:

- All my tables have primary keys
- After re-creating my dataset I configured the table adapter and everything was checked (generate update/delete statements, etc). Was there anything special I needed to do in that configure step?
- I added the Clear and Fill statements to my save event. But when I make a new record on the form (before I click imgSave), my checkbox is still Null.

Just for future reference, why would I need to clear and then re-fill my dataset after each save?

I purchased an ADO.net 2.0 book over the weekend (Microsoft ADO.NET 2.0 Step By Step, Microsoft Press). I've learned a lot from it so far, but there's really nothing in there about doing anything special to get default values into VS (unless you want to create Data Columns in code at run-time). I really appreciate all your help, though. It's just weird that my keys/relationships pull through to VS, but the default values do not. :eek:
 

WindSailor

Registered User.
Local time
Today, 04:41
Joined
Oct 29, 2003
Messages
239
I really dont think the dataset (your form) has ever been designed to pull or store database/table/column default value properties for creating new rows (by design)… so you will have to do it in the run-time.

I was thinking you weren't pulling your default values from the database after inserting a new row... :eek: I am still doing some research on this (SQL Server Express).

I looked at the properties of the new DataGridView and couldn’t find an option to set default values for any column for a new row… to bad I think it would be a neat feature.

Check this site out; it has a lot of great information, including setting default values in a DataGrid:
http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx

Also I clear and re-fill my dataset after an update just so I know I am viewing current data from the database and not data from my cached memory. And I make every effort to use the ‘Try Catch EndTry’ phrase to trap my errors.
 

WindSailor

Registered User.
Local time
Today, 04:41
Joined
Oct 29, 2003
Messages
239
Ok... after throwing this around a little, I *believe* I understand why Microsoft doesn't pull table level default values into the dataset.

It is so a 'Null' value can still be entered into the database instead of the assigned default value for that column at the table level. At least they left that option open so you could still do that and in case you wanted to test for nulls down the road...
 

Matty

...the Myth Buster
Local time
Today, 06:41
Joined
Jun 29, 2001
Messages
396
Okay, I guess that makes sense. I just wanted to be sure I wasn't missing something (it being my first time using datasets and all). Thanks for all your help/research! :D
 

WindSailor

Registered User.
Local time
Today, 04:41
Joined
Oct 29, 2003
Messages
239
No problem...always learning. And sometimes I get to revisit them more than once. <g>

I didn't know I could assign the default values in the datasets designer in VS2005... Thanks!

With VistaDB and TurboDB I could automatically insert my default table values (inserting a new row in the table, not dataset) when the inserted value was *nothing* or null, it simply tested for null values and inserted the default value. I took that for granted.

SQL Server Express doesn't do it that way, and it threw me.

Evidently they see it as a design option (inserting nulls or dbnull) to over-ride the table level default value.

Other ways I could get the 'table level default values' to be inserted correctly into the table...

If I didn't include the column in the dataset (SQL statement - or no insert statement with a DBNull value for the column)
Use a trigger to assign an ID value for related fields in other tables (inserting a new row)

Or simply... the default value assigned at the table level only works if there is absolutely no value inserted into that column when the row is created, otherwise it is simply over-written. <g>

Thanks Matty, you made me look at something I took for granted, and thanks for helping. :)
 
Last edited:

devobject

New member
Local time
Today, 04:41
Joined
Mar 6, 2007
Messages
1
RunTime DefaulValue Setting.

dsMain.Tables["tbl_Customer_Master"].Columns["iCompanyYearId"].DefaultValue=2007;
 

Users who are viewing this thread

Top Bottom