private void BindData(bool newSearch) { try { if (newSearch) { SEARCH_FIELD sf = (SEARCH_FIELD)System.Enum.Parse(typeof(SEARCH_FIELD), listSearchWhere.SelectedValue); SEARCH_WHAT sw = (SEARCH_WHAT)System.Enum.Parse(typeof(SEARCH_WHAT), listSearchWath.SelectedValue); int forumID = int.Parse(listForum.SelectedValue); DataView dv = DB.GetSearchResult(txtSearchString.Text, sf, sw, forumID, PageUserID).DefaultView; Pager.CurrentPageIndex = 0; Pager.PageSize = int.Parse(listResInPage.SelectedValue); Pager.Count = dv.Count; Mession.SearchData = dv; bool bResults = (dv.Count > 0) ? true : false; SearchRes.Visible = bResults; NoResults.Visible = !bResults; } PagedDataSource pds = new PagedDataSource(); pds.AllowPaging = true; pds.DataSource = Mession.SearchData; pds.PageSize = Pager.PageSize; pds.CurrentPageIndex = Pager.CurrentPageIndex; SearchRes.DataSource = pds; DataBind(); } catch (System.Data.SqlClient.SqlException x) { Utils.LogToMail(x); if (IsAdmin) { AddLoadMessage(string.Format("{0}", x)); } else { AddLoadMessage("An error occurred in the database."); } } catch (Exception x) { Utils.LogToMail(x); if (IsAdmin) { AddLoadMessage(string.Format("{0}", x)); } else { AddLoadMessage("An error occured while searching."); } } }
/// <summary> /// Returns Search results /// </summary> /// <param name="ToSearch"></param> /// <param name="sf">Field to search</param> /// <param name="sw">Search what</param> /// <param name="fid"></param> /// <param name="UserID">ID of user</param> /// <returns>Results</returns> public static DataTable GetSearchResult( string ToSearch, SEARCH_FIELD sf, SEARCH_WHAT sw, int fid, int UserID ) { if ( ToSearch.Length == 0 ) return new DataTable(); if ( ToSearch == "*" ) ToSearch = ""; ToSearch = ToSearch.Replace( "'", "''" ); string sql = "select a.ForumID, a.TopicID, a.Topic, a.TopicMovedID, b.UserID, b.Name, c.MessageID, c.Posted, c.Message, c.Flags "; sql += "from yaf_topic a left join yaf_message c on a.TopicID = c.TopicID left join yaf_user b on c.UserID = b.UserID join yaf_vaccess x on x.ForumID=a.ForumID "; sql += String.Format( "where (a.Flags & 8) = 0 and x.ReadAccess<>0 and a.TopicMovedID IS NULL and x.UserID={0} ", UserID ); if ( sf == SEARCH_FIELD.sfUSER_NAME ) { sql += string.Format( "and b.Name like '%{0}%' ", ToSearch ); } else { string [] words; sql += "and ( "; switch ( sw ) { case SEARCH_WHAT.sfALL_WORDS: words = ToSearch.Split( ' ' ); foreach ( string word in words ) { sql += string.Format( "(c.Message like '%{0}%' or a.Topic like '%{0}%' ) and ", word ); } // remove last OR in sql query sql = sql.Substring( 0, sql.Length - 5 ); break; case SEARCH_WHAT.sfANY_WORDS: words = ToSearch.Split( ' ' ); foreach ( string word in words ) { sql += string.Format( "c.Message like '%{0}%' or a.Topic like '%{0}%' or ", word ); } // remove last OR in sql query sql = sql.Substring( 0, sql.Length - 4 ); break; case SEARCH_WHAT.sfEXACT: sql += string.Format( "c.Message like '%{0}%' or a.Topic like '%{0}%' ", ToSearch ); break; } sql += " ) "; } if ( fid >= 0 ) { sql += string.Format( "and a.ForumID = {0}", fid ); } sql += " order by c.Posted desc"; using ( SqlCommand cmd = new SqlCommand() ) { cmd.CommandType = CommandType.Text; cmd.CommandText = sql; return GetData( cmd ); } }