sql字符串语句出现
未将对象引用设置到对象的实例 是什么原因?
C# code
protected void ButtonCommit_Click(object sender, EventArgs e)
{
string sqlText = "";
string contentText = this.TextBoxContent.Text.Trim();
switch (m_state)
{
case "create": //创建帖子
if (FindTitle())
{
Response.Write("<script>window.alert('库中已经有要发布的主题,无法重复发布!');</script>");
return;
}
else
{
sqlText = "INSERT INTO mainforum(title,text,author,releasetime) VALUES('"
+ this.TextBoxTitle.Text.Trim() + "','"
+ contentText + "','"
+ Membership.GetUser().UserName + "','"
+ DateTime.Now.ToString() + "')";
ExecuteSql(sqlText);
Response.Redirect("forum.aspx");
}
break;
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息:
System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 147: else
行 148: {
行 149: sqlText = "INSERT INTO mainforum(title,text,author,releasetime) VALUES('"
行 150: + this.TextBoxTitle.Text.Trim() + "','"
行 151: + contentText + "','"
源文件: f:\YUDAO\projects\MySimpleBBS\ArticleModify.aspx.cs 行: 149
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。] ArticleModify.ButtonCommit_Click(Object sender, EventArgs e) in f:\YUDAO\projects\MySimpleBBS\ArticleModify.aspx.cs:149
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
------解决方案--------------------
应该Membership为null了吧
------解决方案--------------------
Membership.GetUser().UserName这当中有null值。