Set default values with BindingSource On New with Navigator with LinqToSQL

I created a Linq to SQL class from an mdf.

I created a DataSource from that object.

I created a WinForm and dragged fields from my DataSource, including the built-in Navigator.

My form is for a table called "Tickets"

Each ticket has a Foreign Key to the Batch table. The idea is that a batch of tickets are created in one session, so each ticket has a BatchID.

So, here's my problem...

When I create a new record (by clicking the + on the NavBar), I need to assign the BatchID to the Ticket, and display it on the form.

I cannot figure out a way to do that.

I bound the controls on my WinForm by dragging fields from the DataSource, which is based on the LinqToSql object.

I've placed code assign the value to the textbox in the AddingNew event of the BindingSource, but apparently, that event runs before the new record is created (or the form is updates, or something) and thus the value gets reset to null.

I've tried setting a flag in the AddingNew event that indicates we're creating a new record and then set the BatchID in the CurrentChanged event when that flag is true.

That works -- until I change the first value in the form. Then the BatchID textbox clears.

I've read that there's a way to set the field's Default Value (each time a new batch is created) but I can't figure out how to do it.

Do you have any suggestions on any way to set my BatchID when a new ticket record is created?


From my searches, here on EE, it seems the primary recommendation is to set the value in the dataset, but I can't figure out what that is because my BindingSource's DataSource is the LinqToSql class. I don't see where a dataset is created.
KapriceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KapriceAuthor Commented:
My feeling is that at at some point after pressing the + on the Navigator, my LinqToSql class is being referenced and eventually points to a new record.

It seems I should be able to tap into that and set a default value.

BTW, the default does not have to be set at the AddNew record level. When I create a new Batch, that would be an ideal time to set the default value of BatchID in the Tickets class. That way, when the AddNew is called, it will automatically use that default.

But, I'm at a loss as to how to do that.
0
CodeCruiserCommented:
Do you have to show BatchID textbox?

How is data in database being updated?
0
KapriceAuthor Commented:
It's not critical that the batch show on the form, but it's useful. Regardless, I need it to get stored with the record, so the bound form seems the best choice.

The form is bound via BindingSource, which has a Linq to Sql as it's DataSource.

I use BindingSource.SubmitChanges to update the database.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

CodeCruiserCommented:
And you were creating a custom e.NewObject in AddingNew event handler?

https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.addingnew(v=vs.110).aspx
0
KapriceAuthor Commented:
No, I didn't create a cutom e.NewObject. At least not manually.

I let VS create the dbml class by dragging tables to the designer.

I created a DataSource based on the dbml object.

I dragged an entire table from my DataSource panel onto my WinForm, creating a Navigation bar and a BindingSource.

When a new Ticket record is created, I want to assign a BatchID to the record (and form). The BatchID is the same for every Ticket entered during a single session.

I discovered that (as your linked article points out) that AddingNew doesn't work because the object isn't created.

So, instead I set a variable in Adding New and then added code to CurrentChanged. As stated in my OP, that does display the BatchID on the form, but it goes away after I add any data to the form.
0
CodeCruiserCommented:
AddingNew can be used to initialise the new object and run any code on it that you want. In that event handler, you can create a new object of your class (of the type that the list contains) and assign it to e.NewObject. You can then assign its BatchID a value that you want.
0
KapriceAuthor Commented:
CodeCruiser, once I've created that new object, how do I link or associate or apply it to the form, which is already bound to another object (BindingSource)?
0
CodeCruiserCommented:
When you set it to e.NewObject, BindingSource will handle it.
0
KapriceAuthor Commented:
Sorry, what is "it" in this case?
0
CodeCruiserCommented:
linking/associating.

Have you tried it?
0
KapriceAuthor Commented:
I honestly don't even know what that means. Got an example?
0
CodeCruiserCommented:
Assuming that your class name is Ticket.


private sub bindingsource1_AddingNew(...)
e.NewObject = New Ticket()
e.NewObject.BatchID = 11
end sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KapriceAuthor Commented:
So, in this example, Ticket is the class represented by the dbml?

I'm still confused because it seems to me the BindingSource is already creating a new Ticket in the Ticket class. Isn't it?

If so, now I'm creating a second New Ticket. Doesn't that conflict? Or, does e.NewObject OVERRIDE the new ticket the BindingSource would otherwise create?
0
CodeCruiserCommented:
Your last sentence is true.
0
KapriceAuthor Commented:
Got it. Thanks much. I'll give it a try in the next couple of hours and report back.

If it works, it's a better solution than I came up with on my own. What I ended up doing is adding a SubmitChanges call in CurrentChanged when when Adding a new record.
0
KapriceAuthor Commented:
Works great. Thanks so much for your patience in helping me understand what you were actually suggesting.

CORRECTION to my previous comment. I used EndEdit, not SubmitChanges. That sets the value in the class so it's later available to SubmitChanges.

Apparently, just programatically setting the value of a textbox doesn't update the class.
0
CodeCruiserCommented:
Glad its working
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.