0

I am learning C#, and in one of the assignment i have to display the data from an Access file into a DataGrid upon button click using a WPF Application, and a web reference.

There is a SOAP Exception which i am not able to resolve, any help is appreciated, thanks in advance -

Web reference asmx file contains the below method, that is called upon button click -

[WebMethod]
    public ArrayList queryDB()
    {
        string selectStmt = "select * from Students";
        OleDbConnection conn_obj = new DBConnect().checkDBStatus(databaseLocation);
        conn_obj.Open();

        OleDbCommand query = new OleDbCommand(selectStmt, conn_obj);
        OleDbDataReader dbReader = query.ExecuteReader();

        ArrayList dbRecordList = new ArrayList();

        foreach (DbDataRecord dbRecord in dbReader)
        {
            dbRecordList.Add(dbRecord);
        }

        conn_obj.Close();
        return dbRecordList;
    }

WPF file code is as below - "Button click method"

    private void GetData_Click(object sender, RoutedEventArgs e)
    {
        ArrayList datagridList = (ArrayList) websrvc.queryDB();
        dataGrid.ItemsSource = datagridList;
    }

The above code is creating an error - Cannot implicitly convert type 'object[]' to 'System.Collections.ArrayList'

Hence based on a suggestion over web, changed the code as below -

private void GetData_Click(object sender, RoutedEventArgs e)
    {
        object obj = websrvc.queryDB();
        ArrayList datagridList = (ArrayList) obj;
        dataGrid.ItemsSource = datagridList;
    }

Now, there seems to be no errors, but an exception at the below line of code, which i am not able to solve -

object obj = websrvc.queryDB();

{"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: System.Data.Common.DataRecordInternal cannot be serialized because it does not have a parameterless constructor.\n at System.Xml.Serialization.TypeDesc.CheckSupported()\n at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)\n at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)\n at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_queryDBResponse(Object[] p)\n at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\n --- End of inner exception stack trace ---\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\n at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)\n at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)\n at System.Web.Services.Protocols.WebServiceHandler.Invoke()\n --- End of inner exception stack trace ---"}

Prashanth kumar
  • 949
  • 3
  • 10
  • 32
  • I think you are looking at the wrong exception here. The important one seems to be `System.Data.Common.DataRecordInternal cannot be serialized because it does not have a parameterless constructor`. which basically means some class you are trying to serialize uses a `DataRecordInternal`, which doesn't have a parameterless constructor and therefore can't be serialized. You will need to save your data in a different (selfmade) class. – Manfred Radlwimmer Jan 16 '17 at 07:12
  • How about `dataGrid.ItemsSource = websrvc.queryDB();`? – Clemens Jan 16 '17 at 08:40
  • I think your solution is already here :http://stackoverflow.com/questions/26546339/why-exception-occured-when-method-in-webservice-return-arraylist-in-c?rq=1 – tabby Jan 16 '17 at 09:08

1 Answers1

0
  1. Create a class on web service side named Student with all the properties that you want to send to client.
  2. Mark this class as serializable.
  3. Map the DB object properties one by one to this Student class.
  4. From the service, instead of returning ArrayList, return ArrayList();

Let me know if this works.

kevalsing
  • 196
  • 8