未将对象引用设置到对象的实例。
当单击登录的时候,在if (Request.Cookies["CheckCode"].Value == code)提示:未将对象引用设置到对象的实例。
这些代码是从书里面抄的,但是好像也是没有找到CheckCode这个控件?
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2007 login test.aspx.cs" Inherits="_2007_login_test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
密 码:<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox>
<br />
验证码:<asp:TextBox ID="txtCode" runat="server" Width="148px"></asp:TextBox>
<asp:Image ID="imgCode" runat="server"/>
<br />
<asp:LinkButton ID="likbtnRegister" runat="server" CausesValidation="False"
PostBackUrl="~/Register.aspx">注册</asp:LinkButton>
<br />
<asp:Button ID="btnLog" runat="server" onclick="btnLog_Click" Text="登录" />
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Security;
using System.Web.Security;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _2007_login_test : System.Web.UI.Page
{
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["MichaelConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLog_Click(object sender, EventArgs e)
{
string code = txtCode.Text;
if (Request.Cookies["CheckCode"].Value == code)///未将对象引用设置到对象的实例。
{
SqlConnection con = GetConnection();
con.Open();
string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUserPwd.Text, "MD5");
string sqlSel = "select count(*) from tb_userInfo where userName=@username and userPass=userpass";
SqlCommand com = new SqlCommand(sqlSel, con);
com.Parameters.Add(new SqlParameter("username", SqlDbType.VarChar, 20));
com.Parameters["username"].Value = txtUserName.Text;
com.Parameters.Add(new SqlParameter("userpass", SqlDbType.VarChar, 50));
com.Parameters["userpass"].Value = pass;
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Hello1","<script>alert('登录成功')</script>");
txtCode.Text = txtUserName.Text = "";
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello2", "<script>alert('用户名或密码错误')</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Hello3", "<script>alert('验证码输入错误')</script>");
}
}
}