C # patch loop for Datagridview cells
I am working on a form with datagridview and webbrowser controls. I have three columns as url, username and password in datagridview. I want to do login automation for some of the websites that I use frequently. For this reason, I'm not sure if this is the correct approach, but I created the code below. The problem is with the switch argument.
I'll click a row on the datagridview and then click the login_button so that the username and password information is passed to the appropriate fields on the webpage. Why do I need a switch loop because all web pages have different item IDs for the username and password fields.
As I said, I'm not sure if the datagridview resolves the toggle case, I searched the net but couldn't find any samples.
private void login_button_Click(object sender, EventArgs e)
{
switch (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())
{
case "http://www.website1.com":
webBrowser1.Document.GetElementById("username").InnerText = dataGridView1.Rows[3].Cells[3].Value.ToString();
webBrowser1.Document.GetElementById("password").InnerText = dataGridView1.Rows[3].Cells[4].Value.ToString();
return;
case "http://www.website2.com":
webBrowser1.Document.GetElementById("uname").InnerText = dataGridView1.Rows[4].Cells[3].Value.ToString();
webBrowser1.Document.GetElementById("pswd").InnerText = dataGridView1.Rows[4].Cells[4].Value.ToString();
return;
}
HtmlElementCollection elements = this.webBrowser1.Document.GetElementsByTagName("Form");
foreach (HtmlElement currentElement in elements)
{
currentElement.InvokeMember("Login");
}
}
a source to share
You made sure that
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
returns url?
This will be the first place to start.
May be:
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()
As for the switch datagrid +, the datagrid doesn't know / care about the switch. Everyone knows that when the button is pressed, the call is:
private void login_button_Click(object sender, EventArgs e)
What happens if you hardcoded the URL? Does everything work?
EDIT: Oh, it won't work! You are returned to your switchboard. So you end up with a method and never call:
HtmlElementCollection elements = this.webBrowser1.Document.GetElementsByTagName("Form");
foreach (HtmlElement currentElement in elements)
{
currentElement.InvokeMember("Login");
}
a source to share
I'm not sure if this is definitely in C #, but you might have to switch to a temporary variable eg
string site = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
switch(site)
{
....
}
if nothing else makes debugging easier.
also each case must end with a break; no refund;
a source to share