Checking for Null values
-
Hi, I retrieving the value from Database. While retrieving i am checking for data is null or not. But if is not working properly. This is my code, tell me if any correction is required If dr is a datareader then, if (dr[0].ToString() == null) { //code } else { // code } Please help me.
-
Hi, I retrieving the value from Database. While retrieving i am checking for data is null or not. But if is not working properly. This is my code, tell me if any correction is required If dr is a datareader then, if (dr[0].ToString() == null) { //code } else { // code } Please help me.
srikantha_nagaraj wrote:
But if is not working properly.
What do you mean by not working Properly. What problem are you getting ? You can use
String.IsNullOrEmpty()
Method to implement it.string S=dr[0].ToString();
if (String.IsNullOrEmpty(S) == true)
{
// Do your job !
}Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
-
Hi, I retrieving the value from Database. While retrieving i am checking for data is null or not. But if is not working properly. This is my code, tell me if any correction is required If dr is a datareader then, if (dr[0].ToString() == null) { //code } else { // code } Please help me.
Check like this :
if(DBNull.Value.equals(dr[0]))
{
//code
}
else
{
//code
}This would be better.:rose:
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
srikantha_nagaraj wrote:
But if is not working properly.
What do you mean by not working Properly. What problem are you getting ? You can use
String.IsNullOrEmpty()
Method to implement it.string S=dr[0].ToString();
if (String.IsNullOrEmpty(S) == true)
{
// Do your job !
}Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
Abhijit Jana wrote:
dr[0].ToString();
This will fail if value of dr[0] IS NULL.
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
Hi, I retrieving the value from Database. While retrieving i am checking for data is null or not. But if is not working properly. This is my code, tell me if any correction is required If dr is a datareader then, if (dr[0].ToString() == null) { //code } else { // code } Please help me.
Handle it in database. While retrieving value use ISNULL(CULUMNNAME, 0.00) AS NewColumn Or if you want to handle it at code behind use as told in above replies
Inderjeet Kaur Sr. Software Engg