Exception in template (Designs\Ikasttelefonbog\Paragraph\SearchSuggestions.cshtml): System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.bdeacbcde.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()
@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; } } }@{ IList<SearchResultItem> results = new List<SearchResultItem>(); string search = System.Web.HttpContext.Current.Request.Params["searchString"]; string[] searchValues = search.Split(new char[]{',',' '}); searchValues = searchValues.Where(j => j.Trim().Length > 2 || (j.Trim().Length == 2 && (j.Trim() == "3f" || j.Trim() == "3F") ) ).Select(k => k.Trim()).ToArray(); using (var command = Dynamicweb.Database.CreateConnection().CreateCommand()) { System.Text.StringBuilder query = new System.Text.StringBuilder(); query.Append("SELECT "); // Fields string prefix = "ProductCustomPhb"; string productPrefix = "Product"; // query.Append(prefix + "OptagelsesType AS OptagelsesType" + ","); query.Append(prefix + "Fornavn AS Fornavn" + ","); query.Append(prefix + "Vejnavn AS Vejnavn" + ","); // query.Append(prefix + "Husnr AS Husnr" + ","); // query.Append(prefix + "Husbogstav AS Husbogstav" + ","); // query.Append(prefix + "Etage AS Etage" + ","); // query.Append(prefix + "Side AS Side" + ","); // query.Append(prefix + "Flaekke AS Flaekke" + ","); // query.Append(prefix + "Postnr AS Postnr" + ","); // query.Append(prefix + "By AS city" + ","); query.Append(prefix + "Telefon AS Telefon" + ","); // query.Append(prefix + "Web AS Web" + ","); // query.Append(prefix + "Email AS Email" + ","); // query.Append(prefix + "BilledSti AS BilledSti" + ","); query.Append(productPrefix + "Name" + ","); query.Append(productPrefix + "ID" + ","); query.Append(productPrefix + "LongDescription" + ","); // query.Append(productPrefix + "Price" + ","); query.Append("grp.GroupName" + ","); query.Append("grp.GroupID" + ""); // FROM query.Append(" FROM EcomProducts "); query.Append(" INNER JOIN EcomGroupProductRelation AS gpr ON EcomProducts.ProductID = gpr.GroupProductRelationProductID "); query.Append(" INNER JOIN EcomGroups AS grp ON gpr.GroupProductRelationGroupID = grp.GroupID "); string[] searchFields = new string[] { "ProductCustomPhbTelefon", "ProductCustomPhbVejnavn", "ProductName", "GroupName", "ProductLongDescription", "ProductCustomPhbFornavn" }; // WHERE query.Append(" WHERE ProductDefaultShopID = 'SHOP1' AND ProductLanguageID = 'LANG1' AND ("); bool isStart = true; foreach(string searchField in searchFields){ int valueCount = 1; foreach(string searchValue in searchValues){ if(isStart){ isStart = false; query.Append("" + searchField + " LIKE '%'+" + "@p" + searchField + "" + valueCount + "+'%' "); } else { query.Append("OR " + searchField + " LIKE '%'+" + "@p" + searchField + "" + valueCount + "+'%' "); } command.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "p" + searchField + "" + valueCount, SqlDbType = System.Data.SqlDbType.NVarChar, Value = searchValue.Trim() }); valueCount++; } } query.Append(")"); command.CommandText = query.ToString(); try{ using (var reader = command.ExecuteReader()) { while (reader.Read()) { //float price = 0; // float.TryParse(reader["ProductPrice"].ToString() ?? "0", out price); var item = new SearchResultItem(){ // OptagelsesType = reader["OptagelsesType"].ToString(), ID = reader["ProductID"].ToString(), Navn = reader["ProductName"].ToString(), Fornavn = reader["Fornavn"].ToString(), Vejnavn = reader["Vejnavn"].ToString(), //Husnr = reader["Husnr"].ToString(), //Husbogstav = reader["Husbogstav"].ToString(), //Etage = reader["Etage"].ToString(), //Side = reader["Side"].ToString(), //Flaekke = reader["Flaekke"].ToString(), //Postnr = reader["Postnr"].ToString(), //By = reader["City"].ToString(), Telefon = reader["Telefon"].ToString(), //Web = reader["Web"].ToString(), //BilledSti = reader["BilledSti"].ToString(), //Email = reader["Email"].ToString(), Beskrivelse = reader["ProductLongDescription"].ToString(), GruppeNavn = reader["GroupName"].ToString(), GruppeID = reader["GroupID"].ToString(), //AnnoncePris = (int)(price*100), Relevance = 0 }; results.Add(item); } } } catch(Exception ex){ <text>Error: @ex.Message</text> } } string[] searchProperties = new string[] { "Navn", "Vejnavn", "Telefon", "GruppeNavn", "Beskrivelse" }; int searchValuesCount = searchValues.Length; foreach (SearchResultItem p in results) { int propertyMatch = 0; bool isVip = false; HashSet<string> searchValueMatch = new HashSet<string>(); foreach(string property in searchProperties){ foreach(string searchValue in searchValues){ int index = -1; int relevanceValue = 0; switch(property){ case "Navn": index = p.Navn.IndexOf(searchValue, StringComparison.OrdinalIgnoreCase); relevanceValue = 10; break; case "Fornavn": index = p.Fornavn.IndexOf(searchValue, StringComparison.OrdinalIgnoreCase); relevanceValue = 10; break; case "Vejnavn": index = p.Vejnavn.IndexOf(searchValue, StringComparison.OrdinalIgnoreCase); relevanceValue = 7; break; case "Telefon": index = p.Telefon.IndexOf(searchValue, StringComparison.OrdinalIgnoreCase); relevanceValue = 5; break; case "GruppeNavn": index = p.GruppeNavn.IndexOf(searchValue, StringComparison.OrdinalIgnoreCase); relevanceValue = 3; break; case "Beskrivelse": index = p.Beskrivelse.IndexOf(searchValue, StringComparison.OrdinalIgnoreCase); relevanceValue = 3; break; default: break; } if(index > -1){ searchValueMatch.Add(searchValue); propertyMatch++; if(index == 0){ p.Relevance += relevanceValue; } else { p.Relevance += (relevanceValue-2); } } } } if(searchValuesCount > 1 && searchValueMatch.Count == searchValuesCount){ p.MatchCount = propertyMatch; } } var seralizer = new System.Web.Script.Serialization.JavaScriptSerializer(); var result = string.Empty; if(searchValuesCount > 1 && results.Count(j => j.MatchCount >= searchValuesCount) > 0){ result = seralizer.Serialize(results.Where(l => l.MatchCount >= searchValues.Length).OrderByDescending(j => j.Relevance).Take(10).Select( k => new { name = (k.Navn + (!string.IsNullOrEmpty(k.Fornavn) ? " - "+k.Fornavn : "") + " - "+k.GruppeNavn), id = k.ID}).ToArray()); } else { result = seralizer.Serialize(results.OrderByDescending(j => j.Relevance).Take(10).Select( k => new { name = (k.Navn + (!string.IsNullOrEmpty(k.Fornavn) ? " - "+k.Fornavn : "") + " - "+k.GruppeNavn), id = k.ID }).ToArray()); } var response = System.Web.HttpContext.Current.Response; response.Clear(); response.AddHeader("Content-type", "text/json"); response.AddHeader("Content-type", "application/json"); response.ContentType = "application/json"; response.Write(result.Trim()); response.End(); }