Results 1 to 5 of 5

Thread: How to Check Null Value in C#?

  1. #1
    Join Date
    Aug 2006
    Posts
    148

    How to Check Null Value in C#?

    Hi Folks,
    I am recently started doing the C# programming language. Before starting this I have done C and C++ (basic programs). I have told to create a small program in which I have to check the null value. I have tried lot of different things but was not successful. So its sincere request to tell me how to Check Null Value in C#..?? If you provide the coding for the same, that would be much grateful. Hoping that someone would help me soon..!!
    The Gaming Machine:
    Processor: Core 2 Duo E6400
    Motherboard: Asus P5W DH Deluxe
    RAM: 2GB XMS2 Corsair PC2-6400
    Video: ATI Radeon X1900XT 512MB w/ Arctic Accellero
    Drive: 74GB "WD Raptor" 10000RPM
    Sound: Creative SoundBlaster X-Fi
    Watercooling: *Soon* Zalman Reserator

  2. #2
    Join Date
    Jul 2006
    Posts
    289

    Re: How to Check Null Value in C#?

    I think that before looking at the code you should understand the basic concepts about the null value, since you are new to C#. You can use the null value in a relational database is used when the value in a column is unknown or missing. Also don't go on the name of the function. A null is neither an empty string nor a zero value for data types and numeric data types respectively. Also for your knowledge I would like to tell you that the System.Data.SqlTypes namespace provides null semantics by implementing the INullable interface.
    Signatures reduce available bandwidth

  3. #3
    Join Date
    Aug 2006
    Posts
    227

    Re: How to Check Null Value in C#?

    The coding of the null value is very much similar to the coding that I have provided. So just have a look at the coding which can make you to understand more properly. Here is the coding for that :
    Code:
    if(dr["AdmissionDate"] == DBNull.Value)
    {
    //Null value in AdmissionDate Column
    }
    else
    {
    //Not Null
    }
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  4. #4
    Join Date
    Jul 2006
    Posts
    442

    Re: How to Check Null Value in C#?

    The example that I have provided is little bit complicated but you can get an idea about checking the null values in C# programming language. I know the code is bit large but will definitely help you.
    Code:
    static private void WorkWithSqlNulls()
    {
        DataTable table = new DataTable();
    
        DataColumn idColumn =
            table.Columns.Add("Roll", typeof(SqlInt32));
        DataColumn descColumn =
            table.Columns.Add("Information", typeof(SqlString));
    
        DataRow nRow = table.NewRow();
        nRow["Roll"] = 123;
        nRow["Information"] = "Side Mirror";
        table.Rows.Add(nRow);
    
        nRow = table.NewRow();
        nRow["Roll"] = SqlInt32.Null;
        nRow["Information"] = SqlString.Null;
        table.Rows.Add(nRow);
    
        SqlBoolean isColumnNull = false;
        SqlInt32 rollValue = SqlInt32.Zero;
        SqlString informationValue = SqlString.Null;
    
        foreach (DataRow row in table.Rows)
        {
            rollValue = (SqlInt32)row["Roll"];
            informationValue = (SqlString)row["Information"];
    
            isColumnNull = rollValue.IsNull;
    
            Console.Write("isColumnNull={0}, Roll={1}, Information={2}",
                isColumnNull, rollValue, informationValue);
            Console.WriteLine();
        }
    If you use the above code, it will generate a DataTable with two columns defined as SqlInt32 and SqlString. The code adds one row of known values, one row of null values and then iterates through the DataTable, assigning the values to variables and displaying the output in the console window.
    "When they give you ruled paper, write the other way..." J.R.J.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to Check Null Value in C#?

    Follow the steps for checking the null values in C# :
    1. As usual you will have to create the variable first . A variable must be declared before using it. You can use the following code for creating the variable :
      Code:
      string myNullVar = null;
    2. Then you will have to check that whether the variable is null or not. You can use the following code for that purpose :
      Code:
      if(myNullVar == null) {
      myNullVar = "purple";
      }
    3. Afterward you will have to check if the variable is not null. In some cases, you may wan to reassign the variable back to null. The following code is useful for that :
      Code:
      if(myNullVar != null) {
      myNullVar = null;
      }

Similar Threads

  1. In Java Script how to Check Null Value?
    By taher in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:18 AM
  2. How to check java string not null or blank
    By Capper in forum Software Development
    Replies: 4
    Last Post: 30-07-2009, 07:11 PM
  3. SQL need to check null or empty string
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 18-06-2009, 11:44 AM
  4. What is null value?
    By Swati in forum Software Development
    Replies: 3
    Last Post: 14-02-2009, 04:39 PM
  5. How to check CPU fan speed? Win32_Fan returns null
    By bluej in forum Windows Server Help
    Replies: 0
    Last Post: 20-03-2007, 07:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,251,526.25674 seconds with 17 queries