I use the codes to access sqlite
sqlite3_bind_text(statement,1,[myString UTF8String],-1,SQLITE_TRANSIENT);
It works.
But if myString is NULL, it always causes updating the record in table failed.
(or nothing changed)
How to process when myStrign is NULL?
Welcome any comment
Thanks
Use sqlite3_bind_null(statement, 1); when your string is NULL.
if (myString)
sqlite3_bind_text(statement,1,[myString UTF8String],-1,SQLITE_TRANSIENT);
else
sqlite3_bind_null(statement,1);
Related
Save() returns the saved object with wrong attributes, the attribute "price" should return a rounded value.
Let's say I have the following table:
CREATE TABLE OBJ(
ID INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30) NOT NULL,
DESCRIPTION VARCHAR(200) NOT NULL,
PRICE DECIMAL(10,2) NOT NULL,
);
If I do:
Obj newobj = new Obj("obj name", "brief description", 12.555);
repository.save(newObj);
Looking into the database, the object was saved correctly, so the price saved is 12.56 but method save() has returned the saved object with price = 12.555. Could this behavior be a bug?
Could this behaviour be a bug?
No.
This is the expected behaviour.
The only value that gets returned after it was modified by the database is the id.
If you want such values to get updated you need to require from the database.
When doing so you'll have to makes sure to do so without the entity being in the persistence context, to avoid the 1st level cache to interfere with your attempt to reload the entity.
As far as I understand the problem is about the value stored into the database and in order to fix it you have to replace PRICE DECIMAL(10,2) NOT NULL with PRICE DECIMAL(10,3) NOT NULL. Then the stored value will retain 3 digits after comma.
I'm creating a new record in CRM plugin(by reading the data from a related record) and the data that I'm passing may / may not contain "OptionSetValueCollection". Whenever the value for the OptionSetValueCollection is null the IOrganization.Create is throwing a Generic SQL exception.
Currently I'm checking the submitted value for null and when not null I'm not submitting a value for the created object.
My question is why does OptionSetValueCollection not taking null? Is this a platform issue?
I've also tried creating a List<OptionSetValue> object and adding the incoming OptionSetValues from the OptionSetValueCollection and then passing it to the target attribute, tried passing in null and also used the null-coalescing operator all with no luck.
//Earlybound code
Account account = new Account(){
Name = newBrand,
new_accounttype = new OptionSetValue((int)new_AccountType.Brand),
TerritoryId = siteRequestRecord.new_territoryid,
new_category1 = siteRequestRecord.new_category1 ?? null,
};
if (category2 != null)
{
account.new_category2 = siteRequestRecord.new_category2;
}
service.Create(account);
Seems to be a long outstanding issue.
There is a bug related to multiselect optionset - if you set it to null during creation that will trigger an error. But the same code that sets field to null works fine during update.
So if you set it to null during Create just don't set field value and as a result you'll get blank value of a field.
If I understand you want to set Optionset to null. use below code it shall work and set null for your optionset
new_accounttype = null;
I am trying to input a date into an Oracle database. Sometime this value will be null. So in the function that I am writing I need a way to be able to pass in this null value. I am passing in this date as a parameter to stored proc. This parameter can be null. I am using the Oracle.DataAccess dll to get this thing to work. If it is indeed null, I am thinking of just throwing in a null variable. Do you think that would work??? Here is how I currently am setting up this scenario...
cmd.Parameters.Add("ACTIVE_DATEIn", DateTime.Parse(ActiveDate));
conn.Open();
outcome = cmd.ExecuteNonQuery();
Active Date is the possible null variable that I am going to pass in. Obviously you can't Convert a null value into a date Time. What would you guys suggest doing?
Since DateTime is a struct, it cannot contain a null value.
Some people use DateTime.MinValue to store a null value. A better approach would perhaps be to make ActiveDate a DateTime? instead.
Update:
You can also try something like:
cmd.Parameters.Add("ACTIVE_DATEIn", (ActiveDate == null ? OracleDate.Null : OracleDate.Parse(ActiveDate)));
You can make DateTime implement INullable by placing a ? after the type.
DateTime? date;
Which is ofcourse equivalent to
Nullable<DateTime> date;
You can use DateTime.MinValue instead of null, or make your DateTime object nullable.
You may need to use DBNull.Value in the case that your ActiveDate represents a null.
I'm using a Typed DataSet with an Insert statement; I have a table that has a smalldatetime field defined to accept null values. When I insert from a .NET 2.0 FormView, I get a "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."
Now, I've read this post, and the parameter as sent to the class constructor is defined as
global::System.Nullable<global::System.DateTime> DoB
So, it looks like it should accept a Nullable obj. Additionally, the generated code is testing the value sent.
if ((DoB.HasValue == true)) {
command.Parameters[6].Value = ((System.DateTime)(DoB.Value));
}
else {
command.Parameters[6].Value = global::System.DBNull.Value;
}
Specifically, the error is occurring when generated SqlClient.SqlCommand.ExecuteScalar() runs:
try {
returnValue = command.ExecuteScalar();
}
So, I guess my question is: how do I use a Typed DataSet to set a blank value (passed from a FormView on CommandName=Insert) to a null in a database?
Ok, so here's what worked for me. First, to reiterate, I've got a Typed DataSet with DataAdapters that's generating the ADO objects. So, on my page, I can create a ObjectDataSource with the type that points to my adapter, and then name the different access methods housed there-in.
No, I have an Insert to a table where basically all the columns are nullable; some varchar, some smalldatetime.
When I submit an empty form, I'd like nulls to be entered. They're not and lots of various errors are thrown. What I ended up doing is subclassing the ObjectDataSource to gain access to the Inserting event. (subclassed for reusability) In the Inserting event, I looped through the InputParameters, and if it was a string and == "", I set it to null. Also, you cannot set ConvertNullToDBNull to true; that causes the strings to fail. This successfully allowed the Nullable to remain null.
I have got a field with data type int? price and allow null, when I set
book.price = null;
and update, it is not saved and does not throw any exceptions, when I change value # null, it is ok. I want set it null.
Make sure the field is nullable in the database and is reflected so in the dbml.
Regenerate the dbml for the table just to be sure. (Drop tabel from dbml and add again)
All I can say is it works for us so this is very strange.
I am not suer I understand the question, this part is unclear:
"when I change value # null, it is ok"
Did you make sure to call
db.SaveChanges();
(where [db] is the name of your Entites object)
It is a simple mistake that I make often :P