0

I am having an issue trying to match data table column names with a string containing a wildcard.

I have a data table with various column names which includes a set named like follows: "PsA", "PsB", "PsC", ect. I want to iterate through all the columns containing Ps in the title and use those titles to extract the data.

I currently have the following code which fails to return any matches. I have substituted a straight value in the if statement ("PsA") as a test, which works fine; however, when I use the wildcard, I get no matches. I have also tried Regex with no luck.

    private void dfaSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        string psText = null;
        string colID = "Ps*";

        offBox.Text = "";                       //Clear textbox if a reselection occurs
        psBox.Text = "";                        //Clear textbox if a reselection occurs

        info.offTitle = dfaSection.Text;       //Set textbox from variable

        dt = opr.findOffByTitle(info);          //Get datatable from SQL database

        if(dt.Rows.Count > 0)
        {
             offBox.Text = dt.Rows[0][5].ToString();   //Set textbox from datatable

             foreach(DataColumn dc in dt.Columns)           //Loop through datatable columns
             {
                if (dc.ColumnName.ToString() == colID) //Check that column title matches test string     - Later addition--> && dt.Rows[0][dc].ToString() != null)
                {
                    psText = dt.Rows[0][dc].ToString() + "\n\n";    //Add data from matched column to string variable
                }
             }

             psBox.Text = psText;           //Set textbox from variable
        }

    }

Edit: Issue fixed using .Contains(colID) Column names now being matched, string are now not being loaded to the psText variable, but I'll spend some time with that and get it working. Thanks Skaros Ilias.

Jexodus
  • 30
  • 3

3 Answers3

0

I am not so failiar with C, but == for sure is not the right method. you need to use the contains method. As I said, I am not familiar with it, but the docs have a good example of it.

String s = "This is a string.";
String sub1 = "this";
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
StringComparison comp = StringComparison.Ordinal;
Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));
Skaros Ilias
  • 1,008
  • 12
  • 40
0

As per this answer before : Regular expression wildcard

And here is sample of code :

private static bool Match(string pattern, string stringToCheck) {
    var finalPattern = pattern.Replace("*", ".*?");
    Regex regex = new Regex(finalPattern);
    return regex.IsMatch(stringToCheck);
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
0

take a breakpoint a check what is column name and colID at failure moment.How are you going to solve problems, when don't even know values? Use dc.ColumnName.ToString().StartsWith(). Contains() is good only untill string endswith.

  • I have checked the values at run time. I have also confirmed that a raw value works, which eliminates concern with values requiring trimming or similar. I have also test printed the column name values to ensure that they are being iterated through correctly. – Jexodus Jul 29 '17 at 11:16
  • ok, so what's `colID` value at failure? by the way. Remove wildcard from `colID` and do `.startswith` – user7234965 Jul 29 '17 at 11:23
  • colID is static and set to "Ps*". I have tried your suggestion of removing the wildcard and using `StartsWith()`, however this has not worked either. – Jexodus Jul 29 '17 at 11:31
  • what's a `colID` value at failure? and what is error message? – user7234965 Jul 29 '17 at 11:38
  • There is no error/exception. The code runs, however does not match the values that are supposed to be matched (eg. `psText = dt.Rows[0][dc].ToString() + "\n\n";` is never run because the `if` statement is never satisfied, though I can find no reason for the values not to match. As I mentioned, I have replaced the `colID` in the `if` statement with a raw value (`"PsA"`) which then matches and runs `psText = dt.Rows[0][dc].ToString() + "\n\n";` as it is supposed too. When I then try to use a wildcard to match the column names, no matches are found. – Jexodus Jul 29 '17 at 11:44
  • I said remove wildcard from `colID`, but not to replace with something else. Do `string colID = "Ps";` instead of `string colID = "Ps*";` . And why do you still use wildcards as don't use regexes? If you make `colID` as PsA, then it matches only with PsA, but not PsB. – user7234965 Jul 29 '17 at 11:51