Hussain Md
Hussain Md

Reputation: 129

why dataTable.Rows[0]["columnName"] == null. not working?

I have a code like below

txtbox1.Text = dtDetails.Rows[0]["columnName"] == null ? "--": dtDetails.Rows[0]["columnName"].ToString().Trim();

above line is always not null even though dtDetails.Rows[0]["columnName"] is null whereas

txtbox1.Text = dtDetails.Rows[0]["columnName"] == DBNull.Value ? "--": dtDetails.Rows[0]["columnName"].ToString().Trim();

works perfectly. why is that? and how a null different over DBNull.Value

Upvotes: 2

Views: 6857

Answers (2)

Dai
Dai

Reputation: 155185

ADO.NET (DataSet, DataTable, and DataRow) represents SQL NULL values with DBNull.Value - this is to differentiate between runtime (C#/.NET) null from SQL NULL.

I strongly recommend reading this QA: What is the point of DBNull?

You can simplify your code, btw using DataRowExtensions - (add a reference to System.Data.DataSetExtensions.dll)

txtbox1.Text = dtDetails.Rows[0]["columnName"] == DBNull.Value ? "--": dtDetails.Rows[0]["columnName"].ToString().Trim();

Can be changed to:

txtbox1.Text = dtDetails.Rows[0].Field<String>("columnName")?.Trim() ?? "--";

The DataRowExtensions.Field<T>( this DataRow row, String name ) extension method checks for DBNull.Value and returns null - otherwise it returns the desired field without the overhead of looking-up the DataRow value twice as your code currently does.

Update for Visual Studio 2010

The OP says they're using Visual Studio 2010 (in 2019?!), so there are a few options:

  1. Upgrade to Visual Studio 2019. Unlike the jump from VS2008 to VS2010, no significant functionality has been removed from VS2010 that isn't still available in VS2019 - you can still target .NET Framework 2.0 and Windows XP in VS2019, for example.
    • The free (as in beer) Visual Studio 2019 Community is as-featured as VS2010 and is now free. You can also use the free and open-source Visual Studio Code to work with Visual C# projects as well.
    • (VS2010 removed useful things from VS2008, like the Document Explorer, support for the .NET Compact Framework, the Object Test Bench feature, ran slower, and permanently changed how editor indentation worked (for the worse, imo))
  2. If you have to use VS2010 (but why though?) you can hack your MSBuild project files to use a more modern C# compiler - but this is a complicated process - and there's no IDE editor support (so you'll get red-squiggly lines everywhere even though the project will build fine).

    • Visual Studio 2012 and later no-longer require one-way project and solution file upgrades when working with Visual Studio 2010 SP1 or later files - so you can use VS2019 with VS2010 files without anyone still using VS2010 complaining.
  3. You could change the expression to perform Trim() after the ?? "--" clause:

    txtbox1.Text = ( dtDetails.Rows[0].Field<String>("columnName") ?? "--" ).Trim();
    
  4. You could use an extension method as a hacked-on conditional operator in a lambda:

    static class Extensions
    {
        public static TOut N<TIn,TOut>( this TIn value, Func<TIn,TOut> whenNotNull )
            where TIn : class
            where TOut : class
        {
            if( value == null ) return null;
            return whenNotNull( value );
        }
    }
    

    Used like so:

    txtbox1.Text = dtDetails.Rows[0].Field<String>("columnName").N( v => v.Trim() ) ?? "--";
    
  5. Or extended further:

    static class Extensions
    {
        public static TOut N<TIn,TOut>( this TIn value, Func<TOut> whenNull, Func<TIn,TOut> whenNotNull )
            where TIn : class
            where TOut : class
        {
            if( value == null ) return whenNull();
            return whenNotNull( value );
        }
    }
    

    Used like so:

    txtbox1.Text = dtDetails.Rows[0].Field<String>("columnName").N( () => "--", v => v.Trim() );
    

Upvotes: 6

Prophet Lamb
Prophet Lamb

Reputation: 610

You have to check for the SQL NULL not the Net Framework null. They are two different Things

Upvotes: 0

Related Questions