Guru Prakash

Guru Prakash

  • NA
  • 5
  • 564

How to recover from the error?

Jan 4 2017 5:28 AM
I am retrieving data from database and bind to a gridview & listview controls and here i need to display each "tags"(i.e, comma separated values) as hyperlinks.
  1. string tags = string.Empty;  
  2. using (DataClass1DataContext DcContext = new DataClass1DataContext())  
  3. {  
  4. GridView1(or)ListView1.DataSource = DcContext.tblArticles  
  5. .Join(DcContext.tblCategories,  
  6. x => x.category,  
  7. y => y.categoryId,  
  8. (x, y) => new { x, y })  
  9. .Join(DcContext.tblSubCategories,  
  10. p => p.x.subcategory,  
  11. q => q.subcategoryId,  
  12. (p, q) => new  
  13. {  
  14. tags = p.x.keywords,  
  15. Article = p.x.articleId,  
  16. Published = p.x.articleDate,  
  17. Title = p.x.title,  
  18. Summary = p.x.description,  
  19. Labels = p.x.keywords,  
  20. Poster = p.x.image,  
  21. Category = p.y.Category,  
  22. Subcategory = q.subcategory,  
  23. });  
What I need is?
The tags which i retrieved will need to display as related keywords in the form of hyperlinks and bind to the gridview/Listview.
 
  1. <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false">  
  2. <Columns>  
  3. <asp:TemplateField>  
  4. <ItemTemplate>  
  5. <asp:Image ID="PosterImg" runat="server" ImageUrl='<%#Eval("Poster") %>' Width="50" Height="50" />  
  6. </ItemTemplate>  
  7. </asp:TemplateField>  
  8. <asp:BoundField DataField="Published" HeaderText="Published on" />  
  9. <asp:BoundField DataField="Title" HeaderText="Article" />  
  10. <asp:BoundField DataField="Summary" HeaderText="Summary about your articles" />  
  11. <asp:TemplateField>  
  12. <ItemTemplate>  
  13. <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
  14. </ItemTemplate>  
  15. </asp:TemplateField>  
  16. <asp:BoundField DataField="Category" HeaderText="Category" />  
  17. <asp:BoundField DataField="Subcategory" HeaderText="Subcategory" />  
  18. </Columns>  
  19. </asp:GridView>  
  20. <asp:ListView ID="ListView1" runat="server">  
  21. <ItemTemplate>  
  22. <asp:Image ID="PosterImg" CssClass="thumb" runat="server" ImageUrl='<%#Eval("Poster") %>' Width="120" Height="150" />  
  23. <h4>  
  24. <asp:HyperLink ID="HyperLink2" NavigateUrl='<%#"?p="+Eval("Article") %>' runat="server"><%#Eval("Title") %></asp:HyperLink>  
  25. </h4>  
  26. <cite>  
  27. Posted by  
  28. <asp:HyperLink ID="HyperLink3" NavigateUrl='<%#"?user="+Eval("Article") %>' runat="server">Username</asp:HyperLink>  
  29. under  
  30. <span>  
  31. <asp:HyperLink ID="HyperLink4" NavigateUrl='<%#"?subcategory="+Eval("Subcategory") %>' runat="server"><%#Eval("Subcategory") %></asp:HyperLink>  
  32. ·  
  33. <asp:HyperLink ID="HyperLink5" NavigateUrl='<%#"?category="+Eval("Category") %>' runat="server"><%#Eval("Category") %></asp:HyperLink>  
  34. </span>  
  35. on  
  36. <time><%#Eval("Published") %></time>  
  37. </cite><br />  
  38. <p>  
  39. <%#Eval("Summary") %>  
  40. </p>  
  41. <span>  
  42. <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
  43. </span>  
  44. </ItemTemplate>  
  45. </asp:ListView>  
 
What I have tried:

Added a placeholder inside the item template of each control(i.e, GRIDVIEW and LISTVIEW)
and by using LINQ I retrieved it from database and assign it to local string variable.
 
  1. string[] strArray = tags.Split(',');  
  2. foreach (var obj in strArray)  
  3. {  
  4. HyperLink TagLinks = new HyperLink();  
  5. TagLinks.NavigateUrl = "?tags=" + obj;  
  6. TagLinks.Text = obj;  
  7. PlaceHolder PlaceHolder1 = (PlaceHolder)GridView1.FindControl("PlaceHolder1");  
  8. PlaceHolder1.Controls.Add(TagLinks);  
  9. }  
I received an error:

Object reference not set to an instance of an object.

How to resolve this? 
 
 
 
 

Attachment: default.rar

Answers (2)