I'm working in Asp.net WCF. I'm displaying a table in front end, with the data taken from back end. One of the column having datatype date, contains Null value in backend. When i try to run the solution it displays error page because of the Null value, for the column. How can i avoid this error ?
I think you're looking for SqlDataReader.IsDBNull Method
Check whether the database field contains null value
Handle it with String.Empty
Here is an sample that I have used
logSource = (!reader.IsDBNull(0)) ? reader.GetString(0).Trim() : "";
From your comments,
ObjUnutilizedOwnershipEntities.Dt_VisaValidFrom = (!IsDBNull(osqlDataReader["Dt_VisaValidFrom"]))
? Convert.ToDatetime(osqlDataReader["Dt_VisaValidFrom"].ToString())
: "";
Related
The best way I can explain the problem is by using 2 database. The first database holds all the real time data. The second database holds data that we update periodically. I need to determine if a value needs to be updated in the second database. Lets say a field called Occupation. To give few scenarios:
DB1 has value of null, DB 2 has value of null, no update.
DB1 has empty value, DB2 has empty value, no update.
DB1 has empty value, DB2 has null value, no update.
DB1 has null value, DB2 has empty value, no update.
DB1 has a value, DB2 has empty, null or any other value, update.
DB1 has a null, empty, whitespace, DB2 has an actual value, update.
To achieve I wrote a method like this:
if (string.IsNullOrWhiteSpace(db1.Occupation)) db1.Occupation = string.Empty;
if (string.IsNullOrWhiteSpace(db2.Occupation)) db2.Occupation = string.Empty;
if (!db1.Occupation.Equals(db2.Occupation)) return false;
return true;
My question is would there be a built in way to achieve the scenarios I have above? I've looking into if (String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) and object.Equals(str1, str2) But I don't think those handle the scenarios above due to white space and empty string are 2 different values the way I see it. Maybe I am wrong and this is what this post is to find out if there is a better way.
Would this suit your needs?
return ((db1.Occupation?.Trim() + "") == (db2.Occupation?.Trim() + ""));
? protects for null reference exception
.Trim() cleans up white
space
+ "" converts any null to empty
I want to save null values to a decimal column(allows nulls on db) on a wpf datagrid (with autogenerate columns).
It doesn't allow me to save nulls and shows red error box.
I suspect you are running into a problem that I had but it was a simple text box (not a datagrid). You think you are passing a null but you are really passing string.empty which is neither null nor a decimal. I fixed it with a converter to convert string.empty to null. H.B. (the same H.B.) that edited your question answered mine.
Cannot Assign a Null Value to a Nullable Int32? via Binding
to which kind of database are you binding your datagrid to ? for instance if you bind it to a datatable with ado.net, the datatable is not aware of the underlying sql schema unless you update it yourself. --> see http://support.microsoft.com/kb/310128
with linq2sql it should work fine without this update, with others like MySql i don't know.
Just add this code in AutogeneratingColumn event:
if (e.Column.ToString() ==
"System.Windows.Controls.DataGridTextColumn")
{
(((System.Windows.Controls.DataGridBoundColumn)(e.Column)).Binding).TargetNullValue
= string.Empty;
}
When I am making a query for a data from a MS SQL Server database via C# [.Net 4] Code and adding a parameter to the query with a value of "\0", the datareader is obtained without any exception and when i read the data from the datareader, i get the exception.
For testing purposes i have used the "\0" in place of a GUID, is there a way to fix this issue other than at the input end.
I think you are confusing several flavours of Null (.NET Clr null references, Database NULL values and ASCII 0 character literals).
If you want to pass an empty string in C# pass
string emptyString = String.Empty
To pass a null value pass null;
string nullString = null;
However, to pass database NULL, pass DBNull
dbParam.Value = string.IsNullOrEmpty(str)? DBNull.Value : str;
See also SqlParameter with Nullable value give error while ExecuteNonQuery?
This was originally a problem with Crystal Reports but I tracked down the issue to a single line of code:
foreach (DataRow dr in ds.Tables["CurrentScheduleFields"].Rows)
if (dr["MY_FIELD"].ToString() == string.Empty)
dr["MY_FIELD"] = 0;
I am using an Oracle database. The field is actually a Numeric(2). We are using straight ADO.NET and no Nullable types in this case. In one case, the field in question has a value of an int and the above line of code is fine. The other case, the value of the field is DBNull.Value, and the above or something is converting the 0 to "0" ( a string).
dr is not a `DataReader foreach (DataRow dr in ds.Tables["CurrentScheduleFields"].Rows)
Does anyone know why?
My browser is not letting me click on the Add Comments, so I am adding stuff here. Why can't I do if (dr["MY_FIELD"] == DBNull.Value)
dr["MY_FIELD"] = 0;
the debugger hits that and converts to a string with a value of "0", not 0. I guess the first answer handles this, "So the problem may be that the first "MY_FIELD" is null, convincing ADO.NET that the type for that column is a string." So it's now string with DBNull.Value in it?
My browser is locked down. I can't click on anything except edit this but:
ANSWER: Handle this on the Crystal side by changing the formula to handle a string for this value and not an int. It works!
In some cases, when dealing with DataTable, I've seen it try to "guess" at the type of a column based on the value in that column in the first row in your set. So the problem may be that the first "MY_FIELD" is null, convincing ADO.NET that the type for that column is a string.
You may want to set the schema for your DataTable before you try to load it.
One column in my database (of type double) has some null values.
I am executing a sored procedure to get the data in my app
wipDBTableAdapters.XLSFacturiTableAdapter TAFacturi = new wipDBTableAdapters.XLSFacturiTableAdapter();
var dtfacturi = TAFacturi.GetData(CodProiect);
Then i try to do something like this:
if (dtfacturi[i].CANTITATE == null)
{
//do something
}
this is giving a warning :
The result of the expression is always 'false' since a value of type 'double' is never equal to 'null' of type 'double?
However when i run my code i get the following exception:
StrongTypingException
The value for column 'CANTITATE' in table 'XLSFacturi' is DBNull.
How am I supposed to resolve this ?
While working with data in DB and need to check NULL values use DBNull class instead of .NET null.
Database NULLs are different from null, you should use IsDBNull to check for database NULLs.
Edit: Mixed up VB.Net with C#
Compare with DBNull.Value rather than the VB specific IsDBNull.
Try this:
if (dtfacturi[i].CANTITATE == DBNull.Value)
{
//do something
}
A value of type 'double' is indeed never null; if you want to export into an array of doubles, you need to have two columns in the database, one containing the data and one containing a flag as to whether the data is valid or not.
This is really a bug in your database-to-array adapter code; I can't find any google hits for XLSFacturiTableAdapter so I'm not sure who to shout at.
DBNull is not null
Check DBNull documentation on MSDN
Try this test :
if (DBNull.Value.Equals(dtfacturi[i].CANTITATE))
{
//do something
}
When using TypedDataSets, check if coloumn is null this way..
if (dtfacturi[i].IsCANTITATENull())
{
//do something
}
Also note that, C# null is different than Database null. Type of your coloumn is double which is a value type that can never be null. In order to check if your coloumn value is null you need to compare it with DBNull.Value.