Hi,
I am trying to get the value through WCF service parameter
Before this i have tested by below sql query
select MenuTitle from dbname where ParentId='1'
Now I need the same result through below WCF service url
http://hostname:portno/_vti_bin/PresidentsService.svc/GetPresidentById/1
But I am getting blank page,please let me know if I am missing anything here.
//IPresidentsService.cs
[ServiceContract]
interface IPresidentsService
{
[OperationContract]
[WebGet(UriTemplate = "GetPresidentById/{id}",
ResponseFormat = WebMessageFormat.Json)]
President GetPresidentById(string id);
}
//President.cs
public class President
{
[DataMember]
public string Id { get; set; }
}
//PresidentsService.svc.cs
public class PresidentsService : Barkes.Services.Presidents.ISAPI.IPresidentsService
{
private List<President> Presidents
{
get
{
// If there aren't any presidents in our list, populate with samples
_presidents = _presidents ?? new List<President>(SampleData.SamplePresidents);
return _presidents;
}
}
public President GetPresidentById(string id)
{
var query = from President p in Presidents
where p.Id == id
select p;
return query.FirstOrDefault();
}
}
//PresidentsData.cs
public static class SampleData
{
public static President[] SamplePresidents = new President[]
{
new President{ Id=GetLeftMenuParentId("1")}
};
private static string GetLeftMenuParentId(string parentid)
{
SqlConnection con = new SqlConnection("Data Source=dbservername;Initial Catalog=dbname;User ID=userid;Password=password");
con.Open();
SqlCommand cmd = new SqlCommand("select MenuTitle from dbname where ParentId="+parentid, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
DataTable dt = ds.Tables[0];
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
Regards,
Sudheer
Thanks & Regards, Sudheer