Reputation: 9
I am struggling greatly on how to populate listbox from the WCF. This is my old code i used before I have tried to implement my WCF:
lstProcesses.BeginUpdate();
lstProcesses.Items.Clear();
while (dr.Read())
{
lstProcesses.Items.Add(dr.GetString(0));
}
dr.Close();
con.Close();
lstProcesses.EndUpdate();*/
My New attempted code was:
public void ShowData2()
{
try
{
ServiceReference6.Service1Client obj6 = new ServiceReference6.Service1Client();
if (obj6.SelectSavedProcessInformation())
{
string passed = "";
lstProcesses.Items.Add(obj6);
}
else
{
string failed = "";
}
}
catch
{
}
}
My WCF consists of this:
public bool SelectSavedProcessInformation()
{
SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Processes FROM SaveProcesses", con);
var result = cmd.ExecuteReader();
if (result.HasRows)
{
con.Close();
return true;
}
else
{
con.Close();
return false;
}
}
}
Im pretty sure I am doing something wrong here as the listbox is getting filled with "loging.cs.servicereference6.Service1Client."
Any ideas on how to implement the wcf?
Upvotes: 0
Views: 513
Reputation: 2014
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:
[OperationContract]
string GetDataTableFromDB();
In your Service.svc.cs file I'd implement something like:
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<DataTable>(retrievedDataTable);
}
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:
[DataContract]
public class Result
{
[DataMember]
public DataTable ResultDataTable { get; set; }
}
In your client, I'd call the method for having the datatable:
public void ShowData2()
{
try
{
ServiceReference6.Service1Client obj6 = new ServiceReference6.Service1Client();
DataTable table = Deserialize<DataTable>(obj6.SelectSavedProcessInformation());
if(table != null)
{
foreach(DataRow row in table.Rows)
{
// fill the listbox
}
}
The seirialization and deserialization code that I use is this:
public class JsonHelper
{
public static string JsonSerializer<T>(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<T>(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); }
}
}
I never used DataTable with serialization but I hope it works
Upvotes: 1