Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top