vidya rao

vidya rao

  • NA
  • 3
  • 1.2k

how to display c# object in html?

Aug 14 2015 6:53 AM

Iam working on youtube api v3 to display all the video.

With the new youtube api v3 I got the data in json form then with Deserialize it is converted to c# object.

Now my next challenge is to display the data of the C# object in html page.

protected void Page_Load(object sender, EventArgs e) { string url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=3&playlistId=UUecxTX6PCkwvuP7U1a6EJJw&key=AIzaSyB8BFP0N1eRGGhSh4xYYyz43QTBSHhOdy4"; WebRequest req1 = WebRequest.Create(url.ToString()); WebResponse res1 = req1.GetResponse(); StreamReader rd1 = new StreamReader(res1.GetResponseStream(), Encoding.ASCII); string fileContents1;      using (StreamReader sr1 = rd1) {         fileContents1 = sr1.ReadToEnd(); } Console.WriteLine(fileContents1); VideoTitle video = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<VideoTitle>(fileContents1); Repeater Rpt = base.FindControl("Rpt") as Repeater; Rpt.DataSource = video; Rpt.DataBind(); }  protected void SetItem(object sender, RepeaterItemEventArgs e) { Literal title = e.Item.FindControl("Title") as Literal; VideoTitle video1 = e.Item.DataItem as VideoTitle;      title.Text= video1.items[0].snippet.title;  }  //json to c# object helper class public class PageInfo { public int totalResults { get; set; } public int resultsPerPage { get; set; } }  public class Default { public string url { get; set; } public int width { get; set; } public int height { get; set; } }  public class Medium { public string url { get; set; } public int width { get; set; } public int height { get; set; } }  public class High { public string url { get; set; } public int width { get; set; } public int height { get; set; } }  public class Standard { public string url { get; set; } public int width { get; set; } public int height { get; set; } }  public class Maxres { public string url { get; set; } public int width { get; set; } public int height { get; set; } }  public class Thumbnails { public Default @default { get; set; } public Medium medium { get; set; } public High high { get; set; } public Standard standard { get; set; } public Maxres maxres { get; set; } }  public class ResourceId { public string kind { get; set; } public string videoId { get; set; } }  public class Snippet { public string publishedAt { get; set; } public string channelId { get; set; } public string title { get; set; } public string description { get; set; } public Thumbnails thumbnails { get; set; } public string channelTitle { get; set; } public string playlistId { get; set; } public int position { get; set; } public ResourceId resourceId { get; set; } }  public class Item { public string kind { get; set; } public string etag { get; set; } public string id { get; set; } public Snippet snippet { get; set; } }  public class VideoTitle { public string kind { get; set; } public string etag { get; set; } public string nextPageToken { get; set; } public PageInfo pageInfo { get; set; } public List<Item> items { get; set; } }

The error I get is System.NullReferenceException at the below line. Rpt.DataSource = video;

While debugging if i mouseover on video it shows all the values that video has. Not sure whats wrong. Can anyone help me?

The html code is

<asp:Repeater runat="server" ID="Rpt" OnItemDataBound="SetItem"> <ItemTemplate> <article> <ul class="linkList"> <li> <asp:HyperLink runat="server" ID="Title" /> <br /> </li> </ul> </article> </ItemTemplate> </asp:Repeater>
For now just trying to display youtube video title.

Answers (4)