Home > Software engineering >  Convert datatable into string - this error is being generated 'string' does not contain a
Convert datatable into string - this error is being generated 'string' does not contain a

Time:09-28

I have a feature file with a datatable. When the value UserCode ='0001' is getting passed,it is converting it to '1'. Therefore I added .ToString() method to the following line: dynamic data = table.CreateDynamicInstance().ToString();

Now I am getting the following error:

'string' does not contain a definition for 'UserCode'

Feature File

And I add a user Code
    | UserCode|
    | 0001    |

Step definition

[Then(@"I add a User Code")]
public void ThenIAddAUserCode(Table table)
{
    dynamic data = table.CreateDynamicInstance().ToString();
    // string str = Convert.ToString(data);
    // DocM.AddUserCode((string)data.UserCode);
    DocM.AddUserCode(data.UserCode);
}

Page method

public void AddUserCode(string usercode)
{
    IJavaScriptExecutor je = (IJavaScriptExecutor)driver;

    //find the invisible element on the list by xpath/tag etc. 
    var path = By.XPath("//table[@id='DocMP']/tbody/tr[1]//td[1]//input[@value='"   usercode  "']");
            
    var Userpath = driver.FindElement(path);

    MyFunc.WaitFor(driver, path);

    //use javascript to  navigate to that element
    je.ExecuteScript("arguments[0].scrollIntoView(false);", Userpath);
    MyFunc.HighlightElement(driver, Userpath);

    if (Userpath.Displayed)
    {
        Userpath.Click();
        btnApply.Click();
    }
}

Can someone please help on this. I am new to c# and specflow.

CodePudding user response:

SpecFlow does its best to guess the data type when converting data tables to a dynamic in C#. In this case, SpecFlow appears to guess that a string containing only digits is most likely an int, which of course strips the leading zeros from 0001. Instead, consider creating a Data Transfer Object to represent the data in the data table:

public class X
{
    public string UserCode { get; set; }
}

And change your step definition to create an new instance of this class (see Table.CreateInstance() method). I don't see enough of the scenario to suggest a name, so replace public class X with a name that makes sense for your use case. I typically use the Row or DataTableRow suffix for classes mapped from data tables in SpecFlow. For example, if the data table represented a blog post, I would name the DTO BlogPostRow or BlogPostDataTableRow.

You step definition should change to:

[Then(@"I add a User Code")]
public void ThenIAddAUserCode(Table table)
{
    X data = table.CreateInstance<X>();
    DocM.AddUserCode(data.UserCode);
}

If all you need to do is pass the user code in the step, I would recommend using a quoted value instead:

And I add a user Code "0001"

If you auto generate the step, SpecFlow will still guess that 0001 is an int, so this following step definition should work:

[Then(@"I add a User Code ""(.*)""")]
public void ThenIAddAUserCode(string userCode)
{
    DocM.AddUserCode(userCode);
}
  • Related