I wish to pass Null Value to the parameter as follow:
_db.AddInParameter(dbCommand, "Id", DBNull.Value, myContactPerson.Id);
I am receiving the following error :
"can not convert "System.DBNull to System.Data.DbType".
I know the meaning of this error.
But i need to supply null value to myContactPerson.Id
How can i achieve this ?
If myContactPerson.Id isn't an auto-number, then why not just pass 0.
DBType should be passed in that parameter and should match your the dbtype (string, int, etc.) for the table that you are comparing with in your database. You would replace your value field "myContactPerson.Id" with DBNull.Value to always pass the null value.
Related
.Where(e => EF.Functions.JsonTypeof(e.Customer.GetProperty("Age")))
This syntax in https://www.npgsql.org/efcore/mapping/json.html?tabs=data-annotations%2Cjsondocument not works!! show this error: cannot implicitly convert "string" to "bool"
The sample syntax from the link is simply incomplete. JsonTypeof returns string an according to the link is mapped to jsonb_typeof which
Returns the type of the outermost JSON value as a text string. Possible types are object, array, string, number, boolean, and null.
So the correct sample usage in Where clause would compare the return value of the method to a string containing one of the aforementioned values, for instance
.Where(e => EF.Functions.JsonTypeof(e.Customer.GetProperty("Age")) == "number")
I have a model:
case class MyModel(
id: Pk[Long] = NotAssigned,
startsAt: Option[DateTime] = None,
addedAt: Option[DateTime] = None
)
object MyModel {
// .....................
SQL("""
INSERT INTO my_table(starts_at)
VALUES ({startsAt})
"""
).on('startsAt -> newItem.startsAt).executeInsert()
}
Both startst_at and added_at have a default value of now() in Postgresql and don't allow null values in them. It doesn't cause any error for addedAt (because I never pass it to the server from the client) but it does cause the error for startsAt if it's not specified at newItem.startsAt and, thus, is equal to None and, thus, it's being passed as null.
org.postgresql.util.PSQLException: ERROR: null value in column "starts_at" violates not-null constraint
What I want is be able to specify startsAt whenever I want it and pass it to the server, meaning if I specify it then that value should be passed to the server, if not - nothing should be passed and the server should use its default value now(). I don't want to specify the default value at the client because it's already set at the server at the db level.
How about this SQL fix:
insert into my_table(starts_at)
values (COALESCE({startsAt}, now())
Updated: requirement is to use the default value of the column
The only way that I know of to get the server to use the default value of a column in an insert, is not to mention that column in the columns list. For example (not tested):
startsAt.map { date =>
SQL("""insert into my_table(starts_at) values({startsAt})""")
.on('startsAt -> date)
.execute()
}.orElse {
SQL("""insert into my_table() values()""")
.execute()
}
I am in scenerio where I need to inset a dateTime value in the database using the EF 4.1.
DateColumn in database in Nullable.
While creating the entity i am filling it as,
DTCLOSE = referenceProblemLog.DateClosed.HasValue ?
referenceProblemLog.DateClosed.Value.ToFeedFormatString() :
System.DBNull.Value.ToString(),
where ToFeedFormatString is an extension method.
Now problem i observed is, if I have a proper value then it is inserted correctly but
when i dont have a proper date value, i want to insert NULL in database column. However EF is saving column with Empty string
I tried to change the StoreGeneratedPattern for the field to "Identity" but problem with it is, I cant assign value to DTCLose field.
How can i have both the things?
1. EF should insert NULL in database when proper value is not there
2. proper value otherwise
Please help
Thanks
Anup
This is wrong System.DBNull.Value.ToString() because at the end you will have string object, ToString() never returns null.
You have to set DTCLOSE = null
Also, with EF you don't have to use DBNull, you just define nullable property and set it null
You Can use this :
Nullable<DateTime> date=null;
var entity = new Model()
{
GoDate = date
};
DataContext.Models.Add(entity);
DataContext.SaveChanges();
Background
A ResultSet has many Double value fields (with patterns like "###0.000"). Some values can be null.
Problem
I want to replace null values with "N/A", which is a String and cannot print to a Double field. Printing "0.00" for null values is unacceptable.
Using an PrintWhenExpression value of ($F{value} != null) ? $F{value} : "N/A" does not work; it is not possible to use patterns in that way.
Idea
Add hidden fields that write "N/A". These fields will be printed only if value is null.
Question
Is there a better solution, and if so, what is it?
Thank you.
Solution #1
Your solution:
Use a regular Double field (doubleField) for the column value.
Add a static String text field at the same location.
Change the Double field to Blank When Null.
Set the PrintWhenExpression value for the String text field to: $F{doubleField} == null.
Solution #2
The problem is, as you pointed out, that a Double and a String are two different data types. You can assign a String variable to the value of the Double using an appropriate expression. Then use the String variable as the field. The expression might resemble:
($F{doubleField} == null) ?
"N/A" : new java.text.DecimalFormat("#.##").format($F{doubleField})
(Note: My preference is to use == instead of !=. Think positive.)
Solution #3
Change the SQL statement to pre-format the Double as a text string, and use the "N/A" in the string (by using a CASE or DECODE statement in the query).
Avoid this solution, though, as it is not maintainable.
Recommendation
Do not hard-code the "N/A" string throughout the report(s); put the "N/A" text in a constant, or a parameter with a default value of "N/A".
You can try something like this in your field expression:
("Your static text "+(($F{field}!=null)?$F{field}:""))
Or, if you don't want your static text to be visible when the field is null, try putting $F{field}!=null in your PrintWhenExpression.
I want to convert a query string object into a datetime in this format:- "YYYY-mm-dd HH:mm:ss.xxx" in C#.net. But when i am using Convert.ToDateTime(object) for getting the datetime value then an exception is being fired.
Could anyone can provide me the iFormatProvider for the same.?
Thanks
Varun Sareen
Have a look at DateTime.TryParseExact Method
I think your problem is trying to convert the QueryString object, instead of getting a value from the query string and then converting the value.
A QueryString object is a keyed collection of the values specified in a URL from an HTTP request. So if you have a URL like: http://example.com?a=1&b=2&c=3, the request QueryString object will contain three values: 1, 2, and 3. To access the values you would use the keys:
var aValue = Request.QueryString["a"];
And the variable aValue would then contain the string value "1" (without the quotes).
After getting the value from the query string, you can use the TryParseExact method suggested by #astander.