0

I am adding value to a textbox on webpage. Here is the Inspect Element code:

   <input type="text" name="DERIVED_LAM_GRADE_1$0" id="DERIVED_LAM_GRADE_1$0" 
   tabindex="90" value="0.00" class="PSEDITBOX" style="width:44px; 
   text-align:right; " maxlength="6" onchange="addchg_win0(this);oChange_win0=this;">

and here is my C# code: Which is giving me Exception "Object reference not set to an instance of an object."

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
   using System.Windows.Forms;

    namespace WindowsFormsApplication2
 {
  public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string username = textBox1.Text;

        HtmlDocument doc = this.webBrowser1.Document;
        doc.GetElementById("DERIVED_LAM_GRADE_1$0").SetAttribute("value", username);


    }
}
}

i Dont know whats wrong. i am giving a perfect id and still its throwing exception.

Designer CODE:

   namespace WindowsFormsApplication2
  {
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.webBrowser1 = new System.Windows.Forms.WebBrowser();
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.button1 = new System.Windows.Forms.Button();
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.textBox1 = new System.Windows.Forms.TextBox();
        ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();
        // 
        // webBrowser1
        // 
        this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.webBrowser1.Location = new System.Drawing.Point(0, 0);
        this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
        this.webBrowser1.Name = "webBrowser1";
        this.webBrowser1.Size = new System.Drawing.Size(527, 309);
        this.webBrowser1.TabIndex = 0;
        this.webBrowser1.Url = new System.Uri("http://cms.nust.edu.pk/psp/ps/?cmd=login", System.UriKind.Absolute);
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.Location = new System.Drawing.Point(0, 0);
        this.splitContainer1.Name = "splitContainer1";
        this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.button1);
        this.splitContainer1.Panel1.Controls.Add(this.label2);
        this.splitContainer1.Panel1.Controls.Add(this.label1);
        this.splitContainer1.Panel1.Controls.Add(this.textBox2);
        this.splitContainer1.Panel1.Controls.Add(this.textBox1);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.webBrowser1);
        this.splitContainer1.Size = new System.Drawing.Size(527, 398);
        this.splitContainer1.SplitterDistance = 85;
        this.splitContainer1.TabIndex = 1;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(330, 31);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 4;
        this.button1.Text = "Insert";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(149, 61);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(53, 13);
        this.label2.TabIndex = 3;
        this.label2.Text = "Password";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(149, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(55, 13);
        this.label1.TabIndex = 2;
        this.label1.Text = "Username";
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(208, 58);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(100, 20);
        this.textBox2.TabIndex = 1;
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(208, 12);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(527, 398);
        this.Controls.Add(this.splitContainer1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel1.PerformLayout();
        this.splitContainer1.Panel2.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.WebBrowser webBrowser1;
    private System.Windows.Forms.SplitContainer splitContainer1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox1;
}
  }
Usama Khan
  • 15
  • 4
  • Which exact object reference (on which row) is giving you the error? – D. Petrov May 26 '16 at 14:23
  • {"Object reference not set to an instance of an object."} Sorry typing mistake in haste. This is the exact exception – Usama Khan May 26 '16 at 14:28
  • I know what the error message is, I'm asking you which of those three rows in this block is giving you the error, so that we could know which object to pay attention to. – D. Petrov May 26 '16 at 14:30
  • count = 0 in the exception details. I have only 1 textbox at this moment. No row no column – Usama Khan May 26 '16 at 14:30
  • Its giving exception at doc.GetElementbyId(). – Usama Khan May 26 '16 at 14:31
  • Which means that your 'doc' variable is not set to an instance of an object. With other words `this.webBrowser1.Document` is returning null. Therefore it's either the `webBrowser1` that's not a valid instance or there's no `.Document` present. You should either check it out in details by yourself or just provide a bigger bit of code for us to help. – D. Petrov May 26 '16 at 14:32
  • So sir what to do with this exception. ? anything ? – Usama Khan May 26 '16 at 14:34
  • Look carefuly trough your code and check out which of those two instances I've mentioned above is missing. One of them is returning null and causing your `doc` to be not a valid object reference. – D. Petrov May 26 '16 at 14:35
  • You need to wait for the document to load completely in the WebBrowser control. What happens if you wait for about a minute before clicking your `button1`? – Pradeep Kumar May 26 '16 at 14:35
  • @PradeepKumar Yes, that should be the most probable reason. But he has to actually get into the meaning of the error itself to reach the same consequence. – D. Petrov May 26 '16 at 14:40
  • let me check this through. I have attached my disgner code. in case u find something there. – Usama Khan May 26 '16 at 14:41
  • Same error after 4 minutes as well @pradeepKumar – Usama Khan May 26 '16 at 14:47
  • @D.Petrov anything sir. i have checked everything is ok in there. Both are not null – Usama Khan May 26 '16 at 15:05
  • I haven't been busying myself with online coding so far, but I can guess your '.Document' element of your browser is not being set correctly.. – D. Petrov May 26 '16 at 15:09
  • @D.Petrov sir how shud i set it. believe you me. i am a begginer. learning. and i am stuck in this thing :( i have searched the heck out of google. – Usama Khan May 26 '16 at 15:18
  • Can you see the web page loaded in your WebBrowser control before you click the button? – Pradeep Kumar May 26 '16 at 15:26
  • Yes, It working Perfectly fine. . Fast and furious @pradeepKumar – Usama Khan May 26 '16 at 15:29
  • One more thing @pradeepKumar The Website i am using to learn has username and password. IT works fine in their. but when logged in, it doesnot work on any textbox – Usama Khan May 26 '16 at 15:30
  • "doesnot work" could have a lot of meanings. You would need to be more clear what doesn't work. You may want to open a new question since this one is already resolved, I believe. – Pradeep Kumar May 26 '16 at 15:35
  • There might be a chance that the webpage i am trying have some issue. ? can u look at the inspect code? – Usama Khan May 26 '16 at 15:35
  • @pradeepKumar "does not work" means its throwing exception on the webpages other than the login page. and the problem is still there. ! – Usama Khan May 26 '16 at 15:36
  • I just inspected the page. I can't find anything named `DERIVED_LAM_GRADE_1$0` in its source. You may have got the markup wrong. Please re-check. After running the application, you can right-click on your WebBrowser control and click on `View Source` to get the source of the page. – Pradeep Kumar May 26 '16 at 15:41
  • the element i am trying is on a page needs login. – Usama Khan May 27 '16 at 03:35

0 Answers0