Skip to content
Advertisement

Need to select a value from table and store it into variable

Hello everyone I am currently working on some testing project and I am having a little problem. Using selenium, I need to SendKey in specific element but instead of fixed value i need to use value (data) from my database. Can anyone help me with how to retrieve single value from database and store it in a variable so i can use it later.

Thank you and sorry for a noobish question – see code below:

 SqlConnection conn = new SqlConnection();
 SqlCommand command;
 SqlDataReader dataReader;
 conn.ConnectionString = "Server=******;Database=****;User ID=sqlserver;password=****;MultipleActiveResultSets=true;");
 string query = "select RequestID, from AutomaticPayment where RequestID ='1230322'";
 DataTable dt = new DataTable();
 command = new SqlCommand(query, conn);
 conn.Open();
 dataReader = command.ExecuteReader();
 dt.Load(dataReader);
 driver.FindElement(By.Id("requestID")).SendKeys(VALUE FROM DATABASE);

Advertisement

Answer

Use conditions to limit the result:

Select data SELECT TOP 1 RequestID FROM AutomaticPayment // Always returns 1 row

Or

SELECT RequestID FROM AutomaticPayment WHERE Id = 123  // Id must be unique to return 1 row

And maybe other ways.

Get value

var value = dt.Rows[0][1];

Or

var value = dt.Rows[0]["RequestID"];
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement