当前位置:编程学习 > C#/ASP.NET >>

哪个哥哥能告诉下我,我这些哪错了,应该事件成功有个提示脚本,但是没效果,数据库也没添加进去!多谢各位哥哥们!

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class bookwriter : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (this.IsValid)//如果验证控件验证合法,则执行下面代码
        {
            string posttime;//定义过程变量
            posttime = System.DateTime.Now.ToString();//获取当前时间
            int face = Convert.ToInt32(Request.Form["face"].ToString());//获取表情图标的编号



            SqlConnection conn; //定义数据库连接字符串
            conn = new SqlConnection(Session["getsion"].ToString());//获取数据库路径
            conn.Open();//打开数据库
            try
            {
                //定义数据库插入操作字符串,要注意单、双引号的使用
                string mysql = "insert into book(title,bookuser,address,telephone,qq,mail,url,face,content,posttime) values ('" + this.title.Text + "','" + this.bookuser.Text + "','" + this.address.Text + "','" + this.telephone.Text + "','" + this.qq.Text + "','" + this.mail.Text + "','" + this.url.Text + "'," + face + ",'" + this.content.Text.ToString().Replace("\r\n", "<br>") + "','" + posttime + "')";
                SqlCommand comm = new SqlCommand(mysql, conn);//定义SqlCommand命令对象
                comm.ExecuteNonQuery();//执行数据库插入操作
                Response.Write("<script>alert('留言成功添加');</script>");//提示成功信息
                Response.Redirect("default.aspx");//返回首页
            }
            catch (Exception ex)//捕捉错误信息
            {
                Response.Write(ex.ToString());//输出错误信息
            }
            finally
            {
                conn.Close();//关闭数据库连接
            }
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        this.title.Text = "";
        this.bookuser.Text = "";
        this.address.Text = "";
        this.qq.Text = "";
        this.mail.Text = "";
        this.url.Text = "";
        this.content.Text = "";
        this.telephone.Text = "";

    }
}
--------------------编程问答-------------------- SqlCommand comm = new SqlCommand(mysql, conn);//定义SqlCommand命令对象


这一行放一个断点    执行一下 获取mysql语句  放到数据库里查询执行一下 就知道了啊 --------------------编程问答-------------------- if (this.IsValid)没通过 不然会跑出异常的 --------------------编程问答--------------------
引用 2 楼 psysic0824 的回复:
if (this.IsValid)没通过 不然会跑出异常的
+

楼主开始学么?第一步,你要学会调试 --------------------编程问答-------------------- 不会吧!我验证控件都过了啊,也没报异常啊! --------------------编程问答-------------------- 调试一下  把sql语句 贴到数据库 看一下 有什么错误没有
--------------------编程问答-------------------- 是不是写错了,具体调试跟踪下就出来了. --------------------编程问答-------------------- 先分层 好乱 --------------------编程问答-------------------- Session["getsion"].ToString()字符串对么
mysql 的执行语句对么 --------------------编程问答-------------------- public partial class bookwriter : System.Web.UI.Page
{
    public string Addtime;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
   

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
        this.ImageButton2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);

    }
    #endregion
加上这些就对了,一个方法是相加,另外一个是重写方法,谁能解释一下呢 --------------------编程问答-------------------- 你用的那个版本的VS啊? --------------------编程问答-------------------- 放进global配置里面从web。config中读取的数据库连接字符串放进session-start里面调用! --------------------编程问答-------------------- vs2008 --------------------编程问答-------------------- 谢谢大家,问题自己解决了,调试都是通过的,窗体加载的时候就是page-load(){}
下加上
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
        this.ImageButton2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);
解决! 多谢各位哥哥们! --------------------编程问答-------------------- .....................晕 --------------------编程问答-------------------- .net1.x
这家伙 --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,