Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, usually when I'm trying to get data from a Db using WCF, I serialize the object in a string and then at client level I deserialize. From your snippet, I guess you should return a DataTable, serialize it and then deserialize when you have to fill your listbox. What I'd do is this: In your IService interface I declare something like:</p> <pre><code>[OperationContract] string GetDataTableFromDB(); </code></pre> <p>In your Service.svc.cs file I'd implement something like:</p> <pre><code>public string GetDataTableFromDB() { // your code to retrieve from DB the DataTable // there are plenty examples here in stackoverflow // I will use "retrievedDataTable" name for the retrieved table return Serialize&lt;DataTable&gt;(retrievedDataTable); } </code></pre> <p>To be sure that the serialization works correctly, I'd create a class that will contain the DataTable object because JSON might complain if the object that it is trying to serialize does not have the [DataMember] flag. So:</p> <pre><code>[DataContract] public class Result { [DataMember] public DataTable ResultDataTable { get; set; } } </code></pre> <p>In your client, I'd call the method for having the datatable:</p> <pre><code>public void ShowData2() { try { ServiceReference6.Service1Client obj6 = new ServiceReference6.Service1Client(); DataTable table = Deserialize&lt;DataTable&gt;(obj6.SelectSavedProcessInformation()); if(table != null) { foreach(DataRow row in table.Rows) { // fill the listbox } } </code></pre> <p>The seirialization and deserialization code that I use is this:</p> <pre><code> public class JsonHelper { public static string JsonSerializer&lt;T&gt;(object obj) { try { if (obj == null) return null; using (MemoryStream ms = new MemoryStream()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); serializer.WriteObject(ms, obj); ms.Position = 0; using (StreamReader reader = new StreamReader(ms)) { return reader.ReadToEnd(); } } } catch { return null; } } public static T JsonDeserialize&lt;T&gt;(string source) { try { if (source == null) return default(T); using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(source))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); if (ms.Length == 0) return default(T); return (T)serializer.ReadObject(ms); } } catch { return default(T); } } } </code></pre> <p>I never used DataTable with serialization but I hope it works</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload