We have our own ORM we use here, and provide strongly typed wrappers for all of our db tables. We also allow weakly typed ad-hoc SQL to be executed, but these queries still go through the same class for getting values out of a data reader.
In tweaking that class to work with Oracle, we've come across an interesting question. Is it bette...
Does any one have a preference on how to check if a value is DBNull? I've found these two statements give me the results I want, but just wondering if there's a preference?
if (any is System.DBNull)
...
same as:
if (any == System.DBNull.Value)
...
Thanks!
Ricardo.
...
I am a little confused about null values and variables in .NET. (VB preferred)
Is there any way to check the "nullness" of ANY given variable regardless of whether it was an object or a value type? Or does my null check have to always anticipate whether it's checking a value type (e.g. System.Integer) or an object?
I guess what I'm lo...
I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so:
DbParameter datePrm = updateStmt.CreateParameter();
datePrm.ParameterName = "@change_date";
And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for nulls.
I thought initially I'd b...
This question comes up occasionally but I haven't seen a satisfactory answer.
A typical pattern is (row is a DataRow):
if (row["value"] != DBNull.Value)
{
someObject.Member = row["value"];
}
My first question is which is more efficient (I've flipped the condition):
row["value"] == DBNull.Value; // Or
row["value"] is DBN...
I want to generate some formatted output of data retrieved from an MS-Access database and stored in a DataTable object/variable, myDataTable. However, some of the fields in myDataTable cotain dbNull data. So, the following VB.net code snippet will give errors if the value of any of the fields lastname, intials, or sID is dbNull.
dim ...
I am currently writing an financial application, and we have a pretty standard customer table. It consists of many mandatory fields, and some optional like Cell/Fax etc.. I'm using NHibernate as a ORM and have all the mappings right. It already works.
I just wonder, how do I "express" in code that a field is not-null without commenting?...
I have a database that hold's a user's optional profile. In the profile I have strings, char (for M or F) and ints.
I ran into an issue where I try to put the sex of the user into the property of my Profile object, and the application crashes because it doesn't know how to handle a returned null value.
I've tried casting the data to th...
In my database, I have a few columns in one of my tables that are bit (boolean) values. They are allowed to be NULL since the fields are not always going to contain data.
I've gone through the process of creating an XSD DataSet using the table and made sure that the AllowDBNull field is set to True.
However, when I pull a down record f...
Hi Guys,
I need some expect advice on how to handle the following:- I have a data field misc_text_2 that is of type varchar(25) and allows NULL. Now if I use the following syntax
<asp:Label ID="lblPrinter" runat="server" Text='<%# iif(eval("misc_text_2") is dbnull.value, "", iif(eval("misc_text_2") like "NA", "None", iif(eval("misc_te...
I just read the top answer at this post:
http://stackoverflow.com/questions/374522/problem-inserting-string-or-null-into-sql-server-database
Correct me if I'm wrong, but can the ??-operator not only be used on two variables of the same type, if not I would greatly appreciate it, if anyone could solve my minor problem.
I tried to inser...
SQLServer int field. Value sometimes null.
DataAdapter fills dataset OK and can display data in DatagridView OK.
When trying to retrieve the data programmatically from the dataset the Dataset field retrieval code throws a StronglyTypedException error.
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public int curr_read...
I'd like to avoid having many checks like the following in my code:
myObj.someStringField = rdr.IsDBNull(someOrdinal) ? string.Empty : rdr.GetString(someOrdinal);
I figured I could just have my query take care of the nulls by doing something like this:
SELECT myField1, [isnull](myField1, '')
FROM myTable1
WHERE myField1 = someCondit...
How do you set a field to DBNull in Entity Framework? The fields are strongly typed so you cannot set it equal to DBNull.Value and I did not find any method to set a field to DBNull. It seems like this is a necessary thing to do, but after much Google research I found nothing about it.
I am trying to set a datetime field in Entity Fra...
I have the following LINQ query which always results in an error when my "Remark" column in dtblDetail is null, even though I test if it is NULL.
var varActiveAndUsedElementsWithDetails =
from e in dtblElements
join d in dtblDetails on e.PK equals d.FK into set
from...
Hi,
I got this parameter:
$objDbCmd.Parameters.Add("@telephone", [System.Data.SqlDbType]::VarChar, 18) | Out-Null;
$objDbCmd.Parameters["@telephone"].Value = $objUser.Telephone;
Where the string $objUser.Telephone can be empty. If it's empty, how can I convert it to [DBNull]::Value?
I tried:
if ([string]:IsNullOrEmpty($objUser.Tele...
I have a function that pulls articles records from an MSSQL database. Some are URLs to PDFs, and other are actual articles stored in the SQL. The articles that are stored do not have a URL (DBNull) in the record, so I want to be able to parse that. I tried a simple test:
If Row.Item("url").GetType Is GetType(DBNull) Then
//' do some...
I've always assumed that DbNull.value was a singleton. And thus you could do things like this:
VB.NET:
If someObject Is DbNull.Value Then
...
End if
C#:
If (someObject == DbNull.Value)
{
...
}
But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison o...
I am getting this error when I retrieve a row with a null DataTime field:
'srRow.Closed_Date' threw an exception of type 'System.Data.StrongTypingException'
How do I properly handle these?
...
I've created a strongly typed dataset (MyDataSet) in my .NET app. For the sake of simplicity, we'll say it has one DataTable (MyDataTable), with one column (MyCol).
MyCol has its DataType property set to "System.Int32", and its AllowDBNull property set to "true".
I'd like to manually create a new row, and add it to this dataset. I cre...