未将对象引用设置到对象的实例。

myjieli 2012-04-18 04:32:52
当单击登录的时候,在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>");
}
}
}
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
虫子007 2012-04-18
  • 打赏
  • 举报
回复
<asp:Image ID="imgCode" runat="server"/>
这一块应该有个请求路径, 在请求的处理程序里 把 cookie 赋值, 现在你的cookie 根本就不存在!

你少写代码了!
应该添加 src="checkcode.ashx" 这样 ,checkcode.ashx处理程序中,画出一张 验证图片,并把 cookie 赋值!
EnForGrass 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
当单击登录的时候,在if (Request.Cookies["CheckCode"].Value == code)提示:未将对象引用设置到对象的实例。
这些代码是从书里面抄的,但是好像也是没有找到CheckCode这个控件?

前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2007 login test.aspx.c……
[/Quote]
Request.Cookies["CheckCode"]为null
cookie中有CheckCode吗?
mizuho_2006 2012-04-18
  • 打赏
  • 举报
回复
Request.Cookies["CheckCode"].Value == code

我才看代码
这是从Cookie里读取数据的,你没有往里面写怎么读出来呢
bdmh 2012-04-18
  • 打赏
  • 举报
回复
断点查看
Request.Cookies["CheckCode"].Value == code中是否有空对象,比如Request.Cookies["CheckCode"]是否存在
mizuho_2006 2012-04-18
  • 打赏
  • 举报
回复
因环境,路径,数据库等不同,同样的代码也可能得到不同的执行效果。

你只复制了代码,但是控件没有定义吧。

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