read a single row in csv file

Ask general questions here.
lingzhou
Posts: 21
Joined: Fri Jul 10, 2009 7:49 pm

read a single row in csv file

Post by lingzhou » Tue Sep 15, 2009 12:45 am

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.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: read a single row in csv file

Post by Support Team » Tue Sep 15, 2009 1:33 pm

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

lingzhou
Posts: 21
Joined: Fri Jul 10, 2009 7:49 pm

Re: read a single row in csv file

Post by lingzhou » Tue Sep 15, 2009 3:13 pm

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.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: read a single row in csv file

Post by Support Team » Wed Sep 16, 2009 1:48 pm

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

lingzhou
Posts: 21
Joined: Fri Jul 10, 2009 7:49 pm

Re: read a single row in csv file

Post by lingzhou » Thu Sep 17, 2009 3:14 pm

This one works perfect. Hope I can pick up C# quickly...