-2

I can not understand the logic behind the connectionString command. I am attempting to make a username registration page that checks the availability of a user name before submitting. I have followed my ASP.NET unleashed textbook to a tee, but still I get the "object reference not set" error. Do I have to name my database and table accordingly here or am I missing something else? I have been searching the internet like crazy trying to make sense of this, but I am just confusing myself. Any advise would be greatly appreciated.

Here are my ShowAjaxValidator.aspx and AjaxValidator.cs pages:

<%@ Page Language="C#" %>
<%@ Register TagPrefix="custom" Namespace="myControls" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    /// <summary>
    /// Validation function that is called on both the client and server
    /// </summary>
    protected void AjaxValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (UserNameExists(args.Value))
            args.IsValid = false;
        else
            args.IsValid = true;
    }

    /// <summary>
    /// Returns true when user name already exists
    /// in Users database table
    /// </summary>
    private bool UserNameExists(string userName)
    {
        string conString =
WebConfigurationManager.ConnectionStrings["UsersDB"].ConnectionString;
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName=@UserName", con);
        cmd.Parameters.AddWithValue("@UserName", userName);
        bool result = false;
        using (con)
        {
            con.Open();
            int count = (int)cmd.ExecuteScalar();
            if (count > 0)
                result = true;
        }
        return result;
    }

    /// <summary>
    /// Insert new user name to Users database table
    /// </summary>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string conString =
WebConfigurationManager.ConnectionStrings["UsersDB"].ConnectionString;
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand("INSERT Users (UserName,FavoriteColor) VALUES (@UserName,@FavoriteColor)", con);
        cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
        cmd.Parameters.AddWithValue("@FavoriteColor", txtFavoriteColor.Text);
        using (con)
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        txtUserName.Text = String.Empty;
        txtFavoriteColor.Text = String.Empty;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show AjaxValidator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:Label
        id="lblUserName"
        Text="User Name:"
        AssociatedControlID="txtUserName"
        Runat="server" />
    <asp:TextBox
        id="txtUserName"
        Runat="server" />
    <custom:AjaxValidator
        id="AjaxValidator1"
        ControlToValidate="txtUserName"
        Text="User name already taken!"
        OnServerValidate="AjaxValidator1_ServerValidate"
        Runat="server" />

    <br /><br />
    <asp:Label
        id="lblFavoriteColor"
        Text="Favorite Color:"
        AssociatedControlID="txtFavoriteColor"
        Runat="server" />
    <asp:TextBox
        id="txtFavoriteColor"
        Runat="server" />

    <br /><br />
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" OnClick="btnSubmit_Click" />

    </div>
    </form>
</body>
</html>

AjaxValidator.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace myControls
{
    /// <summary>
    /// Enables you to perform custom validation on both the client and server
    /// </summary>
    public class AjaxValidator : BaseValidator, ICallbackEventHandler
    {
        public event ServerValidateEventHandler ServerValidate;

        string _controlToValidateValue;

        protected override void OnPreRender(EventArgs e)
        {
            String eventRef = Page.ClientScript.GetCallbackEventReference
              (
                this,
                "",
                "",
                ""
              );

            // Register include file
            String includeScript =
                  Page.ResolveClientUrl("~/ClientScripts/AjaxValidator.js");
            Page.ClientScript.RegisterClientScriptInclude("AjaxValidator",
                  includeScript);

            // Register startup script
            String startupScript =
String.Format("document.getElementById('{0}').evaluationfunction = 'AjaxValidatorEvaluateIsValid';", this.ClientID);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "AjaxValidator", startupScript, true);

            base.OnPreRender(e);
        }

        /// <summary>
        /// Only do the AJAX on browsers that support it
        /// </summary>
        protected override bool DetermineRenderUplevel()
        {
            return Context.Request.Browser.SupportsCallback;
        }

        /// <summary>
        /// Server method called by client AJAX call
        /// </summary>
        public string GetCallbackResult()
        {
            return ExecuteValidationFunction(_controlToValidateValue).ToString();
        }

        /// <summary>
        /// Return callback result to client
        /// </summary>
        public void RaiseCallbackEvent(string eventArgument)
        {
            _controlToValidateValue = eventArgument;
        }

        /// <summary>
        /// Server-side method for validation
        /// </summary>
        protected override bool EvaluateIsValid()
        {
            string controlToValidateValue =
this.GetControlValidationValue(this.ControlToValidate);
            return ExecuteValidationFunction(controlToValidateValue);
        }

        /// <summary>
        /// Performs the validation for both server and client
        /// </summary>
        private bool ExecuteValidationFunction(String controlToValidateValue)
        {
            ServerValidateEventArgs args = new ServerValidateEventArgs (controlToValidateValue, this.IsValid);
            if (ServerValidate != null)
                ServerValidate(this, args);
            return args.IsValid;
        }

    }

}

The error I am running into points to the part of the .aspx file that says:

string conString =
WebConfigurationManager.ConnectionStrings["UsersDB"].ConnectionString;

The error says that a null exception was unhanded by the user and "Object reference not set to an instance of an object."

I tried to add the connectionStrings to the web.config file, but I am not sure if it is using the correct syntax. Could you please tell me if this is what you were saying:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="UsersDB"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=.\SQLExpress;
                    AttachDbFilename=|DataDirectory|UsersDB.mdf;
                    Integrated Security=True;User Instance=True" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>

I am now getting an SqlException that says: An attempt to attach an auto-named database for file C:\Users\Mike\Documents\Visual Studio 2010\WebSites\Validation_Attempt\App_Data\UsersDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

I am very new to ASP and C#. Edit for bronze.

MUlferts
  • 1,310
  • 2
  • 16
  • 30
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Mar 07 '14 at 00:08
  • I understand that other cases are similar to mine, but I am very new to asp and C#, and I could not comprehend the other threads being that they were not my exact situation. Eric Noren's answer was exactly what I needed to put me in the right direction, so this thread was worth it's weight in gold for me. – MUlferts Mar 07 '14 at 15:22

1 Answers1

1

It would seem you're trying to read a configuration value that doesn't exist and then accessing a property on that null object.

You would need a connectionStrings node in your web.config file which had a key of UsersDB which had the connection string for your database. Without it, that object is null and calling ConnectionString property throws the null reference exception. Once you resolve the setting, this property will work correctly.

Erik Noren
  • 4,279
  • 1
  • 23
  • 29
  • I have added a web.config file with included in my question. I am getting a new error now. Could you tell me what I am doing wrong with this command please? – MUlferts Mar 07 '14 at 00:00
  • Never mind, I figured it out thanks to your help! Thank you very much. All I had to do after adding the connectionStrings node was create the correct database and table names. It is currently working just as it should. – MUlferts Mar 07 '14 at 00:13