How to read data from excel in C#

1,549 views
Skip to first unread message

Piotr K

unread,
Sep 15, 2011, 5:58:15 AM9/15/11
to Selenium Users
Hi all,

I would like to do simple test to read some data from excel.
Excel example:
Login Pass
Piotr selenium

So I would like to get this login and pass and put into my
application, but I don't know how to get those variables.


I was looking some simple code in internet and found something but I
have some problem. I'm using Visual Studio 2010 and C# language with
Nunit and I get following error:

"System.InvalidOperationException : The 'Microsoft.Jet.OLEDB.4.0'
provider is not registered on the local machine."

---------------------------------------------------------------
Part of my code is below

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
using System.Data.OleDb;
using System.Data;

namespace SeleniumTesting
{
[TestFixture]
public class Untitled
{
[SetUp]
public void SetupTest()
{

selenium = new DefaultSelenium("localhost",
4444,"*firefox", "http://www.mysite.com/");
selenium.Start();

}

[Test]
public void ExcelDataDrivenTest()
{

DataSet dsLoginDet = new DataSet();
dsLoginDet = getData(); <-- here is this problem

....
-----------------------------------------------------------------------


Can you somehelp me with this or maybe provide me some another code
soultion for C# and VS 2010. Please help me!!

Thanks a lot for help!!!

BR
Piotr

PeterJef...@hotmail.co.uk

unread,
Sep 15, 2011, 6:07:38 AM9/15/11
to Selenium Users
Hi Piotr

I can't tell you how to connect C# to Excel. I'm sure it should be
possible but may be more convoluted and slower to run than necessary.

Although I store my source data in Excel files for user convenince, I
decided it would be better to automatically export them as CSV's when
saving them which makes a much more convenient and quicker format to
read.

That also decouples you nicely from Excel so you can more easily
change your data management tool at any time (should Excel ever get
superceeded) without affecting your actual test framework.

Peter

Mamta Garg

unread,
Sep 15, 2011, 6:10:32 AM9/15/11
to seleniu...@googlegroups.com
Please refer below code:

 public DataSet GetRequiredRows(string filename)
        {
            DataSet dataset = new DataSet();
            string ConnectionString = null;
            string testCaseFile = Path.GetFullPath(Property.TEST_CASE + "\\" + MainClass.moduleName + ".xls");
            ConnectionString = MainClass.dic["EXCEL_CONNECTION_STRING"];
            ConnectionString = ConnectionString.Replace("{filepath}", testCaseFile);
            OleDbConnection conn = new OleDbConnection(ConnectionString);
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(Property.testCasequery, conn);
            adapter.Fill(dataset);
            conn.Close();

            return dataset;
        }



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--
Thanks and Regards,
Mamta Garg

gati shah

unread,
Sep 15, 2011, 6:27:38 AM9/15/11
to seleniu...@googlegroups.com

Piotr K

unread,
Sep 15, 2011, 6:53:00 AM9/15/11
to Selenium Users
Hi Peter :)

If you suggest CSV file please provide me some code example.


I'm newbie in Data Driven Development by using selenium so I'm looking
for some solution for this.

Guys,

Can you provide me step by step because I'm learning this and C# and I
don't know how to do this.

Thanks

On 15 Wrz, 12:07, "PeterJeffreyG...@Hotmail.co.uk"
Reply all
Reply to author
Forward
0 new messages