标题:添加数据时出现的问题
取消只看楼主
klose1986
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-8-2
结帖率:0
 问题点数:0 回复次数:0 
添加数据时出现的问题
protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Session["userid"] = 0;
            string strID = Session["userid"].ToString();
            string strSubject = this.tbSubject.Text;
            string strMsg = this.tbMsg.Text;
            string strIP = Request.UserHostAddress.ToString();
            System.DateTime dateTime = DateTime.Now;
            string strDate = dateTime.ToString();
            string strSql = "insert into newpost(subject,userid,message,ip,posttime) values ('" + strSubject + "','" + strID + "','" + strMsg + "','" + strIP + "','" + strDate + "');";
            ExcuteSql(strSql);
            string strConn = "Data Source=admin;Initial Catalog=bbs;User ID=sa;Password=111";
            DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
            DbConnection dbConn = dbProviderFactory.CreateConnection();
            dbConn.ConnectionString = strConn;
            dbConn.Open();
            DbCommand dbComm = dbProviderFactory.CreateCommand();
            dbComm.Connection = dbConn;
             = "select postid from newpost where userid= '" + strID + "' and posttime= '" + dateTime + "'";
            IDataReader reader = dbComm.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(reader);
            int strPostID = (int)dt.Rows[0][0];
            reader.Close();
            dbConn.Close();
            dbConn.Dispose();
            Response.Write("新主题添加成功,请单击<a href='message.aspx?postid=" + strPostID + "'>进入主题</a>,或者单击<a href='bbs.aspx'>返回论坛</a>");
        }
    }
    private void ExcuteSql(string strSql)
    {
        string strConn = "Data Source=admin;Initial Catalog=bbs;User ID=sa;Password=111";
        DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
        DbConnection dbConn = dbProviderFactory.CreateConnection();
        dbConn.ConnectionString = strConn;
        dbConn.Open();
        DbCommand dbComm = dbProviderFactory.CreateCommand();
        dbComm.Connection = dbConn;
         = strSql;
        dbComm.ExecuteNonQuery();
        dbConn.Close();
        dbConn.Dispose();
    }
   
    protected void Button2_Click(object sender, EventArgs e)
    {
       Response.Redirect("bbs.aspx");
    }
}
这是BBS论坛中发表新主题的一段代码,运行后可以进入发表的界面,但是添加内容后报错“无法将 NULL 值插入列 'views',表 'bbs.dbo.newpost';该列不允许空值。INSERT 失败”。
发表新主题连接的数据库newpost表中包括postid(主键),subject,message,posttime,userid,ip,replies(回复次数),views(浏览次数)  都设置不为空。postid标识种子,replies和 views就总是提示上面的错误,请高手帮忙解决下, 谢谢
搜索更多相关主题的帖子: string 数据 ToString Session strID 
2008-08-03 09:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-226595-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.226156 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved