0

I'm querying a DB2 server using WebMatrix. I figured it would be easier for quick, proof of concept related tasks... but using DB2 rather than its built-in providers has been challenge, to say the least.

I know my results are coming back since I can iterate thru my dataset just fine and see my results.

I'm trying to use the built-in WebGrid() function within WebMatrix to display my results and keep getting errors:

CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, string, int, bool, bool, string, string, string, string, string, string, string)' has some invalid arguments

My code is basically:

OleDbConnection conn = new OleDbConnection(connString);
DataTable td = new DataTable;
OleDbDataAdapter da = new OleDbDataAdapter("SELECT USER, GROUP FROM DBO.TEST", connString);
ds = new DataSet();
da.Fill(ds);

var results = new WebGrid(ds.Tables[0].Rows);

Of course, its failing on:

var results = new WebGrid(ds.Tables[0].Rows);

Any help or direction would be appreciated. I'm assuming I need to convert this to a System.Collections.Generic.IEnumerable, but no idea how to accomplish this...?

user500741
  • 833
  • 5
  • 14
  • 25

1 Answers1

0

You can't pass a DataTable into a WebGrid. The WebGrid wants an Enumerable that has public properties that it can use as column names. What you can do is to use Linq To DataSet to project the content of the DataTable into a more suitable form. The WebGrid will accept an anonymous type:

OleDbConnection conn = new OleDbConnection(connString);
DataTable td = new DataTable;
string query = @"SELECT USER, GROUP FROM DBO.TEST";
OleDbDataAdapter da = new OleDbDataAdapter(query, connString);
da.Fill(dt);
var data = dt.AsEnumerable().Select(r => new { 
        User = r.Field<string>("USER"),
        Group = r.Field<string>("GROUP")
    });

WebGrid results = new WebGrid(data);
Mike Brind
  • 28,238
  • 6
  • 56
  • 88