纠结的asp.net程序
using System;using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
//该源码下载自www.(51aspx.com)
public partial class Default : System.Web.UI.Page
{
static string sValidator = "";
private readonly string sValidatorImageUrl = "ValidateImage.aspx?Validator=";
protected void Page_Load(object sender, EventArgs e)
{
///添加页面初始化代码
if (!Page.IsPostBack)
{
sValidator = GetRandomint();
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
}
}
protected void LoginBtn_Click(object sender, EventArgs e)
{
///如果页面输入合法
if (Page.IsValid == true)
{
if (Validator.Text != sValidator)
{
Message.Text = "验证码输入错误,请重新输入验证码!!!";
sValidator = GetRandomint();
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
return;
}
String userId = "";
///定义类并获取用户的登陆信息
WebDBManage.User user = new WebDBManage.User();
SqlDataReader recu = user.GetUserLogin(UserName.Text.Trim(),Password.Text.Trim());
///判断用户是否合法
if (recu.Read())
{
userId = recu["UserID"].ToString();
}
recu.Close();
///验证用户合法性,并跳转到系统平台
if ((userId != null) && (userId != ""))
{
Session["UserID"] = userId;
//跳转到登录后的第一个页面
Response.Redirect("~/Main.aspx");
}
else
{
sValidator = GetRandomint();
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
///显示错误信息
Message.Text = "你输入的用户名称或者密码有误,请重新输入!";
}
}
}
protected void CancelBtn_Click(object sender, EventArgs e)
{
///清空用户名称和密码输入框
UserName.Text = Password.Text = "";
}
private String GetRandomint()
{
Random random = new Random();
return (random.Next(100000,999999).ToString());
}
}
我将这个系统下载好,用的是.NET3.5+SQL Server2005,我运行的时候,出现下列问题:
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。驱六
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 46:
行 47: ///判断用户是否合法
行 48: if (recu.Read())
行 49: {
行 50: userId = recu["UserID"].ToString();
源文件: e:\Inetpub\WebSite3\Default.aspx.cs 行: 48
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Default.LoginBtn_Click(Object sender, EventArgs e) in e:\Inetpub\WebSite3\Default.aspx.cs:48
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
这是 user.GetUserLogin 方法的代码,可能问题就出在这里。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using SQLHelper;
using System.Data.SqlClient;
using System.Text;
//5_1_a_s_p_x.c_o_m
namespace WebDBManage
{
/// <summary>
/// Summary description for User
/// </summary>
public class User
{
public static readonly int USERTYPESUPERADMIN = 0;
public static readonly int USERTYPEADMIN = 1;
public static readonly int USERTYPENORMAL = 2;
public SqlDataReader GetUserLogin(string sUserName, string sPassword)
{
///定义类SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
///定义保存从数据库获取的结果的DataReader
SqlDataReader dr = null;
///创建访问数据库的参数
SqlParameter[] paramList = {
sqlHelper.CreateInParam("@UserName",SqlDbType.VarChar,200,sUserName),
sqlHelper.CreateInParam("@Password",SqlDbType.VarChar,255,sPassword)
};
try
{
///执行存储过程
sqlHelper.RunProc("Pr_GetUserLogin", paramList, out dr);
}
catch (Exception ex)
{
///抛出执行数据库异常
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
///返回从数据库获取的结果
return (dr);
}
}
}
还有,在SQL Server中附加数据库时好像也不合适,哪位高手能帮帮我,为什么recu对象为空?告诉我怎么修改代码?怎么正确的附加数据库和修改配置文件?