Automatic creation of a new WIn Forms button when entering data into the MS access database - winforms

I use DataGridView.
How can I make an automatic addition of a new button in WIn Forms C# when making a new entry in MS access DB?
Example: I add the product "Sandwich" to the MS access "Products" table and after entering it into the database, a new button corresponding to the name in the database is entered in the corresponding tab "Bakery products" Form1

Related

How to stop ComboBox on subform from refreshing (requering) in MS Access 2010 and later

On the main form there's a linked subform with combobox on it. The underlying query of the combobox is rather complex and takes a few seconds to complete. In Access 2007 it is run only once when the form is opened. In Access 2010 every time I change the current record on the main form the combobox is refreshed and thus it takes awhile to move to the next record. Is there a setting in Access 2010 and later to change this behavior?

How to Insert Into SQL Server Using BindingSource

I would like to insert into a database from SQL Server with a BindingSource in Windows Forms and VB.
Right now I have a DataTable with its DataTableAdapter in 'Details' mode and a bunch of TextBoxs (and other Controls, but that isn't the issue) whose Text attributes are bound to my BindingSource.
If I fill the TableAdapter XDataTableAdapter.Fill(XDataSet.XTable) those TextBoxs receive the data from the first entry, and BindingSource has every row from the db and every column value, so BindingSource and the adapters work.
Instead of modifying the existing entries I would like to insert a new one with its contents as those linked in the various TextBoxs.
I was told I should then use XBindingSource.AddNew() and lastly, once I am certain of the data in the TextBoxs XDataTableAdapter.Update(XDataSet.XTable).
The problem is, before I execute AddNew my BindingSource can read what is being written. But once I reach AddNew, the count of BindingSource increments in 1, but the Items of its current row are empty, regardless of the content of the TextBoxs.
So the issue is, after BindingSource.AddNew, the rows from the db contain its values correctly, but the current new one has them empty, without reading from the bindingsources of the textboxes. Is there any instruction for BindingSource to execute so it knows that the new row should read the textboxs?
So when, I reach the Update instruction, the new row is empty so it doesn't change anything.
My question is, how can I tell BindingSource that the new current row is what is being written in the TextBoxs.
I just wanted to insert into my database straight from the controls bindings, without having to link each one manually.
I'll correct a few misconceptions if I may; hopefully it'll help you understand what's going on:
Right now I have a DataTable with its DataTableAdapter in 'Details' mode and a bunch of TextBoxs (and other Controls, but that isn't the issue) whose Text attributes are bound to my BindingSource.
You have a TableAdapter - a generic name for the type of thing VS generates for you when you use this method of accessing a database. Be sure to call them TableAdapters, as that's what we know them by, and they're distinct from DataAdapters, which are a more basic thing for accessing databases. People might be confused as to which you mean if you call it a DataTableAdapter
TableAdapters don't have a "Details" mode. "Details" refers to something you did in the Data Sources window - you changed from datagrid to details and then dragged a node representing a datatable onto the form, and a bunch of textboxes etc appeared, along with a tableadapter, dataset, bindingsource, bindingnavigator and maybe a tableadaptermanager. At this point you could do nothing else except run the program and it will be able to save new data. "Details" means "create a bunch of textboxes rather than a datagrid" and is a function of the datasources window only
If I fill the TableAdapter XDataTableAdapter.Fill(XDataSet.XTable) those TextBoxs receive the data from the first entry, and BindingSource has every row from the db and every column value, so BindingSource and the adapters work.
Again, a slight terminology tweak: You don't fill a TableAdapter, a TableAdapter is a device that fills a datatable. The datatable that it fills lives inside a dataset. The bindingsource is bound (connected) to the datatable. The datatable holds a cache of local data downloaded from the database. Textboxes are bound to the bindingsource. Because a textbox can only show one record at a time, the bindingsource maintains knowledge of what is the "current" record. The bindingnavigator (a toolbar) moves the bindingsource's current pointer, which causes the textboxes to change values. You use the textboxes to change the values or add new ones. This causes the bindingsource to transmit changes through to the underlying datatable, affecting whatever row it considers to be the "current" row. At some point you save the changed datatable back to the db. The underlying datatable rows change from Unchanged (the state they have when downloaded) to Modified. This is how the tableadapter knows to run the SQL UPDATE query - it looks at the state of the row
Instead of modifying the existing entries I would like to insert a new one with its contents as those linked in the various TextBoxs. I was told I should then use XBindingSource.AddNew() and lastly, once I am certain of the data in the TextBoxs XDataTableAdapter.Update(XDataSet.XTable).
You can indeed do this. The bindingnavigator has a + button on it that does this, or you can write some other code that calls AddNew() on the bindingsource. When you call AddNew the bindingsource makes a new data row but it doesn't add anything to the datatable immediately. When you tell a bindingsource to AddNew, it will switch to pointing its current at the new item, all the textboxes go blank, and you can type the new details into them. These details will go into the new temporary row but remember that that row is not a part of the underlying datatable yet
The problem is, before I execute AddNew my BindingSource can read what is being written. But once I reach AddNew, the count of BindingSource increments in 1, but the Items of its current row are empty, regardless of the content of the TextBoxs.
It's not a problem, it's by design. It's only the same as opening Word and writing a document - it's not saved on your hard disk yet, it's just in memory. You need to do something to commit it to hard disk, just like you need to do something to a bindingsource to make it commit the new item into the underlying data store (the datatable)
To make it commit you need to do something like: navigating to another record, or calling EndEdit() on the bindingsource. At this point it adds the row to the underlying table and this new row has a RowState of Added - this means that when the tableadapter runs its Update (which should really be called Save) it will use the built in SQL INSERT query to save the row; because it's Added/new/needs inserting into the DB
So the issue is, after BindingSource.AddNew, the rows from the db contain its values correctly, but the current new one has them empty, without reading from the bindingsources of the textboxes. Is there any instruction for BindingSource to execute so it knows that the new row should read the textboxs?
I think you're doing things in the wrong order. You're supposed to AddNew, then fill in the text boxes with the new data, then do something like navigate away from the new data, or hit save or some other button that does a Validate()/EndEdit() on the form/the bindingsource. The committing of the new row happens upon EndEdit() of the bindingsource; that puts the row into the table but it still isn't saved to the db at this point. To save, one must call tableadapter.Update(theTable)
It might seem a bit odd, but again, think of it like MS Word. You could open an existing document, then you can do Ctrl+N for a new document, then you write the document. The new document isn't saved to disk yet.
In tableadapter terms the process is the same: You could download an existing database table, then you can do AddNew for a new row, then you write the new row details. The new row isn't saved to database yet.

Running VB when a row is clicked on a datasheet subform in Microsoft Access 2013

So I'm creating a basic database in Microsoft Access 2013. I have a ParentForm, called formLanding, and multiple subforms within that ParentForm. Basically, what I want to happen is when a user clicks on a row within one of the subform datasheets (which display tables like Products and Users), I want to be able to display more information about whatever they clicked on.
Let's say I have subform datasheets called Products, Users, and Locations. If a user were to click on a row within Products, a box/form to the side would show all Users and Locations of the Product. If a user were to click on a row within Locations, the same box would show all Products in that Location.
The reason I want to do this via VB is because I think it'll be easier. I know how to write SQL and VB, but I don't understand Access very well. Rather than trying to link these multiple objects together using their interface I think it'd just be easier to work with VB. I'd also like to be able to run multiple queries sequentially and with parameters, something I haven't figured out how to do in Access yet.
How do I do this? How do I call VB when a user clicks on a row, have my VB queries run and return relevant information based on what they clicked on, and then send that information to another object and have it display it? Is Access built to handle this sort of thing?
Thanks.
EDIT: I wanted to add that the data details I want to display when a row is clicked could include data from multiple tables and return multiple rows. For instance, a Product could have 5 users and 2 locations. I would like to display that information in the same form/report.
You would do this by linking the sub forms to the parent form using the Child / Master properties in the SubForm container.
Assuming you can define a relationship between a Control(Field) in the Parent form and the Data in the sub forms, Access will perform this task automatically.
If you want to Link multiple forms you may need to use the OnCurrent event of the sub forms to change a unbound textbox value on the parent form to force a cascade update of the other subforms.
I can't answer all of your topics, but this string usually works for running more code when certain cells or rows are selected/edited.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("O2:O2")) Is Nothing Then
Exit Sub
Else
'Do the things
Or reference a specific sheet and range of cells with
Worksheets("Sheet1").Range("R17:R55")

creating buttons from db xaml

i have a db which contains 2 tables, one of them descibes credit cards and holds their number, owner and an id; the other one describes the monthly charges of the card, holding their amount dates and the id of the card(wich is the foreign key to the first table).
in my program i am creating a main menu which will allow the user to chose the card, and than it will open a new window that will show the card's charges. in the main menu i would like to create a button for every credit card, and design it as a cover flow.
i am using wpf, and i am new at this. the way i am creating it is through the code behind, i am querying the db for the card's numbers and foreach result i am creating a button.
i want to know if this is the right way to do it, or is there any other way to create the buttons through the xaml?
What you want is called Data Templates, you create a template that represents a single item, and then apply the template to the menu control and set it's ItemsSource property to the rows from your database.
Here's a quick post I found that might help: http://www.vbknowledgebase.com/?Id=133&Desc=WPF-Menu-Databinding
I'm sure you can find more with a quick web search.

ViewModel -> Model interaction

Suppose I have a WPF/MVVM application for managing some hypothetical customers :).
Domain model contains an entity named Customer (represented as a POCO in code).
The main screen contains a grid, bound to a view model (CustomersViewModel) that loads its data from Repository< Customer>.
The main screen also allows to create new customers (and save it to the DB).
Suppose I need to implement 'add customer' use-case. The most obvious approach is as follows:
Present the user with a dialog window to be filled out with new customer data.
Handle 'Save' button click in the ViewModel.
Create customer (var new_customer = new Customer(..)) domain object using the data from the dialog (step 1).
Call Repository< Customer>.Save(new_customer) to save the new customer to the DB.
Reload CustomersViewModel with fresh data from the DB so that newly added customer is visible in the grid.
Personally I don't like this 'quick-and-dirty' way (because of need to reload the full list of customers from DB every time a new customer is added).
Can anyone suggest a better approach (that wouldn't require refreshing the customer list from the DB)??? I feel there gotta be some best practice for handling scenarios like that:) ).
Thanks in advance!
If the saving of the Customer is successful, why can't you just add that single Customer instance to your collection of customers? No need to re-load all customers unless the user explicitly refreshes the view (usually via a refresh button).
If you are loading the list in your view through a binding (to a list of customers) you can just add the new customer to that list and everything is alredy ;-)
I have a similar application where in the object is created in UI. I solve it by adding the object in VM and then syncing it with Model on click of Save button.
I am assuming you have a list of CustomerViewModel in CustomersViewModel to which the grid view is bound to. You can add a new CustomerViewModel object to the list in CustomersViewModel. While saving the ViewModel data back into the model, the model gets in sync with VM. No need to refresh VM back from Model unless somebody else apart from your app is changing the Model data.
You could create an ObservableCollection<Customer> and fill it with the customers from the database which you want to show in the View. When you add a new customer then add it to this collection as well as save it into the database. The CustomersView binds on the ObservableCollection and is updated automatically without the need to refresh the data from the database.
The BookLibrary sample application of the WPF Application Framework (WAF) shows how this can be done.

Resources