I have a small question about EntityFramework Data Annotations (Code First).
I want to make integer / decimal required.
[Required]
public int? Nummer { get; set; }
But I have a small problem with this. The integer can't be null in my WPF application. Look at the screenshot bellow:
Because of the required the property wont change to null when the textbox is empty.
It's clearly visible the Selected row still has a number, 3 while it should be empty...
I don't have this problems with 'required' strings.
Why is this a problem? Because now the Opslaan (save) button doesn't get disabled when the number is 'empty'.
I can fix this by doing my data validation again manually.
With a switch and the IDataErrorInfo implementation.
Does someone know if I can solve this with the aid of the Data Annotations?
You could update your binding of your text box like this:
<TextBox Text="{Binding Nummer, TargetNullValue=''}"/>
With this binding the Nummer property gets set to null when the value of the text box is an empty string. If you don't specify TargetNullValue, the empty string cannot be converted to an int? and therefore you get a conversion error.
Just a question to get you right: When you want your Nummer to be required, why do you use int? as the data type instead of int?
Related
I don't know if this is even possible, but I have a large datagrid in my WinForms C# app and I want the user to be able to select any cell and mark it as 'Not Applicable' or some other such note (String). This is fine when the column contains Strings already, but I can't find away to post a String note into a cell which is in a Column designated for Dates. Short of converting the whole column of dates into string values, does anyone know of a way to achieve this kind of effect?
If your column type is DateTime it accepts DateTime values and null only (period).
With a Framework like WPF cou could have a fancy composite control for a single cell which does the tricks you want.
With WinForms you should use the column format string which allows for all kinds of values and do all the processing yourself.
I have a very simple Model with an ID(int) and 4 Non-Null String fields. They can be empty though. If I put [Required] on them it forces a Value to be entered. If I don't do anything and you leave it blank it throws an error as by default Blank==null.
How do I get it to default to String.Empty?
I am trying to work out how to multiply a selected value in a ComboBox by a string value stored in a label. I have tried converting both values to ints but this error keeps appearing:
Unable to cast object of type 'System.Windows.Controls.ComboBoxItem' to type 'System.IConvertible'.
int quantity = Convert.ToInt32(comboBox3.SelectedItem);
int price = Convert.ToInt32(label1.Content);
label2.Content = quantity*price;
Many Thanks
I don't quite remember which exact property of ComboBoxItem it was (I think Text), but you need to get the value of its string rather than the whole object to perform the conversion. After that, just use int's ParseString class method.
You want the SelectedItem's Content, not the SelectedItem itself (assuming WPF):
Convert.ToInt32(comboBox3.SelectedItem.Content);
But if you have set SelectedValuePath and are using some data binding to a model, you can get the selected value in an easier way (without even using convert, if the model is an integer):
comboBox3.SelectedValue
Sheesh...
I think this can be done but I just can't figure it out at the moment.
So I've created this app, that's working fine. But for some reason (too long to explain sorry) I would need to predict next ID to come from data table.
Note that last id + 1 will not work. I've tried this.
var lastProperty = db.Properties.OrderByDescending(p => p.PropertyID).FirstOrDefault();
int propID;
if (lastProperty == null)
{
propID = 1;
}
else
{
propID = 1 + lastProperty.PropertyID;
}
And as long as properties don't get deleted it works...
As soon as one is deleted, it messes up of course, since lets say we delete 6th Property,
last one will be 5th now, and with that code we'll get 6 5(last one) + 1, and I save my model related to Property with PropertyID 6 which I got from that code, and next Property will be 7 since database still remembers that 6 existed and was deleted... Model I intended to have same PropertyID as THAT last Property will not have it, and it'll fail...
Also, I can't add this AFTER saving Property, I realize that might seem as a solution but it's not. It has to be predicted.
Thank you... Please help...
UPDATE
This is the use case I'm trying to accomplish.
Property model with ID Name DataType properties.
List model with ID ListValue PropertyID properties.
When new Property is created user types in the Name, and from premade dropdown list for DataType selects a value, if that value is List, it opens additional form that contains a listbox, textbox and a button (add_Button). User types in the value in a textbox, and clicks the add_Button to add it to List.
Listbox is populated from List model, and add_Button, saves values from textbox to ListValue property of List, also as you've seen, I'm trying to manually add PropertyID to List by predicting it's value...
Then, upon finishing adding all the elements wanted through textbox, by clicking Create button, Property is then saved.
Hope this was clear enough.
It seems like you want to know the current state of the identity column:
SELECT IDENT_CURRENT('Table')
When need to know what your primary key values are going to be before committing to the Db you could use a uniqueidentifier (GUID) as the Pk for your Property and therefore as the Fk for your List entity. That way you can create it your self and it'll always be unique on committal.
You mention Entity Framework, have you tried adjusting your model?
StoreGeneratedPattern can be adjusted to allow automatic update of your local model when you store to the database.
I have this enum:
public enum SupportedISOCurrencySymbol { DKK = 208, EUR = 978, NOK = 578, SEK = 752 }
I save the value for an order in the approved_orders table, Currency field.
I populate the sql insert query with a parameter like:
cmd.Parameters.AddWithValue("?Currency", this.Currency);
Well, If i do debug on the above line I clearly see that this.Currency has value DKK.
Why then it inserts in the DB: 208?
Do you have any ideea?
Thanks.
SupportedISOCurrencySymbol is an enum, which has integer as base. "DKK" represents the number 208.
What you are seeing in the debug window, is the value of .ToString() on your enum, which is defined to return the name of the enum, not the value. If you want to send the string "DKK" to your database, you could simply state it explicitly:
cmd.Parameters.AddWithValue("?Currency", this.Currency.ToString());
Of course, the columns involved in the database would then need to be of a compatible text type.
"DKK" is a friendly name for the value "208" for you (as programmer) to use in your code.
When the code is compiled all occurrences of "DKK" are replaced by "208" which will include the code that inserts the value into the database.
You could change your database schema to hold a string rather than an integer and store the name of the enum rather than it's value - but you would need to parse the string into an enum when reading it back out of the database into your code.
do you need to add a this.Currency.ToString()? so as to get the text not the numeric value of the enum?