I have various admin pages on my system that allow user to insert a new item or update one. The code I am using to check if the description entered is already in the drop down list is

 //loop through each item in ddlX and compare with description entered by user
                foreach (ListItem i in ddlX.Items)
                    //check if description matches existing values
                    if (i.Text == a.XDescription)
                        //set boolean value
                        XExists = true;

                        //set panel visiblity
                        pnlInsertSuccess.Visible = false;
                        pnlInsertError.Visible = true;
                        lblInsertError.Text = "Insert cancelled - item already exists";
                 if (!XExists)
                        //insert object into database

                        //set panel visibility
                        pnlInsertSuccess.Visible = true;
                        pnlInsertError.Visible = false;

                        //display success message
                        lblInsertSuccess.Text = "item '" + X.Text + "' inserted successfully";

                        ListItem i = new ListItem("Please Select ... ", "0");
                        ddlX.AppendDataBoundItems = true;

                    catch (Exception ex)
                        pnlInsertSuccess.Visible = false;
                        pnlInsertError.Visible = true;

                        lblInsertSuccess.Text = "Error Inserting - " + ex.Message.ToString();

I am using this code on other admin pages and it works perfectly but for here it doesn't, debugging has brought to my attention it is only comparing the text the user entered with the first item in the drop down 'Please Select ...'
anyone know how I can fix this?

Thanks in advance

Recommended Answers

All 4 Replies

I'm not sure your logic is correct. From what I am interpreting, you are going through your for loop and comparing each value. You are not exiting the for loop at the time there is a match so the results are always going to be the same unless the description happens to match the last item checked in this loop.

Use "for loop", rather than using "foreach loop". It may solve your issue.

The foreach will move throught the list of items i in ddlX.Items, and stop when the last one is read. So that should be fine.

Question though ... What is a.XDescription? Is it a string object? If not it'll need to be converted or cast to a string to get a match.

I actually can't see anything wrong with the loop, as such.

JorgeM has a good point, but since you aren't resetting XExists it should maintain its state through the remainder of the iterations.

The comparison may be an issue as ggamble suggests, but afaik C# will throw an exception if it can't do an implicit type conversion during the comparison anyway.

Can you break your code on the foreach and check the contents of ddlX.Items to ensure it has all the items you expect? If this list is being created dynamically the processing may be occuring prior to the list being re-populated after the postback.

Otherwise it is possible the cause of the issue is outside the code snippet provided here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.