Hello, I'm struggling with the following issue, and need help with it..

I'm using ASP.NET, and trying to create a primitive webshop. You "buy" services, not products, and you only need to buy them once, so I'd like to filter the cart to add an element only once, no matter how many times a customer trying it. Here's my code, which ought to do this:

public void addItem(string name, string price)
    {
        GridView kosar = (GridView)this.Master.FindControl("kosarGridView");
        bool van = false;
        for (int i = 0; i < kosar.Rows.Count; i++)
        {
            
            if (kosar.Rows[i].Cells[0].Text == name)
            {
                van = true;
                aa.Text = "aa"; //just for diagnostical purpose

            }
            
        }


          if (van == false)
          {
              DataRow product = Cart.NewRow();
              product[0] = name;
              product[1] = price;
              Cart.Rows.Add(product);
          }
           kosar.DataBind();
           

        
    }

As you may see, it just compares the two strings, and if they are the same, does nothing, else it adds a new line to the gridview.

Trouble is the two strings are never the same for this method, and I just can't figure it out why. Because they are the same, I've tested it a couple of times.

What am I doing wrong? Please help me out!

Thanks in advance!

Does kosar.Rows.Cells[0].Text return string value?

For example if the argument 'name' is 'Product1' and the value of 'kosar.Rows.Cells[0].Text ' is also 'Product', but the condition kosar.Rows.Cells[0].Text == name returns false.

Did you mean the above scenario?

Thank you for your answer!

It's not that simple I think.

The girdView kosar contains two columns, one for the product name inserted into it by the void's "name" property, and price. The relevant is the name. E.g.: I'd like to buy tomatoe. I click on the button, and then the method would look like this:

public void addItem(string name, string price)
    {
        GridView kosar = (GridView)this.Master.FindControl("kosarGridView");
        bool van = false;
        for (int i = 0; i < kosar.Rows.Count; i++)
        {
            
            if (kosar.Rows[i].Cells[0].Text == "tomatoe")
            {
                van = true;
                aa.Text = "aa"; //just for diagnostical purpose

            }
            
        }


          if (van == false)
          {
              DataRow product = Cart.NewRow();
              product[0] = "tomatoe";
              product[1] = price;
              Cart.Rows.Add(product);
          }
           kosar.DataBind();
                   
    }

So as you see there are two options:
1. tomatoe is not in the girdview -> the method checks for it and then insert it.
2. tomatoe is on the list -> method checks, founds it, then does nothing.

So the problem is why doesn't it find it? Is my comparsion method wrong? I've tried to call the string.Normalize() and similar functions, but still nothing..

Any suggestions?

The comparison seems to be ok.

Can you post the code on how you are binding the GridView and also its HTML?

I think I got it.

I'm Hungarian, and it means we've got an alphabet of 44 characters, including "á, ő, ű", etc.
And the porblem shows up only, when these characters are involved. So everything works fine with the word tomatoe, but the issue will show up with the word "térkép".

I think the codebehind and the website are using different text formats. But it's just a clue, I'm not sure how to figure this out..

Any ideas how could I overcome this? Some unicode or any other text conversion?

This article has been dead for over six months. Start a new discussion instead.