Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First time code runs smothly and load the user on 2nd time an exception occurs for data type conversion. Double to string conversion at bold inner loop for password reading 1234 from column B.


for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    Loginname = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;

                    <big>(for (cCnt = 2; cCnt <= range.Columns.Count; cCnt++)
                    {
                        Password = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value; 
                    }</big>
                }



Excel File like View


Rows ColumnA(Username) Column B(Password)
1 ab ab12
2 cd 1234

What I have tried:

<pre lang="c#"><pre>using System;
using System.Text;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.CSharp;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using Excel = Microsoft.Office.Interop.Excel;


namespace Reading_from_Excel
{
    [TestClass]
    public class ReadingExcel
    {
        string Loginname;
        string Password;
        [TestMethod]
        public void TestMethod1()
        {
            
            
            int rCnt;
            int cCnt;
            string workbookPath = "e:\\Test\\Reading_from_Excel\\Reading_from_Excel\\TestData.xlsx";

            Excel.Application excelApp;
            Excel.Workbook excelWorkbook;
            Excel.Sheets excelSheets;
            Excel.Worksheet excelWorksheet;
            Excel.Range range;

            string currentSheet = "DataSet";
            
            excelApp = new Excel.Application();
            excelWorkbook = excelApp.Workbooks.Add(workbookPath);
            excelSheets = excelWorkbook.Sheets;
            excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
            range = excelWorksheet.UsedRange;

            //Excel.Range myIDBinder = (Excel.Range)excelWorksheet.get_Range("B2", "B2");
            //string loginID = myIDBinder.Value.ToString();

            //Excel.Range myPasswordBinder = (Excel.Range)excelWorksheet.get_Range("C2", "C2");
            //string usrPwd = myPasswordBinder.Value.ToString();

            for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    Loginname = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;

                    for (cCnt = 2; cCnt <= range.Columns.Count; cCnt++)
                    {
                        Password = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;
                    }
                }
                    

                string baseURL = "https://www.mycabtravel.com";

                FirefoxDriver driver = new FirefoxDriver();

                driver.Navigate().GoToUrl(baseURL);
                driver.FindElement(By.Id("loginlink")).Click();
                driver.FindElement(By.Id("login-popup"));
                driver.FindElement(By.Id("userFieldDiv"));
                driver.FindElement(By.Id("headerSubView:inputUserName:input")).SendKeys(Loginname);
                driver.FindElement(By.Id("passwordFieldDiv"));
                driver.FindElement(By.Id("headerSubView:inputPassword:input")).Clear();
                driver.FindElement(By.Id("headerSubView:inputPassword:input")).SendKeys(Password);
                driver.FindElement(By.Id("headerSubView:loginBtnId")).Click();
            }
        }
    }
}
Posted
Updated 17-Feb-17 2:05am

1 solution

Excel used auto formatting for that cell and treated the content as numeric value (which it is in your second row).

Just use the Text property to get the value as text:
C#
Password = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Text; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900