5 Replies - 607 Views - Last Post: 03 February 2016 - 10:36 AM Rate Topic: -----

#1 henryvuong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 05-April 09

How to check if a variable with type int is null

Posted 02 February 2016 - 08:29 PM

I have an SQL table lilke this:

table: Employees
ID (int)	Name (varchar)	Age (int) 	Department
1	        John		30		Sales
2	        Smith		NULL		Sales
3 	        Bob		45		QC



I want to get the age of a specific employee. My C# code is as followed:

string str = "Select * from Employees";
SqlDataAdapter da = new SqlDataAdapter();
try
{
     SqlCommand sqlCmd = new SqlCommand(str, Conn);
     da.SelectCommand = sqlCmd;
}
catch (SqlException ex)
{
     MessageBox.Show(ex,ToString());
}
DataTable dt = new DataTable();
da.Fill(dt);

var age = from row in dt.AsEnumerable() 
	where row.Field<int>("ID") == 2
	select row.Field<int>("Age");

If (age.FirstOrDefault<int>() != null)
	//do something
else
	//do something else



The problem with the If statement is that the condition is always true since a value of type 'int' is never equal to 'null', so it never goes to the 'else' part. I tried to change it to
if (age.FirstOrDefault<int>() != 0)
but it still does not work.

Note that this is a part of a larger code so using SqlCommand.ExecuteScalar is not an option for me,

even though it may work.

Is This A Good Question/Topic? 0
  • +

Replies To: How to check if a variable with type int is null

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,265
  • Joined: 05-May 12

Re: How to check if a variable with type int is null

Posted 02 February 2016 - 08:58 PM

An int cannot be null. It's a value type, and value types cannot be null (unlike reference types).

I think that what you are asking is how to check if a database field value is null.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,265
  • Joined: 05-May 12

Re: How to check if a variable with type int is null

Posted 02 February 2016 - 09:38 PM

You'll want to use the nullable type instead of the raw type. In your case int? or Nullable<int>.

Here's some sample code using your code as the basis:
using System;
using System.Linq;
using System.Collections.Generic;

namespace SimpleCSConsole
{
    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int? Age { get; set; }
        public string Department { get; set; }
    }

    class Program
    {
        public static void Main()
        {
            var employees = new List<Employee>()
            {
                new Employee() { ID = 1, Name = "John",     Age = 30,       Department = "Sales" },
                new Employee() { ID = 2, Name = "Smith",    Age = null,     Department = "Sales" },
                new Employee() { ID = 3, Name = "Bob",      Age = 45,       Department = "QA" },
            };

            var age = from employee in employees
                      where employee.ID == 2
                      select employee.Age;

            if (age.FirstOrDefault().HasValue)
                Console.WriteLine("Got an age.");
            else
                Console.WriteLine("Age is null");
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 593
  • View blog
  • Posts: 3,823
  • Joined: 12-January 10

Re: How to check if a variable with type int is null

Posted 03 February 2016 - 05:27 AM

you can also use
if(yourIntVariable != null)  checks if it isnt null


if(yourIntVariable == null)   checks if it is null


sorry didnt realize you were trying to use it as a database call

skydiver 's way is better

This post has been edited by DarenR: 03 February 2016 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 446
  • View blog
  • Posts: 2,179
  • Joined: 07-April 08

Re: How to check if a variable with type int is null

Posted 03 February 2016 - 06:46 AM

if your looking for the age of a specific employee and you know the employee's id why not just use sql to pull just that record instead of pulling the entire table then narrowing down your request?

var sqlCommand = "SELECT * FROM Employees WHERE ID = @EmpID";

try
{
    using(var cmd = new SqlCommand(sqlCommand, Conn)
    {
        using(var da = new SqlDataAdapter())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@EmpID",2);
            
            da.Fill(dt);
        }       
    }
}


Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to check if a variable with type int is null

Posted 03 February 2016 - 10:36 AM

View PostDarenR, on 03 February 2016 - 06:27 AM, said:

you can also use
if(yourIntVariable != null)  checks if it isnt null


if(yourIntVariable == null)   checks if it is null


sorry didnt realize you were trying to use it as a database call

skydiver 's way is better


Skydiver also made it clear that int is not a nullable type. Checking for null like that is useless; it will never be null.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1