Exception in template (Designs\Ikasttelefonbog\Paragraph\AjaxProductSearchResults.cshtml): System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.bffdeadebee.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; } public string ParentGruppeID {get; set;} } }@{ int VIPBoundaryminorCurrency = 500000; // 1.000 kr ~ 400.000 ører IList<SearchResultItem> vips = new List<SearchResultItem>(); IList<SearchResultItem> results = new List<SearchResultItem>(); string search = System.Web.HttpContext.Current.Request.Params["searchString"]; string productId = System.Web.HttpContext.Current.Request.Params["productId"]; 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(); string sqlQuery; 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("grpRel.GroupRelationsParentID AS relParentGroupID" + ","); 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 "); query.Append("LEFT JOIN EcomGroupRelations AS grpRel on grpRel.GroupRelationsGroupID = grp.GroupID "); string[] searchFields = new string[] { "ProductCustomPhbTelefon", "ProductCustomPhbVejnavn", "ProductName", "GroupName", "ProductLongDescription", "ProductCustomPhbFornavn" }; // WHERE query.Append(" WHERE ProductDefaultShopID = 'SHOP1' AND ProductLanguageID = 'LANG1' AND ("); if(string.IsNullOrEmpty(productId)) { 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++; } } } else { query.Append("ProductID = ''+@pProductId+''"); command.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "pProductId", SqlDbType = System.Data.SqlDbType.NVarChar, Value = productId.Trim() }); } query.Append(")"); sqlQuery = query.ToString(); command.CommandText = sqlQuery; 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(), ParentGruppeID = reader["relParentGroupID"].ToString(), AnnoncePris = (int)(price*100) }; results.Add(item); } } } catch(Exception ex){ <text>Error: @ex.Message</text> } string[] searchProperties = new string[] { "Navn", "Fornavn", "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; } } if(searchValuesCount > 1 && results.Count(j => j.MatchCount >= searchValuesCount) > 0){ results = results.Where(l => l.MatchCount >= searchValues.Length).ToArray(); } vips = results.Where(j => j.AnnoncePris >= VIPBoundaryminorCurrency && j.OptagelsesType == "Annonce").ToList(); } <div id="ajaxHelper"> <div class="col-sm-4 hidden-xs ecomFirstColumn"> <div id="hideSearchResults" class="ssec">Skjul søgeresultater</div> <div class="fremhaev row"> @if(vips.Count > 0){ Random random = new Random(); var vip = vips[random.Next(0, vips.Count)]; if(!string.IsNullOrEmpty(vip.BilledSti)){ <a href="/Files/Images/Ikasttelefonbog/Annoncer/@(vip.BilledSti)" data-lightbox="fremhaev" title="@vip.Navn"> <img src="/Files/Images/Ikasttelefonbog/Annoncer/@(vip.BilledSti)" class="img-responsive" alt="@vip.Navn - logo" title="@vip.Navn"/> </a> } <text><br /><strong>@vip.Navn @vip.Fornavn</strong></text> <text><br />@vip.Vejnavn @vip.Husnr@vip.Husbogstav @vip.Etage @vip.Side</text> if(!string.IsNullOrEmpty(vip.Flaekke)){ <text><br />@vip.Flaekke</text> } <text><br />@vip.Postnr @vip.By</text> if(!string.IsNullOrEmpty(vip.Telefon)){ <text><br />@vip.Telefon</text> } if(!string.IsNullOrEmpty(vip.Email)){ <text><br /><a href="mailto:@vip.Email">@vip.Email</a></text> } if(!string.IsNullOrEmpty(vip.Web)){ string webString = vip.Web; string[] webs = webString.Split(','); if(webs.Length > 1){ foreach(string web in webs){ if(web.Contains("facebook")){ <text><br /><a target="_blank" href="http://@web"><img src="/Files/Images/System_Graphics/facebook.png" alt="Besøg vores Facebook side" title="Besøg vores Facebook side"/></a></text> } else { <text><br /><a target="_blank" href="http://@web">@web</a></text> } } } else { if(vip.Web.Contains("facebook")){ <text><br /><a target="_blank" href="http://@vip.Web"><img src="/Files/Images/System_Graphics/facebook.png" alt="Besøg vores Facebook side" title="Besøg vores Facebook side"/></a></text> } else { <text><br /><a target="_blank" href="http://@vip.Web">@vip.Web</a></text> } } } } </div> </div><!--/.ecomFirstColumn --> <div class="col-sm-8 col-xs-12 ecomSecondColumn"> <div class="row"> <h2 class="groupName">Søgeresultat (@results.Count)</h2> </div> <div class="row"> <div id="secondColumnResults" class="col-sm-12"> @{ string currentGroupId = string.Empty; } @foreach (SearchResultItem p in results.OrderBy(j => j.GruppeID).ThenBy(k => k.Navn)){ bool isFirma = p.ParentGruppeID == "PbFirma"; bool isGade = p.ParentGruppeID == "PbGade"; string optagelsesType = ""; string optClass = ""; string dbOptType = p.OptagelsesType; switch(dbOptType){ case "GratisOptagelse": optagelsesType = "0"; optClass = "gratis"; break; case "Fremhævelse": optagelsesType = "1"; optClass = "fremhaev"; break; case "Annonce": optagelsesType = "2"; optClass = "annonce"; break; default: break; } if(currentGroupId != p.GruppeID){ currentGroupId = p.GruppeID; <div class="row productRow @optClass"> <div class="productContent col-sm-12"> <div class="row productContentRow"> <div class="col-sm-6 categoryName"> <h2 class="groupName">@p.GruppeNavn</h2> </div> <div class="col-sm-6 categoryLink"> <a href="Default.aspx?ID=11&GroupID=@p.GruppeID">Vis alle i denne kategori</a> </div> </div> </div> </div> } <div class="row productRow @optClass" data-phone="@p.Telefon" data-name="@(p.Navn) @(p.Fornavn)" data-value="@(p.AnnoncePris)" data-opttype="@optagelsesType" data-address="@(p.Vejnavn) @(p.Husnr)@(p.Husbogstav) @(p.Etage) @(p.Side)" data-zip="@(p.Postnr)" data-city="@(p.By)" > <div class="productContent col-sm-10"> <div class="row productContentRow"> <div class="col-sm-6"> <div class="row"> <div class="col-sm-12 companyName"> @if (isGade) { <text>@p.Fornavn @p.Navn</text> } else { <text>@p.Navn<br />@p.Fornavn</text> } </div><!--/.col-sm-12 --> </div><!--/.row --> @if(optagelsesType != "0"){ <div class="row"> <div class="col-sm-12 companyAddress"> @p.Vejnavn @p.Husnr@p.Husbogstav @p.Etage @p.Side @if(!string.IsNullOrEmpty(p.Flaekke)){ <text><br />@p.Flaekke</text> } </div><!--/.col-sm-12 --> </div><!--/.row --> <div class="row"> <div class="col-sm-12 companyCity"> @p.Postnr @p.By </div><!--/.col-sm-12 --> </div><!--/.row --> if(!string.IsNullOrEmpty(p.Beskrivelse)){ <div class="row"> <div class="col-sm-12 companyDescriptionLink" data-id="@(p.ID)"> Mere information </div><!--/.col-sm-12 --> </div><!--/.row --> } } </div><!--/.col-sm-6 --> <div class="col-sm-6"> <div class="row"> <div class="col-sm-12 companyPhone"> <a href="tel:+45@(p.Telefon)">@p.Telefon</a> </div><!--/.col-sm-12 --> </div><!--/.row --> @if(optagelsesType != "0") { if(!string.IsNullOrEmpty(p.Email)){ <div class="row"> <div class="col-sm-12 companyMail"> <a href="mailto:@p.Email">@p.Email</a> </div><!--/.col-sm-12 --> </div><!--/.row --> } if(!string.IsNullOrEmpty(p.Web)){ string webString = p.Web; string[] webs = webString.Split(','); int webCounter = 0; <div class="row"> <div class="col-sm-12 companyWeb"> @if(webs.Length > 1){ foreach(string web in webs){ if(web.Contains("facebook")){ <a target="_blank" href="http://@web"><img src="/Files/Images/System_Graphics/facebook.png" alt="Besøg vores Facebook side" title="Besøg vores Facebook side"/></a> } else { <a target="_blank" href="http://@web">@web</a> } if(webs.Length -1 > webCounter){ <text><br /></text> } webCounter++; } } else { if(p.Web.Contains("facebook")){ <a target="_blank" href="http://@p.Web"><img src="/Files/Images/System_Graphics/facebook.png" alt="Besøg vores Facebook side" title="Besøg vores Facebook side"/></a> } else { <a target="_blank" href="http://@p.Web">@p.Web</a> } } </div><!--/.col-sm-12 --> </div><!--/.row --> } } </div><!--/.col-sm-6 --> </div><!--/.productContentRow --> </div><!--/.productContent --> <div class="productBrochure col-sm-2 hidden-xs"> @if(optagelsesType != "0" && !string.IsNullOrEmpty(p.BilledSti)){ <a href="/Files/Images/Ikasttelefonbog/Annoncer/@(p.BilledSti)" data-lightbox="@p.ID" title="@p.Navn"> <img src="/Files/Images/Ikasttelefonbog/Annoncer/@(p.BilledSti.Replace(".", "_thumb."))" class="img-responsive" alt="@p.Navn - logo" title="@p.Navn"/> </a> } </div><!--/.productBrochure --> @if(isFirma && optagelsesType != "0" && optagelsesType != ""){ <div class="row"> <div class="col-xs-12 companyGoogleMap "> <div class="row"> <div class="col-xs-12 googleMapLink"><a href="javascript:void(0);">Se på Google Maps</a></div> <div class="col-xs-12 googleMapFrame"></div> </div> </div> </div> } @if(isGade){ <div class="row"> <div class="col-xs-12 streetGoogleMap "> <div class="row"> <div class="col-xs-12 googleMapLink"><a href="javascript:void(0);">Se på Google Maps</a></div> <div class="col-xs-12 googleMapFrame"></div> </div> </div> </div> } </div><!--/.productRow --> if(!string.IsNullOrEmpty(p.Beskrivelse)){ <div class="row productRow productContentDescriptionRow companyDescription" data-id="@(p.ID)"> <div class="col-sm-12"> @p.Beskrivelse </div><!--/..col-sm-12--> </div><!--/.productContentDescriptionRow--> } } </div><!--/#secondColumnResults --> </div><!--/.row --> </div><!--/.ecomSecondColumn --> </div><!--/#ajaxHelper --> }