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.