Reputation: 2129
I have written a fairly simple web service that places the result set of a SQL query into a list. I am having trouble calling that list with the result set in it in the project where I am using the web service. I will place my code below:
Web Service
[OperationContract]
List<ViewDetails> ViewDetails();
[DataContract]
public class ViewDetails
{
public string TitleView { get; set; }
public string BodyView { get; set; }
public string AuthorView { get; set; }
public ViewDetails() { }
public ViewDetails(string myTitleView, string myBodyView, string myAuthorView)
{
this.TitleView = myTitleView;
this.BodyView = myBodyView;
this.AuthorView = myAuthorView;
}
}
public List<ViewDetails> ViewDetails()
{
List<ViewDetails> details = new List<ViewDetails>();
SqlConnection conn = new SqlConnection(strConnString);
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 2 [My_Title] AS 'Title', [My_Body] AS 'Body', [My_Author] AS 'Author' FROM [My_table] ORDER BY [Date] DESC", conn);
SqlDataReader rdrDetails = cmd.ExecuteReader();
try
{
while (rdrDetails.Read())
{
details.Add(new ViewDetails(rdrDetails.GetSqlString(rdrDetails.GetOrdinal("Title")).ToString(), rdrDetails.GetSqlString(rdrDetails.GetOrdinal("Body")).ToString(), rdrDetails.GetSqlString(rdrDetails.GetOrdinal("Author")).ToString()));
}
}
catch (Exception e)
{
//exception
}
finally
{
conn.Close();
}
return details;
}
Project where I am using web service
public async void ViewData()
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
string title = string.Empty;
string body = string.Empty;
string author = string.Empty;
var res = await client.ViewDetailsAsync();
}
I expect to be able to do something like this in my ViewData
class so I can store the results in a variables and assign them to textblocks
, etc..
title = res.TitleView;
But it is not allowing me to.... Does anyone see something that I am missing?
NOTE: This is a Universal Windows Application
Upvotes: 0
Views: 66
Reputation: 6437
you are missing DataMember
attribute in your ViewDetails
data contract properties.
[DataContract]
public class ViewDetails
{
[DataMember]
public string TitleView { get; set; }
[DataMember]
public string BodyView { get; set; }
[DataMember]
public string AuthorView { get; set; }
public ViewDetails() { }
public ViewDetails(string myTitleView, string myBodyView, string myAuthorView)
{
this.TitleView = myTitleView;
this.BodyView = myBodyView;
this.AuthorView = myAuthorView;
}
}
Upvotes: 1
Reputation: 139
You have missing that res
is the List<ViewDetails>
. res
is not the instance of ViewDetails
. So you should type title = res[0].TitleView;
Upvotes: 1