In project i user SQL CE, i have Table:
[Table]
public class Article : INotifyPropertyChanged, INotifyPropertyChanging
{
// Define _cid: private field, public property, and database column.
private int _aid;
[Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
public int aid
{
get { return _aid; }
set
{
NotifyPropertyChanging("aid");
_aid = value;
NotifyPropertyChanged("aid");
}
}
// Define nameColor name: private field, public property, and database column.
private int _rid;
[Column]
public int rid
{
get { return _rid; }
set
{
NotifyPropertyChanging("rid");
_rid = value;
NotifyPropertyChanged("rid");
}
}
private string _title;
[Column]
public string title
{
get { return _title; }
set
{
NotifyPropertyChanging("title");
_title = value;
NotifyPropertyChanged("title");
}
}
private string _thumnail;
[Column]
public string thumnail
{
get { return _thumnail; }
set
{
NotifyPropertyChanging("thumnail");
_thumnail = value;
NotifyPropertyChanged("thumnail");
}
}
private string _DesScription;
[Column(DbType = "NTEXT")]
public string DesScription
{
get { return _DesScription; }
set
{
NotifyPropertyChanging("DesScription");
_DesScription = value;
NotifyPropertyChanged("DesScription");
}
}
private int _orderID;
[Column]
public int orderID
{
get { return _orderID; }
set
{
NotifyPropertyChanging("orderID");
_orderID = value;
NotifyPropertyChanged("orderID");
}
}
private string _pubDate;
[Column]
public string pubDate
{
get { return _pubDate; }
set
{
NotifyPropertyChanging("pubDate");
_pubDate = value;
NotifyPropertyChanged("pubDate");
}
}
private string _linkURL;
[Column]
public string linkURL
{
get { return _linkURL; }
set
{
NotifyPropertyChanging("linkURL");
_linkURL = value;
NotifyPropertyChanged("linkURL");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify that a property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region INotifyPropertyChanging Members
public event PropertyChangingEventHandler PropertyChanging;
// Used to notify that a property is about to change
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
#endregion
}
when i Update Colum Thumnail , i have erros :
SQL Server does not handle comparison of NText, Text, Xml, or Image data types
because of special characters into database insert sequence trogn should I use BbType = "NTEXT"
Please Help me !
You could remove this column for concurrency checking by adding [Column(UpdateCheck = UpdateCheck.Never)] to this column.
See this blogpost about Linq to sql concurrency checking: http://blogs.msdn.com/b/matt/archive/2008/05/22/into-to-linq-to-sql-optimistic-concurrency.aspx
Related
i'm having difficulties deserialising a json file. The json object has the following structure which has been simplified
{"date":"2015-11-11",
"retailer_id":"CLD001",
"orders":[{
"products":
[{
"product_id":"53743443003",
"quantity":4,"
unit_price":42.71}],
"value":170.84,
"customer":{"id":58}}]}
This structure indicated to me that the top class is
[Table]
public class RetailOrders : INotifyPropertyChanged, INotifyPropertyChanging
{
private List<OrderItems> oi;
private string retailer_id;
private DateTime date;
public List<OrderItems> OrderItems
{
get { return oi; }
set { oi = value; }
}
public string Retailer_id
{
get { return retailer_id; }
set { retailer_id = value; }
}
public DateTime Date
{
get { return date; }
set { date = value; }
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
}
As you can see orders take a list of products being ordered with the variable id, quantity and total price
[Table]
public class ProductsOrdered: INotifyPropertyChanged, INotifyPropertyChanging
{
private string productID;
private int quantity;
private double unit_price;
public string ProductID
{
get { return productID; }
set { productID = value; }
}
public int Quantity
{
get { return quantity; }
set { quantity = value; }
}
public double UnitPrice
{
get { return unit_price; }
set { unit_price = value; }
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
finally the orderItems contain a list of the orders followed by a total price and associated customer
[Table]
public class OrderItems : INotifyPropertyChanged, INotifyPropertyChanging
{
private List<ProductsOrdered> po = new List<ProductsOrdered>();
private double TotalPrice;
private int customer_id;
public List<ProductsOrdered> Productsordered
{
get { return po; }
set { po = value; }
}
public double totalprice
{
get { return TotalPrice; }
set { TotalPrice = value; }
}
public int customerid
{
get { return customer_id; }
set { customer_id = value; }
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
}
I'm expecting the list to fill out however currently they are null values and can't seem to think of why this is happening
Your property names don't match the JSON you've supplied. Specifically in your JSON there is an array orders but in your C# class the property is OrderItems.
You could annotate the property like this:
[JsonProperty(PropertyName = "orders")]
public List<OrderItems> OrderItems { get; set; }
I haven't worked all the way through your hierarchy, but any other null fields will likely be caused by a similar mismatch.
One other thing to be careful of is private properties and private setters, which I don't thing will be an issue for you here, but is the other top reason for null values when you deserialize JSON.
I have problem with windows phone 8 local database. That's how my database code looks:
[Table(Name = "Folders")]
public class FolderTable : INotifyPropertyChanged, INotifyPropertyChanging, IEqualityComparer<FolderTable>
{
private string _folderName;
private string _description;
private string _password;
private string _tileColorName;
[Column(IsPrimaryKey = true)]
public string FolderName
{
get { return _folderName; }
set
{
if (_folderName != value)
{
NotifyPropertyChanging();
_folderName = value;
NotifyPropertyChanged();
}
}
}
[Column(CanBeNull = false)]
public string Description
{
get { return _description; }
set
{
if (_description != value)
{
NotifyPropertyChanging();
_description = value;
NotifyPropertyChanged();
}
}
}
[Column]
public string Password
{
get { return _password; }
set
{
if (_password != value)
{
NotifyPropertyChanging();
_password = value;
NotifyPropertyChanged();
}
}
}
[Column]
public string TileColorName
{
get { return _tileColorName; }
set
{
if (_tileColorName != value)
{
NotifyPropertyChanging();
_tileColorName = value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanging([CallerMemberName] string propertyName = "")
{
if ( PropertyChanging != null)
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if ( PropertyChanged != null )
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
public bool Equals(FolderTable x, FolderTable y)
{
return x.FolderName == y.FolderName;
}
public int GetHashCode(FolderTable obj)
{
return obj.FolderName.GetHashCode();
}
}
Folder add:
public Task Add(Folder folder)
{
var folderTable = GetFolderTable(folder);
_databaseContext.Folders.InsertOnSubmit(folderTable);
return Task.FromResult(true);
}
Commit:
public Task Commit()
{
try
{
_databaseContext.Folders.Context.SubmitChanges();
}
catch (DuplicateKeyException duplicateKeyException)
{
_databaseContext.Dispose();
_databaseContext = new FolderDbContext(FolderDbContext.DBConnectionString);
throw new FolderAlreadyExistsException(duplicateKeyException);
}
return Task.FromResult(true);
}
When I Add new Folder (folder with such name didn't exist before), Commit and then I try add folder with same name DuplicateKeyException is thrown and handled correctly. However after context gets recreated in catch section the next call to Add and Commit folder with same name throws SqlCeException with message: "A duplicate value cannot be inserted into a unique index. [ Table name = Folders,Constraint name = PK_Folders ]". Is that normal behaviour or am I doing something wrong? In what cases DuplicateKeyException is thrown? How to catch SqlCeException? I can't even see System.Data.SqlServerCe namespace.
use reflection to get the type name of the ex eption, if you wish to catch the SqlCeException explicitly:
ex.GetType().Name == "SqlCeException"
This approach seems to work half the time for me.
I can see these lines get executed in the debugger:
agencyListBox.DataBindings.Add(new Binding("DataSource", this.Data.Agencies, "AvailableAgencies"));
agencyListBox.DataBindings.Add(new Binding("SelectedItem", this.Data.Agencies, "SelectedAgency", false, DataSourceUpdateMode.OnPropertyChanged));
The agencies class looks like this:
public AgencyType SelectedAgency
{
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("SelectedAgency");
}
}
public List<AgencyType> AvailableAgencies
{
get
{
return _availableList;
}
set
{
_availableList = value;
OnPropertyChanged("AvailableAgencies");
}
}
So the fields I reference in the binding do exist.
The DisplayMember is set to "Label" which is defined in the AgencyType class:
public event PropertyChangedEventHandler PropertyChanged;
private string _label { get; set; }
public string Label
{
get { return _label; }
set
{
_label = value;
OnPropertyChanged("Label");
}
}
private string _identifier { get; set; }
public string Identifier
{
get { return _identifier; }
set
{
_identifier = value;
OnPropertyChanged("Identifier");
}
}
public AgencyType()
{
Label = string.Empty;
Identifier = string.Empty;
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
The values are displayed as desired.
But then when I change the selection, data.Agencies.SelectedAgency is null!
Does anyone have any tips?
I have a property which looks like this.
public int NumberOfElephants { get; set; }
this property is in an observablecollection and it has to notify another property that it has changed.
how would i do the following
public int NumberOfElephants { get; set { OnPropertyChanged("totalAnimals"); }
without the code needing to be like this
private int _numberOfElephants;
public int NumberOfElephants {
get {
return _numberOfElephants;
}
set {
_numberOfElephants = value;
OnPropertyChanged("totalAnimals");
}
}
You don't. You can't.
Automatically implemented propertieS only work when the property is trivial - when no code is needed for the get/set beyond "return the variable's value" or "set the variable's value". You can make it shorter with reformatting, of course... I'd write that as:
private int numberOfElephants;
public int NumberOfElephants {
get { return numberOfElephants; }
set {
_numberOfElephants = value;
OnPropertyChanged("totalAnimals");
}
}
Actually, I'd use "opening brace on a line on its own" for the start of the set and the start of the property, but I've kept your favoured style for those. But having "single expression get/set implementations" on a single line can make classes with lots of properties much cleaner.
As an alternative to Jon's answer, you can get tools that will do this via IL weaving, such as NotifyPropertyWeaver, also available as a tool through the VS Gallery
For your sample, you should be able to have something like the following, according to their doco on Attributes:
[NotifyProperty(AlsoNotifyFor = new[] { "TotalAnimals" })]
public int NumberOfElephants { get; set; }
public int TotalAnimals { get; set; }
However, based on the example below from their site it might not be required depending on the implementation of TotalAnimals:
Your Code
public class Person : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public string GivenNames { get; set; }
public string FamilyName { get; set; }
public string FullName
{
get
{
return string.Format("{0} {1}", GivenNames, FamilyName);
}
}
}
What gets compiled
public class Person : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
string givenNames;
public string GivenNames
{
get { return givenNames; }
set
{
if (value != givenNames)
{
givenNames = value;
OnPropertyChanged("GivenNames");
OnPropertyChanged("FullName");
}
}
}
string familyName;
public string FamilyName
{
get { return familyName; }
set
{
if (value != familyName)
{
familyName = value;
OnPropertyChanged("FamilyName");
OnPropertyChanged("FullName");
}
}
}
public string FullName
{
get
{
return string.Format("{0} {1}", GivenNames, FamilyName);
}
}
public virtual void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Expanding on the answer by #jeffora
Using NotifyPropertyWeaver you could write this
public class Animals: INotifyPropertyChanged
{
public int NumberOfElephants { get; set; }
public int NumberOfMonkeys { get; set; }
public int TotalAnimals
{
get
{
return NumberOfElephants + NumberOfMonkeys;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
And this would be compiled
public class Animals : INotifyPropertyChanged
{
int numberOfElephants;
int numberOfMonkeys;
public int NumberOfElephants
{
get { return numberOfElephants; }
set
{
numberOfElephants = value;
OnPropertyChanged("TotalAnimals");
OnPropertyChanged("NumberOfElephants");
}
}
public int NumberOfMonkeys
{
get { return numberOfMonkeys; }
set
{
numberOfMonkeys = value;
OnPropertyChanged("TotalAnimals");
OnPropertyChanged("NumberOfMonkeys");
}
}
public int TotalAnimals
{
get { return NumberOfElephants + NumberOfMonkeys; }
}
public virtual void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
With tool like PostSharp you can weave the property to create the boiler plate code code. More, you don't have to implement the INotifyPropertyChanged, PostSharp can do it for you.
See this blog post.
I would use this in C#6
private int numberOfElephants;
public int NumberOfElephants {
get => numberOfElephants;
set {
_numberOfElephants = value;
OnPropertyChanged();
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
This needs
using System.Runtime.CompilerServices;
I have a few properties in a bindingList for a XtratreeList(DevExress) where a child node needs to show a parentnode'e property. I have the following code.
public abstract class ClassBase : INotifyPropertyChanged
{
protected static int initialId = 0;
private int id;
private int parentID;
private string productName;
private string productType;
private string colorProductType;
private void RaisePropertyChanged(string propertyName)
{
if ( PropertyChanged != null )
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public int ID
{
get { return id; }
set
{
if ( id == value )
return;
id = value;
RaisePropertyChanged("ID");
}
}
public int ParentID
{
get { return parentID; }
set
{
if ( parentID == value )
return;
parentID = value;
RaisePropertyChanged("ParentID");
}
}
public string ProductName
{
get { return productName; }
set
{
if ( productName == value )
return;
productName = value;
RaisePropertyChanged("ProductName");
}
}
public string ProductType
{
get { return productType; }
set
{
if ( productType == value )
return;
productType = value;
RaisePropertyChanged("ProductType");
RaisePropertyChanged("ColorProductType");
}
}
public string ColorProductType
{
get { return colorProductType ; }
set
{
if (colorProductType == value)
return;
colorProductType = value;
RaisePropertyChanged("ColorProductType");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}`
My requirement is to have the ColorProductType property changed when the ProductType property changes, basically ProductType is a parent node property and ColorProductType - child's. So on changing the parent's property the child's need to be changed. I have both these properties bound to 2 text boxes. So changing the parent prop should change both textboxes, but the vice versa is not true. RaisePropertyChanged("ColorProductType"); within the parent is not working, colorproducttype is null, what is the issue here?
RaisePropertyChanged does not actually update the property. It simply signals the PropertyChanged event. Something somewhere must subscribe to it and update the other property accordingly. Something like this:
public abstract class ClassBase : INotifyPropertyChanged
{
private string productType;
private string colorProductType;
public ClassBase()
{
this.PropertyChanged += HandlePropertyChanged;
}
private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == "ProductType")
{
// update ColorProductType here
}
}
private void RaisePropertyChanged(string propertyName)
{
if ( PropertyChanged != null )
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public string ProductType
{
get { return productType; }
set
{
if ( productType == value )
return;
productType = value;
RaisePropertyChanged("ProductType");
}
}
public string ColorProductType
{
get { return colorProductType ; }
set
{
if (colorProductType == value)
return;
colorProductType = value;
RaisePropertyChanged("ColorProductType");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
Naturally, this is complete overkill. You can update ColorProductType when ProductType is updated and let the PropertyChanged event and databinding handle the textbox update:
public string ProductType
{
get { return productType; }
set
{
if ( productType == value )
return;
productType = value;
// update ColorProductType here
RaisePropertyChanged("ProductType");
}
}