expenguin expenguin - 1 year ago 109
Vb.net Question

"Object reference not set to an instance of an object." when using BotDetect Captcha

I've used BotDetect before to utilize a captcha on an asp

, but now when I try to add BotDetect to another Asp
project using NuGet, I'm greeted with an 'Object Reference not set to an instance of an object' error upon debugging.

I cannot load my page nor get any breaks to occur. It seems to be failing when it tries to do an Application insights request GET of my form page. It then throws an unhanded exception in System.Web.dll

However, that being said, when I try to run he debugger, I'm still met with a 'Object Reference not set to an instance of an object' error, but it references
BotDetect.Web.UI.WebFormsCaptcha.OnInit(EventArgs e)

I've removed the BotDetect package, re-added the assemblies, tried to add the assemblies from the working project, and I've tried to recreate the project from the ground up to see if I maybe had mis-configured something. Nothing seems to work.

I currently have
from the NuGet package (version 4.0.1) and I've checked to make sure I'm using the .net 4.5 versions with my .net 4.5 project.

I've checked my Packages.config and I can see:

<package id="Captcha" version="4.0.1" targetFramework="net452" />

Below is my code that references the BotDetect Captcha. It is a MasterPage web form, but the Captcha resides in a content placeholder since we interact with it after the master has been rendered.

I'm just not sure what else I can do to fix this, any suggestions?

ASP Form Page

<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<legend>ASP.NET WebForm CAPTCHA Validation</legend>
<p class="prompt">
<label for="CaptchaCodeTextBox">Retype the characters from the picture:</label></p>
<BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha"
UserInputControlID="CaptchaCodeTextBox" />
<div class="validationDiv">
<asp:TextBox ID="CaptchaCodeTextBox" runat="server"></asp:TextBox>
<asp:Button ID="ValidateCaptchaButton" runat="server" />
<asp:Label ID="CaptchaCorrectLabel" runat="server" CssClass="correct"></asp:Label>
<asp:Label ID="CaptchaIncorrectLabel" runat="server" CssClass="incorrect"></asp:Label>


Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
' initial page setup
If Not IsPostBack Then
' set control text
ValidateCaptchaButton.Text = "Validate"
CaptchaCorrectLabel.Text = "Correct!"
CaptchaIncorrectLabel.Text = "Incorrect!"

' these messages are shown only after validation
CaptchaCorrectLabel.Visible = False
CaptchaIncorrectLabel.Visible = False
End If

If IsPostBack Then
' validate the Captcha to check we're not dealing with a bot
Dim isHuman As Boolean
isHuman = ExampleCaptcha.Validate()
If isHuman Then
CaptchaCorrectLabel.Visible = True
CaptchaIncorrectLabel.Visible = False
CaptchaCorrectLabel.Visible = False
CaptchaIncorrectLabel.Visible = True
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim HF As HiddenField = DirectCast(Page.Master.FindControl("HF1"), HiddenField)

If Not IsPostBack Then
ElseIf HF.Value = "AddNewRow" Then
End If
End Sub

Answer Source

So I've since spoken to the devs of BotDetect and it turned out that this is in fact a bug with their current release.

You currently cannot set the UserInputControlID property in a content page (Web form with master page) with BotDetect (v4.0.1). There will be a patch for this in the next version.

In the meantime, you can set it programmatically in the codebehind (VB).

CODE BEHIND (in page_prerender)

ExampleCaptcha.UserInputID = CaptchaCodeTextBox.ClientID


<BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download