积极答复者
调用自己写的类库,出现"未将对象引用设置到对象的实例"错误.求解!

问题
-
出现这个错误的. 但是,我用VS2005运行调试,就没有这个问题.搞了一整天都不行,麻烦各位老师指导一下.
“/”应用程序中的服务器错误。
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。] _Default.LoginButton_Click(Object sender, EventArgs e) +134 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 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) +5102
这个是网页代码:
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; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void LoginButton_Click(object sender, EventArgs e) { string username = string.Empty; string role = string.Empty; string sql = "select zhanghao,role from users where zhanghao='{0}' and mima='{1}'"; sql = string.Format(sql, Login1.UserName, Login1.Password); connsql sql2 = new connsql(); DataTable dt = sql2.getdataset(sql); if (dt.Rows.Count > 0) { username = dt.Rows[0][0] as string; role = dt.Rows[0][1] as string; } else { Login1.FailureText = "对不起,您输入的帐号或者密码错误!"; return; } Login1.FailureText = "登录成功"; } }
这个是放在APP_CODE里面的类
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.Data.SqlClient; /// <summary> /// connsql 的摘要说明 /// </summary> public class connsql { public connsql() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 判断帐号密码是否正确 /// </summary> /// <paramname="usersql">请输入SQL语句</param> /// <returns>返回一个数据集</returns> public DataTable getdataset(string usersql) { string connsql = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; SqlConnection userconn = new SqlConnection(); userconn.ConnectionString = connsql; DataSet ds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(usersql, userconn); try { sda.Fill(ds); } catch { return null; } userconn.Close(); userconn.Dispose(); return ds.Tables[0]; } }
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42
- 已编辑 QAL1988 2011年10月30日 7:52
答案
-
你好
或者你可以嘗試檢查一下getdataset 這個功能
會不會是因為 SQL 語句或Connection string 有問題所以他Return Null
或者你可以先嘗試加多一個IF Statement 去 看看是不是這個問題
E.G.
Please correct me if my concept is wrongDataTable dt = sql2.getdataset(sql); if(dt != null){ //<--加入這個If statement if (dt.Rows.Count > 0) { username = dt.Rows[0][0] as string; role = dt.Rows[0][1] as string; } else { Login1.FailureText = "对不起,您输入的帐号或者密码错误!"; return; } }
Chi- 已标记为答案 QAL1988 2011年10月31日 14:41
全部回复
-
你好
或者你可以嘗試檢查一下getdataset 這個功能
會不會是因為 SQL 語句或Connection string 有問題所以他Return Null
或者你可以先嘗試加多一個IF Statement 去 看看是不是這個問題
E.G.
Please correct me if my concept is wrongDataTable dt = sql2.getdataset(sql); if(dt != null){ //<--加入這個If statement if (dt.Rows.Count > 0) { username = dt.Rows[0][0] as string; role = dt.Rows[0][1] as string; } else { Login1.FailureText = "对不起,您输入的帐号或者密码错误!"; return; } }
Chi- 已标记为答案 QAL1988 2011年10月31日 14:41
-
改成粗體字
public DataTable getdataset(string usersql)
{
string connsql = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
SqlConnection userconn = new SqlConnection();
userconn.ConnectionString = connsql;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(usersql, userconn);
sda.Fill(ds);//這樣有可能還是沒有數據集,所以...
userconn.Close();
return ds.Tables.Count>0?ds.Tables[0]:new DataTable();
}
}
Shadowと愉快なコード達 -
你好QAL1988
两个意见:
1)F5然后调试你的程序,请列出究竟哪句话出问题了,便于判断。
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处