none
nullable value for system.int32 RRS feed

  • Question

  • Hi all,

    I have a dataset with a DataColumn having System.Int32 as DataType.

    At the moment the NullValue property is set to (Throw exception).

    If I try to change it in either (Empty) or (Null) I get an error stating : Propertty Value is not valid.

     

    Is it possible to set that property as nullable ?

     

    Many thanks

    Monday, May 16, 2011 3:23 PM

Answers

  •  

    Hi skuanet,

    Welcome!

    DataSet doesn't support System.Nullable<>. You can just add the column as int32 type, the default for a new column is to allow null values. You should check for a null value before casting it.

     static void Main(string[] args)
        {      
          DataTable dt = new DataTable();
          dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
          dt.Columns.Add(new DataColumn("Name", typeof(string)));
          DataRow dr = dt.NewRow();     
          dr[1] = "alan";
          dt.Rows.Add(dr);
          if (dr[0] is DBNull)
          {
            Console.WriteLine("it is null");
          }
          else
          {
            Console.WriteLine((int)dr[0]);
          }      
        }
    

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 7:36 AM
    Moderator
  • In addition to Alan, when setting a DataColumn (AllowNullable = true) to null you should use DBNull.Value, like
    dr[NullableColumn] = DBNull.Value;
    
    Furthermore you get a property Is[NullableColumn]Null() property on strong typed datasets that you can use to check whether the value is null.
    Miha Markic [MVP C#] http://blog.rthand.com
    Friday, May 20, 2011 6:33 AM

All replies

  •  

    Hi skuanet,

    Welcome!

    DataSet doesn't support System.Nullable<>. You can just add the column as int32 type, the default for a new column is to allow null values. You should check for a null value before casting it.

     static void Main(string[] args)
        {      
          DataTable dt = new DataTable();
          dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
          dt.Columns.Add(new DataColumn("Name", typeof(string)));
          DataRow dr = dt.NewRow();     
          dr[1] = "alan";
          dt.Rows.Add(dr);
          if (dr[0] is DBNull)
          {
            Console.WriteLine("it is null");
          }
          else
          {
            Console.WriteLine((int)dr[0]);
          }      
        }
    

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 7:36 AM
    Moderator
  • Hi skuanet, 

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
     
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 20, 2011 1:09 AM
    Moderator
  • In addition to Alan, when setting a DataColumn (AllowNullable = true) to null you should use DBNull.Value, like
    dr[NullableColumn] = DBNull.Value;
    
    Furthermore you get a property Is[NullableColumn]Null() property on strong typed datasets that you can use to check whether the value is null.
    Miha Markic [MVP C#] http://blog.rthand.com
    Friday, May 20, 2011 6:33 AM