Check if item is in data

I need to check if an item is in my datasource on post back.  I am using C# with asp.net.  What I have is a cascading dropdown, where the first dropdown's selected item(projects) determines the second dropdown(users).  I would like to store the user and keep it "selected" if they exist in another project.  

My psuedo code looks like this, but I'm not sure about syntax:
On Postback
string selectedusername = dropdown2.selecteditem;

if (selectedusername is in datasource items)
{
    dropdown1.selecteditem = selectedusername
}
else

thanks in advance
delousedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GreymanMSCCommented:

String selectedusername = dropdown2.SelectedValue;
if (dropdown1.Items.FindByValue(selectedusername))
{
    dropdown1.SelectedValue = selectedusername
}
else
{
    'do some other stuff
}

Open in new window

0
delousedAuthor Commented:
I'm getting the error:
Cannot implicitly convert type 'System.Web.UI.WebControls.ListItem' to 'bool'

Why is it thinking it's a bool? Here's my code:
String PreviouslySelectedUser = UserList.SelectedValue;
 
      if (UserList.Items.FindByValue(PreviouslySelectedUser))
      {
          UserList.DataBind();
          UserList.SelectedValue = PreviouslySelectedUser;
      }
      else
      {
          UserList.DataBind();
          //do some other stuff
      }

Open in new window

0
GreymanMSCCommented:
Ah.  Sorry.  My fault.  The statement is missing a !=null on the end.  Opps!

The FindByValue method returns the found ListItem object (or null if not found),  The if statement requires a boolean condition.  This it is trying to implicitly convert the found list object.  


      String PreviouslySelectedUser = UserList.SelectedValue;
 
      UserList.DataBind(); //Bind before you search to reselect.
      if (UserList.Items.FindByValue(PreviouslySelectedUser)!=null)
      {
          UserList.SelectedValue = PreviouslySelectedUser;
      }
      else
      {
          //do some other stuff
      }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

delousedAuthor Commented:
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
0
delousedAuthor Commented:
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
0
delousedAuthor Commented:
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
0
delousedAuthor Commented:
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
0
delousedAuthor Commented:
Thanks for your help!  I figured it out!  I had something missing, your code worked prefectly!  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.