Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to create a web form where clicking on button will open Gmail in IE.

Below is the code I am using, when I run it and click on Open Gmail button I am getting error at underlined line.

I have tried both Passwd and passwd-hidden but get same error.

Also, I want to know how can I read my password from a secure place and not hardcode it in code.

Thanks for your help in advance.

C#
private void button2_Click(object sender, EventArgs e)
        {


            //second try
            //open Gmail
            InternetExplorer explorer = new InternetExplorer();

            explorer.DocumentComplete += OnIEDocumentComplete;

            // Setting the documentComplete Event to false
            documentComplete = new AutoResetEvent(false);

            object mVal = System.Reflection.Missing.Value;
            explorer.Navigate("http://www.gmail.com", ref mVal, ref mVal, ref mVal, ref mVal);

            // Waiting for the document to load completely
            documentComplete.WaitOne();

            HTMLDocument doc = (HTMLDocument)explorer.Document;

            HTMLInputElement userID = (HTMLInputElement)doc.all.item("Email", 0);
            userID.value = "abc@gmail.com";
            HTMLInputElement pwd = (HTMLInputElement)doc.all.item("Passwd-hidden", 0);
            pwd.value = "abcdef";
            HTMLInputElement btnsubmit = (HTMLInputElement)doc.all.item("signIn", 0);
            btnsubmit.click();
            explorer.Visible = true;
        }


Error:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe

Additional information: Object reference not set to an instance of an object.
Posted

1 solution

If you are getting the error on this line:
C#
pwd.value = "abcdef";
Then pwd has to be null. So look at where it is set - in teh line above in this case:
C#
HTMLInputElement pwd = (HTMLInputElement)doc.all.item("Passwd-hidden", 0);
pwd.value = "abcdef";
So clearly doc.all.item is returning null, which almost certainly means it can't find a matching item called "Passwd-hidden".

So check the page data, and find out what it is called!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900