Simple if statement doesn't work

Discussion in 'ASP .Net' started by Andy B, Apr 25, 2008.

  1. Andy B

    Andy B Guest

    I have a wizardStep with 2 textBoxes and a button on it. The textBoxes are
    called WordTextBox and DefinitionTextBox. In the button_click event, I added
    this code:

    AddDefinition();

    The code for the AddDefinition method is below:

    private void AddDefinition() {

    ///Todo: Add validation for adding duplicate words, empty values

    StockContract.Dictionary = new ContractDictionary<string, string>();

    if(StockContract.Dictionary.ContainsKey(WordTextBox.Text)) {

    DefinitionList.Caption = "This word already exists!";

    return;

    }


    StockContract.Dictionary.Add(WordTextBox.Text, DefinitionTextBox.Text);

    DefinitionList.DataSource = StockContract.Dictionary;

    DefinitionList.DataBind();

    WordTextBox.Text = String.Empty;

    DefinitionTextBox.Text = String.Empty;

    }

    It creates a Dictionary collection and assigns it to the Dictionary of the
    StockContract object. It then takes the 2 textboxes and puts the values
    inside the dictionary collection. Everything works fine except the if
    statement. It is supposed to determine if the value of WordTextBox.Text is
    actualy a Key in the dictionary. If it is already in the collection, change
    the caption of the gridview to something different and exit the method
    without continuing. If it doesn't already exist, continue adding it to the
    collection. Any idea what might be wrong with it?
    Andy B, Apr 25, 2008
    #1
    1. Advertising

  2. Andy B

    Jeff Dillon Guest

    What is not working? I assume you've tested with hard coded values, and
    stepping through the code in the debugger, etc

    Jeff

    "Andy B" <> wrote in message
    news:...
    >I have a wizardStep with 2 textBoxes and a button on it. The textBoxes are
    >called WordTextBox and DefinitionTextBox. In the button_click event, I
    >added this code:
    >
    > AddDefinition();
    >
    > The code for the AddDefinition method is below:
    >
    > private void AddDefinition() {
    >
    > ///Todo: Add validation for adding duplicate words, empty values
    >
    > StockContract.Dictionary = new ContractDictionary<string, string>();
    >
    > if(StockContract.Dictionary.ContainsKey(WordTextBox.Text)) {
    >
    > DefinitionList.Caption = "This word already exists!";
    >
    > return;
    >
    > }
    >
    >
    > StockContract.Dictionary.Add(WordTextBox.Text, DefinitionTextBox.Text);
    >
    > DefinitionList.DataSource = StockContract.Dictionary;
    >
    > DefinitionList.DataBind();
    >
    > WordTextBox.Text = String.Empty;
    >
    > DefinitionTextBox.Text = String.Empty;
    >
    > }
    >
    > It creates a Dictionary collection and assigns it to the Dictionary of the
    > StockContract object. It then takes the 2 textboxes and puts the values
    > inside the dictionary collection. Everything works fine except the if
    > statement. It is supposed to determine if the value of WordTextBox.Text is
    > actualy a Key in the dictionary. If it is already in the collection,
    > change the caption of the gridview to something different and exit the
    > method without continuing. If it doesn't already exist, continue adding it
    > to the collection. Any idea what might be wrong with it?
    >
    >
    >
    >
    >
    >
    Jeff Dillon, Apr 25, 2008
    #2
    1. Advertising

  3. You may not be showing us everything, but it doesn't look like you're
    looking for case sensitivity in here. I would insert all values
    as .ToUpper or .ToLower and check the textbox.text.tolower[toUpper]
    against the dictionary. You'll also want to .Trim the text.
    cfps.Christian, Apr 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    101
  2. Replies:
    2
    Views:
    142
  3. ranrodrig
    Replies:
    2
    Views:
    85
  4. stroncococcus

    type casting for DBI SQL statement doesn't work

    stroncococcus, Dec 6, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    167
    stroncococcus
    Dec 6, 2006
  5. Huub
    Replies:
    10
    Views:
    171
    Joe Smith
    Apr 29, 2007
Loading...

Share This Page