Link to home
Start Free TrialLog in
Avatar of lenmor
lenmor

asked on

Search via dropdown

I want to check on DropDown.SelectedItem.Value.ToString() ,

IF DropDown.SelectedItem.Value.ToString() = 9999
do that SQL
ELSE
do another SQL

This is what I have so far. This is a search engine, I want to be able to choose "All" from the dropdown and only search excluding category search.

Anyone?

void btnSok_Click(Object Source, EventArgs E)
 {
 



     if (DropDown.SelectedIndex !=-1 )
     {
     string sqlText = "SELECT INTWebUser.Firmanavn, INTWebUser.UserApproved, INTWebUser.Adresse, INTWebUser.Telefon, INTWebUser.Bodytext, INTWebUser.Epost, INTWebUser.Logo, INTWebUser.Hjemmeside, INTWebUser.Bilde1, INTWebUser.Bilde2, INTWebUser.Bilde3, b1.triggerid, b2.triggerid, b3.triggerid, b4.triggerid, b1.filename,b2.filename, b3.filename, b4.filename, b1.sourceid, b2.sourceid, b3.sourceid, b4.sourceid, b1.width, b2.width, b3.width, b4.width,b1.height, b2.height, b3.height, b4.height FROM INTWebUser, INTFileObject b1, INTFileObject b2, INTFileObject b3, INTFileObject b4 Where (b1.SourceId = INTWebUser.Bilde1 AND b1.TriggerId = 281347) AND (b2.SourceId = INTWebUser.Bilde2 AND b2.TriggerId = 281347) AND (b3.SourceId = INTWebUser.Bilde3 AND b3.TriggerId = 281347) AND (b4.SourceId = INTWebUser.Logo AND b4.TriggerId = 281347) AND (Firmanavn LIKE '%"
     + txtSok.Text.Trim()  +"%' OR Bodytext LIKE '%" + txtSok.Text.Trim()  + "%' OR Adresse LIKE '%"  + txtSok.Text.Trim()  + "%' OR Telefon LIKE '%"  + txtSok.Text.Trim()  + "%') AND Kategori= " + DropDown.SelectedItem.Value.ToString() + " AND INTWebUser.Approved = 1 AND INTWebUser.UserGroup = 288921" ;

      dlAnnonse.DataSource = GetDr( sqlText );
      dlAnnonse.DataBind();
      }

 }
Avatar of jj819430
jj819430

I am not 100% sure what you are asking.

first comment
use a switch statement

switch(Convert.ToInt32(DropDown.SelectedItem.Value.ToString()))
{
  case(-1):
  {
   break;
  }
  ....
  default:
  {
  }
}

Next peice, if you can, break out your Data Access from your bussiness logic. This will save you a ton of time on maintenance.
All of these likes are also going to kill your performance. You should look into the CONTAINS keywork. It is not soo much faster, but it is built for this stuff.

Now if they select "ALL" just remove the AND Kategori section of the select statement.

You can actualy use a command in SQL (I forget it right know) that is something like "is in" {Comma seperated list of items}. If you put the items you want to search into that string and then use that command you will be able to do exclusions etc.
ASKER CERTIFIED SOLUTION
Avatar of dungla
dungla
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lenmor

ASKER

what if all the other cases has the same sql?
Avatar of lenmor

ASKER

default: