0

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)
                {
                    try
                    {
                        //insert object into database
                        XManager.InsertX(a);

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

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


                        ddlX.Items.Clear();
                        ListItem i = new ListItem("Please Select ... ", "0");
                        ddlX.Items.Add(i);
                        ddlX.AppendDataBoundItems = true;
                        ddlX.DataBind();
                    }

                    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

5
Contributors
4
Replies
35
Views
4 Years
Discussion Span
Last Post by |-|x
0

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.

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.