添加数据时出现的问题
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就总是提示上面的错误,请高手帮忙解决下, 谢谢