1

I am new to c# and I am using windows forms.

I am trying to login into a website (https://www.followmee.com) using C# application with webBrowser control but I faced a problem when I logged in.

As shown in the code, when I click button_Login it logs in fine but as soon as it logs in it throws an error "object reference not set to an instance of an object"

This is all the code I use:

    string userName= "abcd";
    string LPassword = "1234";
    private void button_Login_Click(object sender, EventArgs e)
    {

        webBrowser1.Navigate("https://www.followmee.com/login.aspx");
     }

   private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {


          webBrowser1.Document.GetElementById("ctl00_Main_Login1_UserName").SetAttribute("value", userName);
            webBrowser1.Document.GetElementById("ctl00_Main_Login1_Password").SetAttribute("value", LPassword);
            webBrowser1.Document.GetElementById("ctl00_Main_Login1_LoginButton").InvokeMember("click");

    }

Anyone knows what I am doing wrong or what code I am missing. Please help me , Thank you.

Someone marked my question as duplicated but I don't think so. I have already read that question and it did not help. ok it explains "You are trying to use something that is null" and I know that I am using the object that contains the null value reference but I do not know which object and what causes this.

Kate
  • 935
  • 4
  • 14
  • 34
  • 1
    Did you try debugging the code? Execution will stop at the point where the exception is thrown. A NullReferenceException is very easy to debug - you tried to access a *null* reference. I'll bet that one of the `GetElementById` methods returnd `Null` because there is no control with that ID. – Panagiotis Kanavos Mar 29 '16 at 11:47
  • Load your webbrowser at form load with your login page and then login from your button event. Your error happen cause you search for id before load your page fully – LoopCoder Mar 29 '16 at 11:55
  • @ Jamiec . you marked my question as duplicated but I don't think so. I have already read that question and it did not help. ok it explains "You are trying to use something that is null" and I know that I am using the object that contains the null value reference but I do not know which object and what causes this. – Kate Mar 29 '16 at 11:56
  • @Kate i replicated your code in my VS, i didn't receive any Exception. It seems the userid/password combo ins't correct for it says `Your login attempt was not successful. Please try again.` even on the site in the browser with given userid/password – manish Mar 29 '16 at 12:05
  • @ LoopCoder . you are the winner , please make your comment as an answer so I can vote you. tnx – Kate Mar 29 '16 at 12:23
  • @ manish . sorry the login details i gave you is just an example and they are not real, anyway @ LoopCoder solved the issue . tnx – Kate Mar 29 '16 at 12:24

0 Answers0