How save changes to a database after IBindingSource.AddNew() method is used - c#

I have a form that I use for both editing and inserting new users into a database. I use Entity Framework 6. All my controls (textboxes and one combobox) are bound to specific binding sources so when I update an entity with:
context.user.add(user);
context.Save();
my UI changes. And vice-versa. Now, updating works fine, but I have some problems with inserting of new users. Currently a data grid view has it datasource set, and I have disabled adding rows by clicking on a grid view directly (because I want that part to go through the form):
Okay, so far so good. Now when I want to add a new user, I do this:
myDataSource = context.user.Local.ToBindingList();
myDataSource.AllowNew = true;
and later on:
myDataSource.AddNew();
After calling AddNew() the blank row is added to a grid view, and when I insert values in my form, everything shows up automatically in the grid view.
The thing is, I can't really persist this data into a database. If I do:
context.Save();
nothing happens. I guess because the newly created entity by the AddNew() method is not attached to the current context. But I don't have a reference to it to attach it.
I guess that AddNew() creates a new entity, right? And adding it to the datasource. Correct me if I am wrong.
So how to save changes in this case?

I have a similar issue with getting an added record persisted to the database. I had same basic setup, using .Load, and setting the binding source data source = .Local.ToBindingList()
And found an odd fix to the issue. So under my binding navigator save event, this is the only way I could get newly added records to make it to the database. The last ResetBindings call is just so that the Identity Id shows it's actual DB value instead of zero.
private void mapBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
mapBindingSource.EndEdit();
mapBindingSource.ResetBindings(false);
mapBindingSource.EndEdit();
_db.SaveChanges();
mapBindingSource.ResetBindings(false);
}

Related

Updating the DataSource of a DropDownList in ASP.NET's C#

