Listing 22.9 contains the code that is behind the search page. Users can type search criteria with the page shown in Figure 22.5, and matching results will be shown. Listing 22.9 This Code Is behind the Search.aspx Page.private void Main_Click(object sender, System.EventArgs e) { Response.Redirect( "Default.aspx" ); } private void PerformSearch_Click(object sender, System.EventArgs e) { // Create a connection object. SqlConnection objConnection = new SqlConnection( ConfigurationSettings. AppSettings["ConnectString"] ); try { // Open the connection. objConnection.Open(); string strSql = "Select * From FileInfo where"; bool bNeedToUseAND = false; if( SearchGroupNames.Checked ) { bNeedToUseAND = true; strSql += " GroupID in (select ID from FileGroup where Name " + "like '%" + Criteria.Text + "%')"; } if( SearchTitles.Checked ) { if( bNeedToUseAND ) { strSql += " and "; } bNeedToUseAND = true; strSql += "(Title like '%" + Criteria.Text + "%')"; } if( SearchDescriptions.Checked ) { if( bNeedToUseAND ) { strSql += " and "; } bNeedToUseAND = true; strSql += "(Description like '%" + Criteria.Text + "%')"; } if( SearchFilenames.Checked ) { if( bNeedToUseAND ) { strSql += " and "; } bNeedToUseAND = true; strSql += "(Filename like '%" + Criteria.Text + "%')"; } if( SearchGroupNames.Checked ) { strSql += " group by GroupID"; } strSql += " order by title"; // Create a command object. SqlCommand objCommand = new SqlCommand( strSql, objConnection ); SqlDataReader objReader = objCommand.ExecuteReader(); FileList.DataSource = objReader; FileList.DataBind(); objReader.Close(); } catch( Exception ex ) { // Alert the user to the error. ErrorMessage.Text = ex.Message.ToString(); } finally { Criteria.Text = ""; // Close the connection. if( objConnection.State == ConnectionState.Open ) { objConnection.Close(); } } } Figure 22.5. The Search Page Allows Users to Find the Files They Want. |