-1

I am trying to run a if statement that checks if a string contains a word that's placed in a datatable but it does not work properly.

if (nickName.Contains(dt.Rows[1]["Word"].ToString()))
{
    //something something
}

In this case Nickname is a string var which contains user inputs. dt is a datatable which contains words (I actually loop through this DT but that does not matter for this example)

But this does not work... even if I copy and paste the word.

I also noticed that just trying to this (Yes I'm sure that that spot in the DT is the word I'm trying to check)

 if (dt.Rows[1]["Word"].ToString() == "some word")

Does not work (even when copy and pasting the word).

Do datatables store string in a funny way or something like that?

Do any of you have an solution?

(here is more of the code if you want)

        //textboxes to vars
        string nickName = tbtNickName.Text;
        string tweet = tbTweet.Text;

        //creating DT
        var dt = new DataTable();
        using (var da = new SqlDataAdapter("SELECT * FROM Words;", "Data Source=localhost;Initial Catalog=twtWall;User Id=<id>; Password=<password>"))
        {
            da.Fill(dt);
        }

        //for each element in an array chek if you are in any inputs
        for (int i = 0; i <= dt.Rows.Count - 1; i++)
        {
            //if word detected set error
            if (nickName.Contains(dt.Rows[i]["Word"].ToString()))
            {
                errorProvider.SetError(BtnTweet, "Bad word detected!");
            }

            //do the same thing with tweets...

        }
Kols
  • 3,641
  • 2
  • 34
  • 42
Merlijn
  • 64
  • 5
  • 1
    Put a breakpoint on that line, look at what `nickName` and `dt.Rows[i]["Word"].ToString()` are equal to in each loop. – Equalsk Dec 21 '17 at 16:33
  • 2
    It may be a leading space in the string. Try something like `dt.Rows[i]["Word"].ToString().Trim()` or do like @Equalsk suggested. – Alisson Reinaldo Silva Dec 21 '17 at 16:34
  • Possibly https://stackoverflow.com/questions/444798/case-insensitive-containsstring – Paul Abbott Dec 21 '17 at 16:35
  • 1
    No, DataTables don't "store strings weird." You have some logic error, but it isn't clear what that error might be. What is some of the example data? How many columns? What are their names? What values will you find in the "word" column? –  Dec 21 '17 at 16:35
  • Case sensitive issues? Check for `IndexOf` instead of `Contains` `if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)` – Sunil Dec 21 '17 at 16:36
  • Or a character that looks like a space but isn't, such as a non-breaking space. https://stackoverflow.com/questions/15565479/weird-space-character-in-string-thats-not-a-space – bornfromanegg Dec 21 '17 at 16:36
  • Why are you using a DataTable instead of strongly typed objects? – mason Dec 21 '17 at 16:37
  • 5
    @DourHighArch Not sure if you're confusing another language, in C# `"Hello".Contains("Hello");` is `true`. – Equalsk Dec 21 '17 at 16:41
  • I should add that `"Hello".Contains("hello");` is `false` as `.Contains()` is case sensitive. – Equalsk Dec 21 '17 at 16:42
  • @DourHighArch [are you sure?](https://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.110).aspx) –  Dec 21 '17 at 16:50
  • @Amy that did it! thanks! But I didn't add any spaces while inputting the data in the database. does sql or a datatable do that? – Merlijn Dec 21 '17 at 17:04
  • @MerlijnLent what did I do or say that did it? i'm happy to have helped, I'm just not sure what I did? –  Dec 21 '17 at 17:05
  • @MerlijnLent SQL can do that, depending on the column type. `varchar(x)` columns will not, but `char(x)` columns will pad their values. –  Dec 21 '17 at 17:06
  • @Amy While selecting some data in my database as example data I found a mistake in my database design which solved another problem I had. – Merlijn Dec 21 '17 at 17:13
  • You still haven't answered my question. – mason Dec 21 '17 at 17:34
  • @mason DataTables haven't been deprecated. Sometimes you just want to use a DataTable — nothing wrong with that. – LarsTech Dec 21 '17 at 18:18
  • @Equilask; you're right, I was thinking of LINQ Contains, not String.Contains. – Dour High Arch Dec 21 '17 at 18:20
  • @LarsTech Yes, there absolutely is something wrong with it. It's a leaky abstraction. It's a lot of complexity for something that should be very simple. It's inefficient. And it leads to devs doing unnecessary type conversions. Outside of a few minimal areas (like SqlBulkCopy), using DataTable is a terrible choice for new development and its usage should be discouraged. – mason Dec 21 '17 at 18:23
  • You guys should discuss the merits of DataTables elsewhere, not in these comments. This isn't the place for it. –  Dec 21 '17 at 19:41

1 Answers1

1

How about case sensitive issue? or contains spaces or extra chars.

For these, you should check for IndexOf instead of Contains

if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)

This will handle case sensitive scenario and also if word is somewhere hidden in the entire input text.

Sunil
  • 3,404
  • 10
  • 23
  • 31