Click here to Skip to main content
15,662,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have exp, functional area, location and search button in two pages home ,searchpage .

now suppose i select one location(Anand) from home page and it give search result in search page and also display location(Anand) in textbox of location.

my code for that is in search page that for display location

txtCountry.Text = Request.QueryString["loc"].ToString();

It works completely,

but when i select location , experience both my code

txtCountry.Text = Request.QueryString["loc"].ToString();

ddyear.SelectedItem.Text = Request.QueryString["exp"].ToString();

it gives object refference error.

so how can i use condition wise,

my condition code

if (txtCountry.Text != "")
              txtCountry.Text = Request.QueryString["loc"].ToString();
          if (ddfarea.SelectedIndex > 0)
              ddfarea.SelectedItem.Text = Request.QueryString["fun"].ToString();
          if (ddyear.SelectedIndex > 0)
              ddyear.SelectedItem.Text = Request.QueryString["exp"].ToString();

but it is not working.this code also even not give any error but not given any result.

1 solution

ddyear.SelectedItem.Text = Request.QueryString["exp"].ToString(); gives you a null reference exception because there are no selected item (yet)

You willhave to loop through your select list options to find the option with the right value, and then set it as selected.
foreach(var item in ddYear.Items){
   if(item.Value == Request.QueryString["exp"].ToString())
      item.Selected = true;
Share this answer
aarohi verma 23-Jul-12 7:12am    
item.value gives error at value.
StianSandberg 23-Jul-12 7:23am    
are you using an ASP.NET DropDownList?
StianSandberg 23-Jul-12 7:25am    
It should be Value (big V)
If you loop through all your items in a dropdownlist you can access each ListItem. One of the ListItem properties is Value (System.String)
Espen Harlinn 24-Jul-12 4:44am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900