Search code examples
c#htmlelements

How to figure out when a job number is not used


On our job database we have a http address that ends with "projects/Project/Entry/5893" The 5893 is the job number that will change job to job. I have a timer set to go through each number till it gets this page End Page. So on the End Page HtmlElement does not exist so it gives me the System.NullReferenceException and there for i know the last used job number. But the Problem is that the Exception does not pop up. Does anyone know an easier way to do this. Sorry for not showing the complete webpage address it has sensitive information.

    private int a = -1;
    private string NJNumber = File.ReadAllText(@"...\CurrentJobNumber.txt"); 
//The Last Confirmed Number by me and where to start searching from.

    private void NewJob_Click(object sender, EventArgs e)
    {
        HtmlDocument doc = nwJob.Document;

        a = Convert.ToInt32(NJNumber);
        JobNumberTimer.Start();
    }
    private void JobNumberTimer_Tick(object sender, EventArgs e)
    {
        HtmlDocument doc = nwJob.Document;

        string aJN = a.ToString();
        try
        {
            nwJob.Navigate("..../projects/Project/Entry/" + aJN);

            HtmlElement njname = doc.GetElementById("Name");
            a += 1;
        }
        catch(System.NullReferenceException)
        { lblJobNumber.Text = a.ToString();
            JobNumberTimer.Stop();
        }
    }

Solution

  • I don't think that HtmlDocument.GetElementById will throw a NullReferenceException.

    You could try and check the body of the the html doc for something on the error page.

    doc.Body.InnerText.Contains("somthing to search for")