2

I am trying to find if in any row the column "flag" in dbo.address is set to 1. And then simply set a bool default = true or default = false if its 0.

 DataTable dt = new DataTable();

 dt = AddressData.Data;

 foreach (DataColumn dtColumn in dt.Columns)
            {

            }

Or maybe something like this ?

    dt.Select("WHERE Flag = " + 1);
klashar
  • 2,519
  • 2
  • 28
  • 38
xterminal0
  • 135
  • 3
  • 11

2 Answers2

1

Can't you use linq ?

var default = dt.AsEnumerable().Any(m => m.Field<int>("flag") == 1);
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • I am getting a 'System.Data.DataTable' does not contain a definition for 'AsEnumerable' and no extension method 'AsEnumerable' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference? – xterminal0 Feb 13 '14 at 18:53
  • I am using Data but DataSetExtensions is not available!?!? – xterminal0 Feb 13 '14 at 19:03
  • @xterminal0 no, sorry, it should be in System.Data... What's your framework version ? – Raphaël Althaus Feb 13 '14 at 19:10
  • @xterminal0 but look at this : http://stackoverflow.com/questions/9217272/datatable-does-not-contain-definition-for-asenumerable : you need to add the correct reference. – Raphaël Althaus Feb 13 '14 at 19:12
0

Try this:

dtColumn = dt.Cols.Find("flag = 1");

You can then assign a bool variable to either true or false.

See if this works

if (dtColumn["flag"].ToString() == 1)
    default = true;
else
    default = false;