Exception in template (Designs\Ikasttelefonbog\Paragraph\SearchSuggestions2.cshtml): System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.bfffabebff.Execute()
   at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context)
   at RazorEngine.Templating.TemplateService.Run(ITemplate template, DynamicViewBag viewBag)
   at RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName)
   at RazorEngine.Razor.Parse[T](String razorTemplate, T model, String cacheName)
   at Dynamicweb.Rendering.Template.Output()
@using System.Data.SqlClient @using System.Web @functions { public class SearchResultItem { public string OptagelsesType { get; set; } public string Navn { get; set; } public string ID { get; set; } public string Fornavn { get; set; } public string Efternavn { get; set; } public string Vejnavn { get; set; } public string Husnr { get; set; } public string Husbogstav { get; set; } public string Etage { get; set; } public string Side { get; set; } public string Flaekke { get; set; } public string Postnr { get; set; } public string By { get; set; } public string Telefon { get; set; } public string Web { get; set; } public string BilledSti { get; set; } public string Email { get; set; } public string Beskrivelse { get; set; } public string GruppeNavn { get; set; } public string GruppeID { get; set; } public int AnnoncePris { get; set; } public int Relevance { get; set; } public int MatchCount { get; set; } } } @{ //http://www.silkeborginfo.dk/Default.aspx?ID=2672&q=gje var Request = HttpContext.Current.Request; var Response = HttpContext.Current.Response; var listQuery = (Request["q"] ?? Request["searchString"]).Split(',',' ') .Where(o => { var x = o.Trim(); if (x.Length > 2) return true; if (string.Equals(x, "3f", StringComparison.OrdinalIgnoreCase)) return true; return false; }) .Select(o => o.Trim()) .ToList(); var listQueryCount = listQuery.Count; var searchFields = new[] { "ProductCustomPhbTelefon", "ProductCustomPhbVejnavn", "ProductName", "GroupName", "ProductLongDescription", "ProductCustomPhbFornavn" }; var listSqlOr = new List<string>(); foreach (var field in searchFields) { for (var i = 0; i < listQueryCount; i++) { listSqlOr.Add(field + " LIKE '%' + @p_" + field + "_" + i + " + '%'"); } } var sql = "SELECT " + "ProductCustomPhbFornavn, " + "ProductCustomPhbVejnavn, " + "ProductCustomPhbTelefon, " + "ProductName, " + "ProductID, " + "ProductLongDescription, " + "g.GroupName, " + "g.GroupID " + "FROM EcomProducts p " + "INNER JOIN EcomGroupProductRelation gpr ON p.ProductID = gpr.GroupProductRelationProductID " + "INNER JOIN EcomGroups g ON gpr.GroupProductRelationGroupID = g.GroupID " + "WHERE ProductDefaultShopID = 'SHOP1' " + "AND ProductLanguageID = 'LANG1' " + "AND (" + (listSqlOr.Any() ? string.Join(" OR ", listSqlOr) : "0 = 1") + ")"; var results = new List<SearchResultItem>(); using (var command = Dynamicweb.Database.CreateConnection().CreateCommand()) { command.CommandText = sql; foreach (var field in searchFields) { for (var i = 0; i < listQueryCount; i++) { command.Parameters.Add(new SqlParameter("@p_" + field + "_" + i, listQuery[i])); } } using (var reader = command.ExecuteReader()) { while (reader.Read()) { results.Add(new SearchResultItem { Fornavn = reader.IsDBNull(0) ? null : reader.GetString(0), Vejnavn = reader.IsDBNull(1) ? null : reader.GetString(1), Telefon = reader.IsDBNull(2) ? null : reader.GetString(2), Navn = reader.IsDBNull(3) ? null : reader.GetString(3), ID = reader.IsDBNull(4) ? null : reader.GetString(4), Beskrivelse = reader.IsDBNull(5) ? null : reader.GetString(5), GruppeNavn = reader.IsDBNull(6) ? null : reader.GetString(6), GruppeID = reader.IsDBNull(7) ? null : reader.GetString(7) }); } } } var searchProps = new[] { "Navn", "Vejnavn", "Telefon", "GruppeNavn", "Beskrivelse" }; foreach (var item in results) { var matchCount = 0; var matchHash = new HashSet<string>(); foreach (var searchProp in searchProps) { foreach (var query in listQuery) { var index = -1; var relevanceValue = 0; switch (searchProp) { case "Navn": index = item.Navn == null ? index : item.Navn.IndexOf(query, StringComparison.OrdinalIgnoreCase); relevanceValue = 10; break; case "Fornavn": index = item.Fornavn == null ? index : item.Fornavn.IndexOf(query, StringComparison.OrdinalIgnoreCase); relevanceValue = 10; break; case "Vejnavn": index = item.Vejnavn == null ? index : item.Vejnavn.IndexOf(query, StringComparison.OrdinalIgnoreCase); relevanceValue = 7; break; case "Telefon": index = item.Telefon == null ? index : item.Telefon.IndexOf(query, StringComparison.OrdinalIgnoreCase); relevanceValue = 5; break; case "GruppeNavn": index = item.GruppeNavn == null ? index : item.GruppeNavn.IndexOf(query, StringComparison.OrdinalIgnoreCase); relevanceValue = 3; break; case "Beskrivelse": index = item.Beskrivelse == null ? index : item.Beskrivelse.IndexOf(query, StringComparison.OrdinalIgnoreCase); relevanceValue = 3; break; } if (index != -1) { matchCount++; matchHash.Add(query); if (index == 0) { item.Relevance += relevanceValue; } else { item.Relevance += (relevanceValue - 2); } } } } if (listQueryCount > 1 && matchHash.Count == listQueryCount) { item.MatchCount = matchCount; } } object result; if (listQueryCount > 1 && results.Any(o => o.MatchCount >= listQueryCount)) { result = results .Where(o => o.MatchCount >= listQueryCount) .OrderByDescending(o => o.Relevance) .Take(10) .Select(o => new { name = o.Navn + (!string.IsNullOrEmpty(o.Fornavn) ? " - " + o.Fornavn : "") + " - " + o.GruppeNavn, id = o.ID }) .ToList(); } else { result = results .OrderByDescending(o => o.Relevance) .Take(10) .Select(o => new { name = o.Navn + (!string.IsNullOrEmpty(o.Fornavn) ? " - " + o.Fornavn : "") + " - " + o.GruppeNavn, id = o.ID }) .ToList(); } var callback = Request["callback"]; var indented = Request["jsonIndented"] == "1"; var json = Newtonsoft.Json.JsonConvert.SerializeObject(result, indented ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None); Response.Clear(); if (!string.IsNullOrEmpty(callback)) { Response.ContentType = "application/javascript"; Response.Write(callback + "(" + json + ")"); } else { Response.ContentType = "application/json"; Response.Write(json); } Response.End(); }