This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the point of DBNull?
I have recently been working on a number of sections of code that deal with the insertion of Nullable types into a database.
As I'm sure anyone who has dealt with similar code will be aware of the annoyance of constantly writing conditional logic to deal with the insertion of nulls into a database
IE:
MyValue.HasValue ? MyValue.Value : DBNull.Value;
If(MyValue.HasValue, MyValue.Value, DBNull.Value)
Basically I am just wondering if someone here could be kind enough to explain why DBNull.Value exists and why Null simply couldn't be used?
Here you have one explanation
http://codebetter.com/petervanooijen/2004/04/12/system-dbnull-value-null/
If you call ExecuteScalar and you get null, then it means that no data was found in the database, but if you get DBNull that means you found data but the value was actually null.
Related
This question already has answers here:
C# null check chain in method call
(4 answers)
Closed 3 years ago.
How Can I check null for each object I am using in below chain?
forensicId = Message.Events.SMS.SMS_Mappings.FirstOrDefault().Bug.ForensicId;
More details: I want to access ForensicId from (tables/Proxies loaded by entity framework) BUG which is part of a SMS_Mappings and SMS_Mappings are again part of some table.
Is there any way where I can check if Message is not null or of events are not null and SMS is not null and so on within a single line.
Try this forensicId = Message?.Events?.SMS?.SMS_Mappings?.FirstOrDefault()?.Bug?.ForensicId; It returns null if any object in chain is null or ForensicId value if everything is ok. There is a nice article about such scenarios
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
What is the correct way to use values that may or may not have the value of null?
I have this piece of code that sends an email to a manager if the employee who is taking a holiday request has a manager.
if(holidayRequestForm.Employee.Site.SiteManagerEmail != null)
{
SendMailToManager();
}
However this is causing a NullReferenceException.
What would be the correct way to implement calling the SendMailToManager() if that Employee has one without causing a NullReferenceException.
Is it bad practise to use possible null values this way?
Take a look at Null-conditional operator ?. in the docs in order to understand how you can rewrite your if statement (avoiding NullReference exceptions).
if(holdayRequestForm?.Employee?.Site?.SiteManagerEmail != null)
{
SendMailToManager();
}
I don't find it a bad practice. I do not think you have any other possibilities in the described situation.
This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I map a char property using the Entity Framework 4.1 “code only” fluent API?
I have some code that requires I use a char in c#.
When I store the data in the database as char(1) and return it via Entity Framework, I get a string back.
I'm currently converting it simply by doing string[0]. Sorry if this is a bit of a noob question but is there a more concrete way of doing this?
I think is perfect, a string is a char followed with a null byte, this way you only retrieve first char. It's fast and secure because if you receive an empty string, the char will be null and you can check it.
Under the curtain, this should be the fastest way, but you should probably use checks for null and .length == 0;
I believe that is one of the better ways to handle that situation. I needed to do that before, and I also had a check in there to make sure it wasn't null.
so
string[0]
is the easiest way to handle that. Just make sure you are checking for null and all that good stuff
This question already has answers here:
Convert OracleParameter.Value to Int32
(6 answers)
Closed 7 years ago.
I have a number of stored procedures, and I've been trying to find the best way to get an int in C# out of a stored procedure out parameter. So does anyone have advice on the best way to do this? Also, in some of the procedures the returned value can be null, so using nullable ints for them would be preferred. I have been doing it like this, but is there a better way? (Also, this is how I'm dealing with nulls currently, since 0 isn't a valid result for those procedures)
int sequence = 0;
int.TryParse(comm.Parameters["osequence"].Value.ToString(), out sequence);
Basically I am wondering if there is a way to cast without having to parse. I had been trying but eventually gave up and settled with this, since it seems to work.
I am pretty sure you are doing it the best way. I don't know if using something like Dapper or Massive would gain you the "auto" mapping to the data type correctly or not, but you may want to give it a try.