We help IT Professionals succeed at work.

Check if item is in data

deloused
deloused asked
on
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
Comment
Watch Question


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

Open in new window

Author

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

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

Author

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.  

Author

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.  

Author

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.  

Author

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.  

Author

Commented:
Thanks for your help!  I figured it out!  I had something missing, your code worked prefectly!  

Explore More ContentExplore courses, solutions, and other research materials related to this topic.