I have an edit form that uses a DropDownList, connected to a SQL server table using a DataSource. This edit form allows users to set the table's "IsDeleted" column value to 1, which would then hide it from all of the application's queries. (So the transaction still exists in the database, but not in the application)
The problem I've encountered with this is that if the page on which the edit form is is not left entirely, and then entered again, the entry still remains in the DataSource.
So essentially, the DataSource is not updating. (It's not running its select statement and repopulating until the whole page is reloaded). I've tried to use a page refresh and it doesn't seem to work, only going to a different page entirely and then coming back seems to update the DataSource.
How would I refresh the contents of the DataSource programmatically without having to recreate the entire DataSource itself?
You might want to add some code next time.
My best guess for now is that you're not doing anything OnPostback.
So I eventually resorted to doing what I did not want to do in the hopes that there would be a "Best-practice" way to do it (and one hopefully already built-in into DataSources), but it seems that was not to be.
All it took was to re-assign the DataSource's parameters to it again and then assign the DataSource back to the DropDownList, as if I were creating a new one.
I created the following method, which I then called at the end of my Delete button's event.
protected void DropDownList_Reload()
{
MyDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
MyDataSource.SelectCommand = "SELECT * FROM [tblMyTable] WHERE [IsDeleted] <> 1";
cbxDropDownList.DataSourceID = "MyDataSource";
cbxDropDownList.DataTextField = "MyHeader";
cbxDropDownList.DataValueField = "MyHeader";
}

Refreshing a datagridview that was added through designer

I've added a MySQL database through the datasources in Visual Studio. I've then dragged and dropped the table I want onto my form design and changed the layout to suit here and there.
I have a background worker that inserts data into the database occasionally using the MySQL connector. I've then been trying to refresh the datagridview to reflect the changes inserted into the database, but I can't get the form to update to show the new data.
Having read a few questions from people having similar trouble I've tried:
Rebinding the data source as per this question Refreshing a DataGridView after DB has changed?
and this one http://social.msdn.microsoft.com/Forums/vstudio/en-US/a1cf64de-6e35-435c-943c-ed6e9b39525b/how-to-refresh-datagridview-component-using-designer?forum=csharpgeneral
this.rfidDataGridView.DataSource = null;
this.rfidDataGridView.Rows.Clear();
this.rfidDataGridView.DataSource = this.rfidBindingSource;
Which causes the datagridview to become a white square with an X in it
Calling the reset methods as per here http://msdn.microsoft.com/en-us/library/f61k6akt.aspx
this.mcbrideDataSet.Reset();
this.rfidBindingSource.ResetBindings(false);
Which gives me a SystemOutOfRangeException at Windows.Forms.CurrencyManager.getItem
Refilling and refreshing
this.rfidTableAdapter.Fill(this.mcbrideDataSet.rfid);
this.rfidDataGridView.PerformLayout();
this.rfidDataGridView.EndEdit();
this.rfidDataGridView.Refresh();
this.rfidDataGridView.Parent.Refresh();
I've tried a few other variations of all of the above.
As a lot of this data has been auto-generated by Visual Studio in Designer.cs I've tried following the data definitions and references to see if I could find anything useful in there, but I'm new enough to C# that I'm not getting anywhere. I can't see where the connection string is or where the dataSource is getting it's data. I've found it's declaration, but not where it's assigned values from the database.
Any help would be greatly appreciated.
I think you need to bind the grid view after it changes.
rfidDataGridView.DataBind();
DataGridView.BindingContext = new BindingContext();

Bound DataGridView with multiple sources

i have been strugeling with this for some time now and its driving me crazy. This is the situation:
I have a bounded DataGridView i designed this with the visual studio designer. All the information shows accordingly.
in my DataGridView i have 2 ComboBoxes in here the data is also correctly displayed. I want that if i click on the ComboBoxea list of options shows.
Because the DataGridView is bounded to a source i can not use the ComboBox.Items.Add() method. So i created another Datasource in the designer and in runtime i change the datasource for that specific combobox. Now the list shows shows the options that i want, yeey !
Now, i want to save this newly added or changed row to the database.. so i use the following method for this (i call the method on the RowLeave event from the DataGridView):
if (tasksDataSet.HasChanges()
{
try
{
tasksBindingSource.EndEdit();
tasksDataSet.GetChanges();
tasksTableAdapter.Update(tasksDataSet);
}
catch (Exception ex)
{
}
}
This won't work for the ComboBoxes since this is another datasource.
So basicly what i want is:
Use a datasource for the DataGridView
Change/Add items to the DataGridViewComboBox
Save the changed made to the (complete) DataGridView to the database
How can i make this work ?
if your problem is to save current data which is given to Grid view.
than i suggest try to use session . while binding data to DataSource assign it to
session["SomeID"] . if your changing binding then again assign same session to it.
next step convert session to what ever you want to save.
ex:
//Datasource.
list<User> DataBoundSource = new list<User>();
DataGrid.DataSource = DataBoundSource;
DataGrid.DatBind();
//Assign to Session where you are binding this
//every time
Session["SameID"] = lsDataBoundSOurce;
//your code.
...
...
...
//covert that session to Datasource you want to save.
list<User> saveData = (list<User>) Session["SameID"];
this is the basic idea i got. i hop this will help you.
please give it +1 if it help you

Parent DataGridView does not update when using TableAdapterManager's Hierarchical Update

Here's are the facts:
I have a parent table, let's call it Order. The data in this table is viewed using a DataGridView (dgvOrder).
I have a child table, let's call it OrderDetails. The data in this table is viewed using a DataGridView (dgvOrderDetails)
I have textboxes (and checkboxes, comboboxes, etc.) that are databound to the Order table (using BindingSource).
I am using Visual Studio 2008.
What I want to do:
dgvOrder is read-only. I use this to browse through records ala BindingNavigator. (I like to use a DataGridView for navigating records because it is intuitive. You immediately see what the next few records are all about.)
To add new records, I use a "new" button (uxNewButton). To save my updates, I use a "save" button (uxSaveButton). Records are added using the textboxes and not directly via dgvOrder. Child records are added directly via dgvOrderDetails.
dgvOrderDetails, I guess, is pretty obvious - it contains the details of the Order (1 to many relationship).
My code and configuration:
Hierchical Update (in the dataset designer) is set to true (by default in VS 2008)
Relationship between Order and OrderDetail is set as "both relation and fk constraint". Update and delete are set to cascade. Accept/Reject rule is set to "none". Nested Relation is unchecked.
TableAdapterManager is dragged to the designer(tam).
private void uxNewButton_Click(object sender, EventArgs e) {
bsOrder.AddNew();
}
private void uxSaveButton_Click(object sender, EventArgs e) {
this.Validate();
bsOrder.EndEdit();
bsOrderDetails.EndEdit();
tam.UpdateAll(dsOrder); //DataSet is named dsOrder. try-catch block excluded for simplicity
}
Problem:
After filling up all the textboxes (and etc.) and the dgvOrderDetails, hitting the save button will cause an error: ForeignKeyConsraint FK_Orderchild_Order requires the child key values (-1) to exist in the parent table.
However, if I add a record directly using dgvOrder (then dgvOrderDetails), hitting the save button will save successfully.
I also found out that I can save successfully if I add new records using the textboxes but have to select a different row in dgvOrder, then selecting the current row again, before adding the records on dgvOrderDetails.
It seems like while adding a new record via the textbox, the underlying data is not synced with the DataGridView. Selecting a different row, then selecting back the current row syncs the data altogether.
I have tried various hacks in the save event like:
dgvOrder.Refresh(); //or
dgvOrder.Invalidate();
dgvOrder.Refresh(); // or
dgvOrder.Parent = null;
Controls.Add(dgvOrder);
//and so on and so forth (suggetstions from Google searches)
Adding bsOrder.EndEdit() on the Enter Event of dgvOrderDetail seems to solve the problem but I'm not sure if this is best practice.

DataGridView bound to Linq to SQL does not show new rows added elsewhere to DB context

I have a DataGridView bound to a LINQ to SQL query expression. I want it to be editable for updates, but for inserts I want users to use separate controls below the grid (textboxes, date pickers, etc - currently not bound to anything). I have an "Add" button that creates an object of the linq to sql entity class based on the values in the controls, and then calls InsertOnSubmit(). When I later call SubmitChanges(), any updates from the grid, and any objects added are all correctly persisted to the database.
My problem is that any new objects are not added to the grid, before or after the call to SubmitChanges(). I would like new objects to be added to the grid as soon as they are created. Also, I only want the controls below the grid to be used for inserting new records, not editing existing records, so I don't think they should be bound to the data source...What is the best way to make this happen?
I did try simply re-setting the DataSource for the grid (ie dataGridView.DataSource = db.<TableName>, which works, but is clumsy because it scrolls to the top of the grid again - I'm sure a better method exists.
(Pls excuse the n00b question, I'm very new to all this .net stuff :P)
The first thing to try is GetNewBindingList(), but this thread: "Linq-SQL canonical editable datagridview sample code" has some thoughts for other scenarios.

Categories

Resources