Page 1 of 1

read a single row in csv file

Posted: Tue Sep 15, 2009 12:45 am
by lingzhou
Hi, I would like to read a single row in a csv file, the csv file is like this:

EnvironmentURL
localhost

want to read "localhost" as a parameter and pass it out, now I have this code in my program.cs but got some errors

CSVConnector csvConnector = new CSVConnector(@"..\..\EnvironmentURL.csv");
string EnvironmentURL = csvConnector.Rows["EnvironmentURL"].ToString();

the error I got was "cannot convert from sting to int"

appreciate the help.

Re: read a single row in csv file

Posted: Tue Sep 15, 2009 1:33 pm
by Support Team
lingzhou wrote:the error I got was "cannot convert from sting to int"
... because you have a syntax error. The Rows property indexer takes an integer as parameter, not a string. The following code should work:
string EnvironmentURL = csvConnector.Rows[0].ToString();
Regards,
Alex
Ranorex Support Team

Re: read a single row in csv file

Posted: Tue Sep 15, 2009 3:13 pm
by lingzhou
Alex, thank you for the help.

The code below doesn't throw any error, but it returns EnvironmentURL equal to "system.data.datarow".

I would like EnvironmentURL to be "localhost" in this case.

Re: read a single row in csv file

Posted: Wed Sep 16, 2009 1:48 pm
by Support Team
Ahhh, now I know what you are trying to do, you are trying to get the value for EnvironmentURL from the first row. Well, that's quite simple, in your code you are simply missing the index for the row! You need to specify what row you want to get the value from like in the code below:
string EnvironmentURL = csvConnector.Rows[0]["EnvironmentURL"].ToString();
Regards,
Alex
Ranorex Support Team

Re: read a single row in csv file

Posted: Thu Sep 17, 2009 3:14 pm
by lingzhou
This one works perfect. Hope I can pick up C# quickly...