title: Trying to log in to a website through a C# program
body: <p>I'm new to C# so I looked for this topic in other questions but they weren't for me. What I am trying to do is I currently try to login to my school's servers using a c# program(Which I'm trying to implement). What I'm trying to do is I know the code of the page, so I am using web browser of c# to navigate then I just want to write name and password to the input boxes and this is where I stuck. Can you please give me any advices? </p>
  
  <p>If you want to look at page: <a href="https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin" rel="nofollow">https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin</a></p>
  
  <p>Thanks for your advices.</p>
  
  <p>Here how I used the code(Edit: Added eventhandler but this is my first time using so it promts me "object reference not set to a instance of an object"):</p>
  
  <pre><code>            private void buttonGo_Click(object sender, EventArgs e)
      {
          try
          {
              string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";
  
  
              webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
  
              webBrowser1.Navigate(input);
              HtmlDocument doc = webBrowser1.Document;
              HtmlElement userName = doc.GetElementById("UserID");
              HtmlElement pass = doc.GetElementById("PIN");
              HtmlElement submit = doc.GetElementById("Login");
  
              userName.SetAttribute("value", textID.Text);
              pass.SetAttribute("value", textPASS.Text);
  
              submit.InvokeMember("Click");
  
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
      public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
          var webBrowser = sender as WebBrowser;
          webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
          MessageBox.Show(webBrowser.Url.ToString());
      } 
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Finally I solved problem I cheated a little but managed to solve. Here is the working code:</p>
  
  <pre><code>private void buttonGo_Click(object sender, EventArgs e)
          {
              try
              {
                  string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";
  
  
                  webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
  
                  webBrowser1.Navigate(input);
  
                  HtmlDocument doc = webBrowser1.Document;
                  //HtmlElement userName = doc.GetElementById("UserID"); These not worked because ID of the elements were hidden so they are here to show which of these did not work.
                  //HtmlElement pass = doc.GetElementById("password");
                  HtmlElement submit = webBrowser1.Document.Forms[0].Document.All["PIN"].Parent.Parent.Parent.NextSibling.FirstChild;
  
                  //userName.SetAttribute("value", textID.Text);
                  //pass.SetAttribute("value", textPASS.Text);
  
  
  
  
                  webBrowser1.Document.Forms[0].All["UserID"].SetAttribute("value", textID.Text);
                  webBrowser1.Document.Forms[0].All["PIN"].FirstChild.SetAttribute("value", textPASS.Text);
                  submit.InvokeMember("Click");
  
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
          public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
          {
              var webBrowser = sender as WebBrowser;
              webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
              MessageBox.Show(webBrowser.Url.ToString());
          } 
  </code></pre>