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;
using System.Configuration;
using System.IO;
namespace baogelyb
{
/// <summary>
/// guestbook 的摘要说明。
/// </summary>
public class guestbook : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox zuti;
protected System.Web.UI.WebControls.TextBox name;
protected System.Web.UI.WebControls.TextBox lianxidizhi;
protected System.Web.UI.WebControls.TextBox dianhua;
protected System.Web.UI.WebControls.TextBox qq;
protected System.Web.UI.WebControls.TextBox content;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected System.Web.UI.WebControls.ImageButton ImageButton2;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
    public string Addtime;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.dianhua.TextChanged += new System.EventHandler(this.dianhua_TextChanged);
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.ImageButton2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(this.IsValid)
{
Addtime=System.DateTime.Now.ToString();
// Addtime=System.DateTime.Now.ToLongTimeString();
//int face=Convert.ToInt32(Request.Form["face"].ToString());

// //取得数据库路径
// string dbPath=System.Configuration.ConfigurationSettings.AppSettings["bjdzqclyb"];
// //定义数据库连接字符串
// string ACconnStr="Provider=Microsoft.Jet.Sql.4.0;Data Source="+Server.MapPath(dbPath);
//创建数据库连接对像
SqlConnection ACconn=db.createconnection(); try
{
//打开数据库
ACconn.Open();
// // int SSID=Convert.ToInt32(SID.SelectedValue);
// //判断添加新闻是否已存在
// string ACsqlII="select * from bjdzqclyb where zuti!='"+this.zuti.Text+"'";
//// string ACsqlII="select * from bjdzqclyb";
// SqlCommand ACcommII=new SqlCommand(ACsqlII,ACconn);
// SqlDataReader ACdrII=ACcommII.ExecuteReader();
// // string y="";
// if(ACdrII.Read())
// {
//
//
// if(this.zuti.Text==ACdrII["zuti"].ToString())
// {
// // Response.Write("<script>alert('此信息已添加过');</script>");
// // Response.Write("<script>history.go(-2);</script>");
// Response.Redirect("Error/404b.htm");
// }
// }
// // else
// // {
//     
// // this.TextBox1.Text=y;
// ACdrII.Close();

// if (Convert.ToInt32(SID.SelectedValue)==0)
// {
string ACsqlI="insert into bjdzqclyb(zuti,name,lianxidizhi,dianhua,qq,content,Addtime) values ('"+this.zuti.Text+"','"+this.name.Text+"','"+this.lianxidizhi.Text+"','"+this.dianhua.Text+"','"+this.qq.Text+"','"+this.content.Text.ToString().Replace("\r\n","<br>")+"','"+Addtime+"')";

SqlCommand ACcommI=new SqlCommand(ACsqlI,ACconn);
ACcommI.ExecuteNonQuery();
Response.Write("<script>alert('留言成功添加');</script>"); // }
// else
// {
// string ACsqlI="insert into Article(ClassID,ChannelID,Title,LinkUrl,Content,Intro,Author,CopyFrom,MY_Site,MY_QQ,UpdateTime) values ('"+SID.SelectedValue+"','"+ChannelID.SelectedValue+"','"+Title.Text+"','"+LinkUrl.Text+"','"+Content.Text+"','"+Intro.Text+"','"+Author.Text+"','"+CopyFrom.Text+"','"+MY_Site.Text+"','"+MY_QQ.Text+"','"+Addtime+"')";
//
// SqlCommand ACcommI=new SqlCommand(ACsqlI,ACconn);
// ACcommI.ExecuteNonQuery();
// Response.Write("<script>alert('信息成功添加');</script>");
//
// }

 




}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
ACconn.Close();
}
}
} private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
this.zuti.Text="";
this.name.Text="";
this.lianxidizhi.Text="";
this.qq.Text="";
this.content.Text="";
} private void dianhua_TextChanged(object sender, System.EventArgs e)
{

}
}
}
System.NullReferenceException: 未将对象引用设置到对象的实例。
at baogelyb.guestbook.ImageButton1_Click(Object sender, ImageClickEventArgs e) in D:\Inetpub\wwwroot\blyb\guestbook.aspx.cs:line 120 请问我这里怎么改呢!http://www.cnat.cn/bjdzqclyb/guestbook.aspx这个是我做的留言板.可是点击发送后就显示上面的话....请指点一下小弟请留下QQ.小弟日后还需讨教

解决方案 »

  1.   

    this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);这句话的指向事件不存在。 
    也就是说不存在
    ImageButton1_Click(Object sender, ImageClickEventArgs e)
    事件。 
    解决方法删除this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);这一行。或者添加一个ImageButton1_Click(Object sender, ImageClickEventArgs e)
      

  2.   

    ClassID和ChannelID在数据库中是INT型吗
    如果是,试试下把122行改为
    string ACsqlI="insert into Article(ClassID,ChannelID,Title,LinkUrl,Content,Intro,Author,CopyFrom,MY_Site,MY_QQ,UpdateTime) values ("+SID.SelectedValue+","+ChannelID.SelectedValue+",'"+Title.Text+"','"+LinkUrl.Text+"','"+Content.Text+"','"+Intro.Text+"','"+Author.Text+"','"+CopyFrom.Text+"','"+MY_Site.Text+"','"+MY_QQ.Text+"','"+Addtime+"')";