Click here to Skip to main content
12,632,072 members (24,659 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
i gine string value to dropdownlist ,then it gives me error.
error is:Object reference not set to an instance of an object.

my code is:

string name= ViewState["name"].ToString(); 
ddlSearchName.SelectedItem.Text = name;  //here it gives me error.
                        



name is not null ,In name variable value is :abc
Posted 12-Feb-13 23:42pm
Comments
srikanth pachava 13-Feb-13 4:47am
   
before setting selected item to dropdown that should be bounded

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

try like below.
string name= ViewState["name"].ToString(); 
object text = ddlSearchName.Items.FindByText(name);
if(text != null)
    ddlSearchName.Items.FindByText(name).Selected = true;


hope it helps.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100