I have a function that saves an XML file and then binds it to a gridview. My problem is that the gridview is updating before the file is done saving.
So far I've been able to get the save to occur first by inserting a 1 second pause, however, I realize that this is a terrible, not to mention unreliable, way of getting the desired result. My code currently looks like this
editingFunction();
gsXML.Save(Server.MapPath("~/xmlFile.xml"));
System.Threading.Thread.Sleep(1000); // Ill-advised, I know...
XmlDataSource1.Data = gsXML.OuterXml;
XmlDataSource1.DataBind();
updatePanel1.Update();
Does anyone know a better way to ensure that the save function occurs before the binding?
EDIT: It seems that I misdiagnosed the problem. The save function was in fact executing first, however, I needed to clear the XmlDataSource.Data first by calling 'XmlDataSource1.Data = null.' Thanks to Graffito for pointing this out!
As the DataGridView is already bound to its source, the instruction " XmlDataSource1.Data = gsXML.OuterXml" does not operate.
To force a new binding, remove the binding first:
XmlDataSource1.Data = null.
XmlDataSource1.Data = gsXML.OuterXml
Related
I'm experimenting with C#/.net/WPF all for the first time. I've created a project and set up a datasource (just a table with some sample data) and created two tableadapters named Prods and Prods1 - the latter has a filter applied in the query to return slightly different results. I've dropped both tables on my form and both dutifully display their respective data.
I thought I would then swap the data source for each. So the default generated Window_Loaded:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
// Load data into the table Prods1. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProds1TableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProds1TableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prods1ViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prods1ViewSource")));
prods1ViewSource.View.MoveCurrentToFirst();
I now want to make the first data grid (prodsViewSource) instead display the data for the second table, and ignore the second table entirely. So, I changed that as follows:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
With the second block having been commented out.
I must be missing something fundamental - what I think I'm doing is redefining the prodtestDataSetProdsTableAddapter variable to use an instance of the prods1 table adapter, and then using that to populate the prodsViewSource grid on the form, but I end up with a blank. Where's my error?
...
Well, I posted this after beating my head against it for an hour and, minutes later, realized the FAR easier thing to do is to just change the datacontext property of the grid in question.
I would still like to understand why doing it the vastly more complicated-bordering-on-Rube-Goldbergian way didn't work, though, so if anyone can explain that, it would still be welcome.
I'm looking at updating stored values in a RethinkDB using the C# RethinkDB.Driver library and I'm just not getting it right.
I can achieve an update by getting the result, altering that object then making a separate call to update with that object. When there are many calls to a record to update like this, the value being updated elsewhere whilst the application is working with the record.
TestingObject record = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Run(_conn);
record.fieldNameIntValue = record.fieldNameIntValue + 1;
var result = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Update(record).Run(_conn);
I've been trying something along these lines :
var result = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Update(row => row["fieldNameIntValue"].Add(1)).Run(_conn);
but the result errors with Inserted value must be an OBJECT (got NUMBER):101 which suggests this is only passing the field value back instead of updating the object.
Ideally I'd like to update multiple columns at once, any advice is appreciated :)
This is an example that works in the ReQL data explorer. You can chain as may filters before the update as you want. I assume this will translate to the C# Driver, but I dont have any experience with that.
r.db('database').table('tablename').update({clicks: r.row("clicks").add(1)}).run().then(function(result){ ...
Thanks T Resudek your answer and a clearer head helped emphasised the need to map the calculation to the property.
Looking at the javadocs for update it has HashMap method which I followed with the c# library and it works.
var result = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Update(row => r.HashMap("fieldNameIntValue",row["fieldNameIntValue"].Add(1))).Run(_conn);
I'd be interested to know if this is the right way or was a better way.
I have a an issue in my windows application project, a very small one I'm afraid which for some reason I can't get a hang of the solution.
I want to display data in a Data grid programmatically, but it returns only the rows based on the number of records fetched from the database WITHOUT Data in it. Below is my code
private void BindDataToGrid()
{
var teller = new TellerService().GetByUsername(username);
var transactions = new PocTransactionService().GetByTellerId(teller.TellerId);
BindingList<PocTransaction> trans = new BindingList<PocTransaction>(transactions);
var source = new BindingSource(trans, null);
dgvTransactions.DataSource = source;
}
Not that the variable transactions gets populated in the code, but for some reason, it's not displaying on grid. It's really driving me nuts. I trust one of you here to show me the way out.
I finally figured it out. There is nothing wrong with the code I wrote in the first place. I just didn't set the DataPropertyName for each of the columns I defined on the grid. Setting them appropriately to the datasource property gave my the desired result.
All thanks to Gabriel for his help so far. I appreciate.
I'm having major problems trying to clear a DataTable in C#.
At all other points when editing information in the database, I've used something like
somethingTableTableAdapter1.Update(greenParksDataSet);
greenParksDataSet1.AcceptChanges();
and this has worked just fine. However, in one of my forms I have a button to clear the open table. It seems to clear the DataTable in memory, but not actually propagate the changes to the database. Essentially the code boils down to
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = -1;
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = -1;
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = 1;
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = 1;
greenParksDataSet.HistoryTable.Rows.Clear();
historyTableTableAdapter.Update(greenParksDataSet);
greenParksDataSet.AcceptChanges();
I've tried messing around with the order of things but nothing seems to work. With that code on its own, the DataTable is cleared (and the changes are shown in the DataGridView) but when adding a historyTableTableAdapter.Fill(greenParksDataSet.HistoryTable); after that, it is shown that the database is not being changed whatsoever.
I've probably missed something obvious, but I've been puzzling over this for ages, and any help would be appreciated. Thanks!
Have a look at this question/answer.
Clear just works on the datatable in memory - it does not flag them for deletion.
I think you are using a binding source so try this :
greenParksDataSet.HistoryTable.Rows.Clear();
HistoryTableBindingSource.EndEdit();
historyTableTableAdapter.Update(greenParksDataSet);
greenParksDataSet.AcceptChanges();
I posted this question yesterday evening, which has led me to discover a huge problem!
I have a decimal column in my database called Units, anytime I set the value of the column to a NON ZERO, and SubmitChanges the column updates with the new value. If I try to set the value of the column to ZERO, the SubmitChanges does not update the column.
data.Units = this.ReadProperty<decimal>(UnitsProperty);
data.UnitPrice = this.ReadProperty<decimal>(UnitPriceProperty);
data.Price = this.ReadProperty<decimal>(PriceProperty);
I've taken a look at the DataContext log and I can see that the field with the ZERO value is not included in the query. Even if I try to hard code the change Linq ignores it.
data.Units = 0;
data.UnitPrice = 0;
data.Price = 0;
Needless to say this is killing me! Any ideas why this happening?
Solution
I figured out my problem with the help of the SO community. My problem was being caused by the fact when I created my entity to attach, the default value of the column was set to zero, so when it tried to assign the value to zero ... LinqToSql says hey ... nothing changed, so I am not updating the value.
What I am doing now ... just to make it work is the following:
ctx.DataContext.InvoiceItems.Attach(data, true);
That seems to force all the values to write themselves to the database. This works for now.
I have tried to reproduce this with a the following code, but for me it works.
using (DataClasses1DataContext ctx = new DataClasses1DataContext())
{
var obj = ctx.DecimalColumnTables.First();
Debug.Assert(obj.B != 0);
obj.B = 0;
ctx.SubmitChanges();
}
So I think there must be something special in your domain that causes this. I suggest you to create a such simple repro with your domain model and see what happens.
LINQ to SQL ignores updates to the current value, so if the field was already zero, you may not see any updates.
Off: The OR/M you use is LINQ to SQL. LINQ is the name of the querying capability in .NET, but LINQ does not define nor implement any update logic. So the issue relates to LINQ to SQL, not LINQ.
Obvious question, but are you sure the column is mapped in the dbml / mapping file?
Also - is it a calculated column? (i.e. price => units * unitprice)
I figured out my problem with the help of the SO community. My problem was being caused by the fact when I created my entity to attach, the default value of the column was set to zero, so when it tried to assign the value to zero ... LinqToSql says hey ... nothing changed, so I am not updating the value.
What I am doing now ... just to make it work is the following:
ctx.DataContext.InvoiceItems.Attach(data, true);
That seems to force all the values to write themselves to the database. This works for now.
Some more information ... I figured out my problem ... it's more of a lack of understanding about LinqToSql ... where I am doing:
private void Child_Update(Invoice parent)
{
using (var ctx = Csla.Data.ContextManager
.GetManager(Database.ApplicationConnection, false))
{
var data = new Gimli.Data.InvoiceItem()
{
InvoiceItemId = ReadProperty(InvoiceItemIdProperty)
};
ctx.DataContext.InvoiceItems.Attach(data);
if (this.IsSelfDirty)
{
// Update properties
}
}
}
I thought this would load the original values ... what happens is that it creates a new object with default values ... empty values, like 0 for decimals, Guid.Empty for uniqueidentifiers and so on.
So when it updates the properties it sees the Units already as 0 and it sets it to zero. Well LinqToSql doesn't recognize this as a change so it doesn't up date the field. So what I have had to do is the following:
ctx.DataContext.InvoiceItems.Attach(data, true);
Now all the modifications are generated in the update statement whether there is really a change or not. This works ... seems a bit hackish!
The correct answer is as many pointed out to use the special overload of Attach which accepts a boolean parameter to consider it as modified, (make the mistake of using another overload and it simply won't work):
ctx.DataContext.InvoiceItems.Attach(data, true);
Note however that you still might need to have a "Version" column in the table of type "timestamp".
I had this problem and all the suggestions I'd seen didn't apply or work.
But I found I had made a very simple mistake!
When updating the property I was actually calling a custom Set method (because there were other things that needed to be changed in response to the main property in question).
After hours of head scratching I noticed that my Set method was updating the private member not the public property, i.e. this._Walking = value;
All I had to do was change this to this.Walking = value; and it all started to work!