用asp.net 做登录程序的时候出现在一个对我来说很奇怪的问题!错误提示如下:“/lonin”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 21: private void Page_Load(object sender, System.EventArgs e)
行 22: {
行 23: string username=Request.Form["username"].ToString(); //这时出错了,红色的!
行 24: string usnum=Request.Form.Get("usnum").ToString();
行 25: SqlConnection con=new SqlConnection("Server=.:database=login:uid=:pwd=:");
源文件: c:\inetpub\wwwroot\lonin\login.aspx.cs 行: 23 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
lonin.login.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\lonin\login.aspx.cs:23
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
我的源代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace lonin
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class login : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string username=Request.Form["username"].ToString();
string usnum=Request.Form.Get("usnum").ToString();
SqlConnection con=new SqlConnection("Server=.:database=login:uid=:pwd=:");
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from login where username='"+username+"'and usnum='"+usnum+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
Response.Redirect("1.htm");
}
else
{
Response.Redirect("2.htm"); }
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
}
}
}
我建一个login.htm 和一个login.aspx
在login.htm中放了两个文本框名称分别为:usname usnum 用户名/密码 还有一个button
login.htm的html代码为:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table width="500" height="174" border="0">
<tr>
<td width="175">用户名:</td>
<td width="315"><form name="form1" method="post" action="login.aspx"> //这里的action属性我并没有掉!
<input type="text" id="username">
</form>
</td>
</tr>
<tr>
<td>密码:</td>
<td><form action="" method="post" name="usnum" id="usnum">
<input type="text" name="textfield" id="usnum">
</form>
</td>
</tr>
<tr>
<td><form name="form2" method="post" action="">
<input type="submit" name="Submit" value="提交">
</form>
</td>
<td><form name="form3" method="post" action="">
<input type="reset" name="Submit" value="重置">
</form>
</td>
</tr>
</table>
</body>
</html>--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 21: private void Page_Load(object sender, System.EventArgs e)
行 22: {
行 23: string username=Request.Form["username"].ToString(); //这时出错了,红色的!
行 24: string usnum=Request.Form.Get("usnum").ToString();
行 25: SqlConnection con=new SqlConnection("Server=.:database=login:uid=:pwd=:");
源文件: c:\inetpub\wwwroot\lonin\login.aspx.cs 行: 23 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
lonin.login.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\lonin\login.aspx.cs:23
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
我的源代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace lonin
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class login : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string username=Request.Form["username"].ToString();
string usnum=Request.Form.Get("usnum").ToString();
SqlConnection con=new SqlConnection("Server=.:database=login:uid=:pwd=:");
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from login where username='"+username+"'and usnum='"+usnum+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
Response.Redirect("1.htm");
}
else
{
Response.Redirect("2.htm"); }
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
}
}
}
我建一个login.htm 和一个login.aspx
在login.htm中放了两个文本框名称分别为:usname usnum 用户名/密码 还有一个button
login.htm的html代码为:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table width="500" height="174" border="0">
<tr>
<td width="175">用户名:</td>
<td width="315"><form name="form1" method="post" action="login.aspx"> //这里的action属性我并没有掉!
<input type="text" id="username">
</form>
</td>
</tr>
<tr>
<td>密码:</td>
<td><form action="" method="post" name="usnum" id="usnum">
<input type="text" name="textfield" id="usnum">
</form>
</td>
</tr>
<tr>
<td><form name="form2" method="post" action="">
<input type="submit" name="Submit" value="提交">
</form>
</td>
<td><form name="form3" method="post" action="">
<input type="reset" name="Submit" value="重置">
</form>
</td>
</tr>
</table>
</body>
</html>--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
就是你的那个参数username没有传过来
你可以在地址栏看到传的值是什么,如果没值可能是?username=
如果你正在调试程序,请设置一个适当的开始页
^_^
你的后台文件是针对。aspx不是针对htm,所以当然取不到username文本框的值
所以那行会出错
加一属性name="username"
加一属性name="username"
加了,不行!
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>"><form name="form1" method="post" action="login.aspx"> //这里的action属性我并没有掉! <table width="500" height="174" border="0">
<tr>
<td width="175">用户名:</td>
<td width="315 <input type="text" id="username">
</td>
</tr>
<tr>
<td>密码:</td>
<td> <input type="text" name="textfield" id="usnum">
</td>
</tr>
<tr>
<td> <input type="submit" name="Submit" value="提交">
</td>
<td> <input type="reset" name="Submit" value="重置">
</td>
</tr>
</table></form> </body>
</html>
你在PageLoad里加一个
if(!Page.IsPostBack)
{
Request.From["..."]
......
}
不然你每次一运行页面的时候,就开始Request了!
而你的Request.Form["username"]得到的会是Null,
Request.Form["username"].ToString(); 这句就相当于Null.ToString()了,你说会不会包你那个错呢?
我主要终了两个错误:
第一:竟然把标点打错了,SqlConnection con=new SqlConnection("Server=.:database=login:uid=:pwd=:");
中的;(分号)我打成了冒号 一字之差导致没有能边上数据库
第二;那个登录窗口我是用Dreamweaver MX 2004做,我只接拖的控件,没有放表单!
希望跟我一样的初学者,以后就不要出我这样的错误了!
if(Request.Form["username"] != null)
{
username=Request.Form["username"].ToString(); //这时出错了,红色的!
}