I'm using Selenium in C# to retreive the data from a database with 2 rows of data with the columns; UserID, UserName, and Password. I'm just getting the UserName and Password from the rows to enter into the webpage. I'm able to return the first row of my Database but I'm trying to have my code run as many times as I have rows of data in my database. Here is my code so far.
public class DBConnection
{
static SqlConnection con;
static SqlDataReader dr;
static SqlCommand cmd;
public static void DBConnect()
{
ConnectionStringSettings connection = ConfigurationManager.ConnectionStrings["DB_Table"];
String connectionString = connection.ConnectionString;
try
{
con = new SqlConnection(connectionString);
con.Open();
cmd = new SqlCommand("SELECT UserName, Password FROM dbo.Users", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
string username = dr["UserName"].ToString();
Console.WriteLine(username);
string password = dr["Password"].ToString();
Console.WriteLine(password);
Thread.Sleep(5000);
AUT.Browser.FindElement(By.Name("username")).Click();
AUT.Browser.FindElement(By.Name("username")).Click();
AUT.Browser.FindElement(By.Name("username")).Clear();
Thread.Sleep(1000);
AUT.Browser.FindElement(By.Name("username")).SendKeys(username);
AUT.Browser.FindElement(By.Name("password")).Clear();
AUT.Browser.FindElement(By.Name("password")).SendKeys(password);
AUT.Browser.FindElement(By.Name("password")).SendKeys(Keys.Enter);
}
}
catch (Exception e)
{
Console.WriteLine("Error" + e.Message);
}
finally
{
con.Close();
}
}
}
This code retrieves the correct data from the Database but it doesn't get the multiple rows of data. It only grabs the first row and then it tries to overwrite the data using the second row of data from the first row after the webpage reaches the next page which causes my Test to fail. I'm trying to have it reopen the webpage and reenter another userName and Password from the 2 or more rows of data I have in my database after it opens and reads the first row of data. My guess is that I would have to close and reopen the connection in my while loop, but I've tried that and I'm having no luck. Any help would be greatly appreciated. Thank you very much.