2

I have an ASP.NET page which houses a Gridview.

I am trying to bind an IDataReader object to the grid. This IDataReader object is assigned by calling a few more layers of code(actually other .ent project dlls that form the Controller layer and the DB Layer) and then finally gets bounded to my grid.

At gridview1.Datasource = dr (dr is my IDataReader), I dont see any problem.

But at the line gridview1.Databind I get an exception and the details in the exception object are as follows:

{"Type 'System.Data.Common.DbEnumerator' in Assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."}

I am not sure on whats going wrong in the application. Any thoughts or comments?

Edit 1

Adding the stacktrace exception:

Server stack trace: 
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Runtime.Remoting.Channels.CoreChannel.SerializeBinaryMessage(IMessage msg, Stream outputStream, Boolean includeVersions)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.SerializeResponse(IServerResponseChannelSinkStack sinkStack, IMessage msg, ITransportHeaders& headers, Stream& stream)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System.Collections.IEnumerable.GetEnumerator()
   at System.Collections.IEnumerable.GetEnumerator()
   at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
   at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
   at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
   at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
   at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at System.Web.UI.WebControls.GridView.DataBind()
   at Drive.CampaignManager.CreateQuery.GridViewBind() 
   in Mycode.aspx.cs

Could not understand much from the above stacktrace.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
SARAVAN
  • 14,571
  • 16
  • 49
  • 70

2 Answers2

2

Look at the stack trace of the exception. It will show you what code was attempting to serialize the DbEnumerator.

Joe
  • 122,218
  • 32
  • 205
  • 338
2

From reading the stack trace you've posted, it looks like there's some remoting going on between your layers - is this code running across multiple machines? Classes like DataReaders (SqlDataReader, EntityDataReader etc) that aren't serialisable (or serializable if you're in the US :-) ) can't be passed across remote boundaries.

You'll need a different design to make this work, one that passes serialisable objects between the layers of your code. Passing a DataSet is likely the lowest friction method to make your UI code work because you can just replace

gridview1.Datasource = dr

with

gridview1.DataSource = ds
PhilPursglove
  • 12,511
  • 5
  • 46
  • 68
  • 1
    phil I can understand something from what you are saying. The controller layer and Data access layer are separate projects in my web solution. But the data access layer is invoking some framework related objects(written by someone) and its methods. I dont know much about the framework because we give some inputs say table name or id and it fetches the output. So I am suspecting that area only, whether if the IDataReader returned by that layer is not at all serializable. I need to dig around this. Let me see. Thanks for your comment. – SARAVAN Aug 06 '12 at 13:50
  • 1
    Your guess is correct. Its remoting. My data reader is actually returning the datareader from the dataaccesslayer which is actually in a different domain or to say its outside my application boundary. So I get that serializatoin error. Thanks for giving me the right direction to check the issue. Now I am loading the datareader to a table withing the Data Access Layer and I am using the datatable in my application, which works great. Thanks and this is the accepted answer :) – SARAVAN Aug 09 '12 at 16:09