Vladimir Zinchenko


Hello all.

I'm trying to use GridView to display a simple MDX query result. If I set AutoGenerateColumns property to true, then it fails on GridView1.Databind(); while executing the following code:

AdomdConnection conn = new AdomdConnection ("Provider=MSOLAP.3;etc., doesn't really matter");
conn.Open ();

AdomdCommand cmd = new AdomdCommand (
"SELECT" +
"{" +
"[Org Structure].[Organization Tree].&[14], " +
"[Org Structure].[Organization Tree].&[15], " +
"[Org Structure].[Organization Tree].&[16], " +
"[Org Structure].[Organization Tree].&[17] " +
"} ON COLUMNS " +
"FROM Test2 " +
"WHERE [Data Levels].[Data Levels].&[0]", conn
);

DataTable dt = new DataTable ();

dt.Load (cmd.ExecuteReader ());

GridView1.DataSource = dt;
GridView1.DataBind ();

conn.Close ();

I get a System.Web.HttpException with the following message:

"The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content."

I am sure that DataTable is not empty, since after I set AutoGenerateColumns to false and make some changes in code (manually bind DataTable to GridView), everything is displayed fine.

dt.Load (cmd.ExecuteReader ());

// -- BEGIN PATCH --
GridView1.AutoGenerateColumns = false;
foreach (DataColumn dc in dt.Columns)
{
BoundField bf = new BoundField ();
bf.HeaderText = bf.DataField = dc.Caption;

GridView1.Columns.Add (bf);
}
// -- END PATCH --

GridView1.DataSource = dt;
GridView1.DataBind ();

However, this approach seems to be quite wrong... I suppose, the databinding could be done automatically in this case.

Do I miss something Any help appreciated.




Re: Displaying MDX result in GridView

Mary Potapova


hello Vladimir,

it seems to be some sort of limitation in System.Web.UI.WebControls.GridView related to types of columns present in the DataTable and the auto-generation of the columns in the grid (System.Windows.Forms.DataGridView does not seem to have the same limitation). For example, if the column might contain varying data, and DataType on it is set to System.Object, then i think the column in the System.Web.UI.WebControls.GridView is not created. The limitation on column types used to be explicitly specified (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx) in docs for System.Web.UI.WebControls.DataGrid class. I don't think the same is stated for the GridView, so maybe most types are supported in the GridView. However, i think i saw it not creating grid columns for the object-typed columns in the DataTable.

i don't think it is specific to adomd.net or executing mdx, but has to do with types of the columns in the DataTable and the System.Web.UI.WebControls.GridView auto generation of columns. If what you see indeed looks like this, i'd suggest perhaps posting a question on http://forums.asp.net/24/ShowForum.aspx. maybe they could clarify more.

hope this helps some,







Re: Displaying MDX result in GridView

Vladimir Zinchenko

Thanks for your response, Mary.

I didn't notice that moment about auto-generated columns limitations in DataGrid. In spite of the same is not stated in GridView documentation, it seems to be the case. I have just tested DataGrid with the same example, and I have a pretty same exception. So, I'm starting to think that workaround I have is ok. Anyway, I didn't see any underwater rocks there in my solution, it works as I expected, it just seemed to be quite unlogical to me.

You are right, my table contains only columns of System.Object type. However, I thought it wasn't a big problem to display those data automatically, simply using ToString () method.

Well, thank you for pointing me to some right direction.






Re: Displaying MDX result in GridView

danparker276

What is the work around for this I'm getting the same error How do I reformat it




Re: Displaying MDX result in GridView

Vladimir Zinchenko

Hello Dan,

There is a workaround code in the second part of the first post. Just create columns manually...





Re: Displaying MDX result in GridView

danparker276

doesn't like

dt.Load(command.ExecuteReader());

Can't load the executed query into a DataTable. The structure of my result set might be wrong. Is there anything I should try

I get the following error:

ExceptioneSystem.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. at System.Data.DataTable.EnableConstraints() at System.Data.DataTable.set_EnforceConstraints(Boolean value) at System.Data.DataTable.EndLoadData() at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue) at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler) at System.Data.DataTable.Load(IDataReader reader) at _Default.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\parkerd\My Documents\Visual Studio 2005\WebSites\WebSite10\Default.aspx.cs:line 77

The following does work to execute and read my data though:

AdomdDataReader reader = myKPICommand.ExecuteReader();

while (reader.Read())

{

// for each item in the resultset, print the value

for (int i = 0; i < reader.FieldCount; i++)

{

Response.Write(reader.GetValue(i));

Response.Write("<br>\n");

}

}






Re: Displaying MDX result in GridView

danparker276

Oh I'm using columns and rows. Anyway to convert the rows in to a DataTable

Or should I just build it manually, I need to customize the columns and rows anyway.