[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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

 }
0
lenmor
Asked:
lenmor
  • 2
1 Solution
 
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
 
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
 
lenmorAuthor Commented:
what if all the other cases has the same sql?
0
 
lenmorAuthor Commented:
default:

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now