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();
      }

 }
lenmorAsked:
Who is Participating?
 
dunglaCommented:
You can do something like that:

ListItem li = ddlSearch.SelectedItem;
                        if (li == null)
                        {
                              Response.Write("Null value");
                        }
                        else
                        {
                              switch (li.Value)
                              {
                                    case "ALL":
                                          // sql here
                                          break;
                                    case "OTHER CASE":
                                          // other sql here
                                          break;
                                    case "9999":
                                          // yes another sql here
                                          break;
                              }
                        }
0
 
jj819430Commented:
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.
0
 
lenmorAuthor Commented:
what if all the other cases has the same sql?
0
 
lenmorAuthor Commented:
default:

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.