Is there such thing as an empty date in C# - oracle

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.

Related

Loading null values with CsvDataFileLoader in dbunit

We are using the CsvDataFileLoader to load in our reference data like so:
new InsertIdentityOperation(DatabaseOperation.CLEAN_INSERT)
.execute(connection,
new CsvDataFileLoader().load("/sql/ReferenceData/"));
Is there anyway to put null values into a csv that is loaded into our db.
I don't think there is, I would imagine that , null and NULL would all get interpreted as their string values.
Has anyone managed to do this or know of a work around for this problem?
CsvDataFileLoader uses CsvURLProducer to load and parse the data.
In that class on Line 145 for dbunit 2.4.8 you see the following:
if (CsvDataSetWriter.NULL.equals(row[col])) {
row[col] = null;
}
CsvDataSetWriter.NULL contains the string "null" therefore your assumption that null would get interpreted as a string value appears to be incorrect and you should use this in your CSV.
Of course this means that you can't have the string "null" in your fields but I'm sure this isn't often required.

LINQ Query Result - dynamically get field value from a field name variable

LINQ newbie here
I am trying to get a value of a field - using a fieldName variable.
If I do a watch on row[FieldName] I do get a value - but when I do it on the actual code it will not compile.
string fieldName = "awx_name"
List<awx_property> propertyQry =
(
from property in crm.awx_propertyawx_properties
where property.awx_propertyid == new Guid(id)
select property
).ToList();
foreach (awx_property row in propertyQry)
{
//THIS DOES NOT WORK
fieldValue = row[fieldName];
}
Thanks in advance. Alternatives would be welcome as well
You keep us guessing what you are trying to do here... You need to specify the types of the objects, so it's easy for us to understand and help. Anyway, I think you are trying to get an object based on the ID. Since you are getting by Id, my guess would be the return value is a single object.
var propertyObj =( from property in crm.awx_propertyawx_properties
where property.awx_propertyid == new Guid(id)
select property
).SingleOrDefault();
if(propertyObj != null) {
fieldValue = propertyObj.GetType().GetProperty(fieldName).GetValue(propertyObj, null);
}
Of course, you need to add validation to make sure you don't get null or any other error while accessing the property value.
Hope it helps.
What type is fieldValue? What does awx_property look like? This will only work is awx_property is a key/value collection. It its not, you could use reflection instead.
If it is a key/value collection you are probably missing a cast. (row[FieldName].ToString() or something) Also you are missing a semi-colon in the foreach block.

sqlite3_bind_text for null string

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);

SqlDateTime overflow thrown by Typed DataSet Insert

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.

LINQ - Update null integer data field

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

Resources