Hi,
I have two DropdownLists. One is for Category and another is for Product. The concept is that with each Category and Product selected, the website will display the result on another page and show all the results present in the database.
With more than 25 Categories with me and approx 370 products in each category, I am putting conditional statement for each Category and Product so that the results should be taken from the database. This makes approx 9250 conditional statements for searching of product in database
I believe this kind of searching where I am using so many lines of codes and conditional statements makes my program extremely slow and inefficient.
May be I am not using the right approach for the searching.
Can anyone please tell me in this matter and let me know a technique where I can optimize my code? Any help will be appreciated. I am putting the sample code below:
The page with DropDownList has the code as below (I am putting a few lines):
If Category.SelectedItem.Value = "Plastic" And ProductList.SelectedItem.Value = "Bottles" Then
'Redirect to the search result page
Response.Redirect("Search Page.aspx?Category=Plastic&Product=Bottles")
ElseIf Category.SelectedItem.Value = "Plastic" And ProductList.SelectedItem.Value = "Calculators" Then
Response.Redirect("Search Page.aspx?Category=Plastic&Product=Calculators")
ElseIf Category.SelectedItem.Value = "Plastic" And ProductList.SelectedItem.Value = "Dryer" Then
Response.Redirect("Search Page.aspx?Category=Plastic&Product=Dryer")
The result page where the results are shown in the GridView is given below:
If Category = "Plastic" And Product = "Bottles" Then
Dim connectionstring As String = WebConfigurationManager.ConnectionStrings("Product_Details").ConnectionString
Dim con As New SqlConnection(connectionstring)
Dim selectsql As String = "SELECT Product_Name,Size,Price FROM Prod_Det WHERE Products='Bottles' ORDER BY Prod_Det"
Dim cmd As New SqlCommand(selectsql, con)
Dim adapter As New SqlDataAdapter(cmd)
'Fill the dataset
Dim ds As New DataSet()
adapter.Fill(ds, "Prod_Det")
'Perform Binding
SearchResultGrid.DataSource = ds
SearchResultGrid.DataBind()
ElseIf Category = "Plastic" And Product = "Calculator" Then
Dim connectionstring As String = WebConfigurationManager.ConnectionStrings("Product_Details").ConnectionString
Dim con As New SqlConnection(connectionstring)
Dim selectsql As String = "SELECT Product_Name,Size,Price FROM Prod_Det WHERE Products='Calculator' ORDER BY Product_Name"
Dim cmd As New SqlCommand(selectsql, con)
Dim adapter As New SqlDataAdapter(cmd)
'Fill the dataset
Dim ds As New DataSet()
adapter.Fill(ds, "Prod_Det")
'Perform Binding
SearchResultGrid.DataSource = ds
SearchResultGrid.DataBind()
The code given above is just a sample. Kindly provide me the suggestion about how to optimize this code.
Regards,
Bilal A. Khan