回 帖 发 新 帖 刷新版面

主题:请求高手帮帮忙!菜鸟的简单开发日志最后一步了,回答加分+感激

数据库:
webdevelop
表:
mainforum
字段
id                 int                主健
title              nvarchar
content           nvarchar
author           nvarchar
releasetime      datetime

回复列表 (共6个回复)

沙发

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;


namespace WebApplication1
{
    /// <summary>
    /// modifyforum 的摘要说明。
    /// </summary>
    public class modifyforum : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.TextBox TextBoxTitle;
        protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator1;
        protected System.Web.UI.WebControls.TextBox TextBoxContent;
        protected System.Web.UI.WebControls.Label LabelMess;
        protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator2;
        protected System.Web.UI.WebControls.Button ButtonOK;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            message=Request.QueryString ["mess"];
            switch(message)
            {
                case"0": //发布信息
                {
                    this.LabelMess.Text="发布信息";
                    this.TextBoxTitle.ReadOnly=false;
                    break;

                }

                case"1"://修改论坛主题
                {
                    
                    this.LabelMess.Text="修改信息";
                    SqlConnection conn=new SqlConnection(MyClass.ConnString);
                    SqlCommand command=new SqlCommand("select * from mainforum where id="
                        +Session["mainid"].ToString(),conn);
                    conn.Open();
                    SqlDataReader rd=command.ExecuteReader();
                    if(rd.Read())
                    {
                        if(rd["author"].ToString().Trim()==Session["userName"].ToString())
                        {
                            this.TextBoxTitle.ReadOnly=false;
                            this.TextBoxTitle.BackColor=Color.White;
                            if(!Page.IsPostBack)
                            {
                                this.TextBoxTitle.Text=Session["zhuti"].ToString();
                                this.TextBoxContent.Text=rd["content"].ToString();
                            }
                        }
                        else
                        {
                            rd.Close();
                            conn.Close();
                            Response.Write("<script>"
                                +"window.alert('抱歉,你不是原作者,不能修改!"
                                +"');</script>");
                            Server.Transfer("subforum.aspx");
                        }
                    }
                    else
                    {
                        Response.Write("<script>window.alert('无此记录!');</script>");
                    }
                    rd.Close();
                    conn.Close();
                    break;
                }

板凳

case"2"://修改论坛回复
                {
                    subid=Request.QueryString["subid"];
                    this.LabelMess.Text="修改信息";
                    SqlConnection conn=new SqlConnection(WebApplication1.MyClass.ConnString);
                    SqlCommand command=new SqlCommand("select * from subforum where id="
                        +subid,conn);
                    conn.Open();
                    SqlDataReader rd=command.ExecuteReader();
                    if(rd.Read())
                    {
                        if(rd["author"].ToString().Trim()==Session["userName"].ToString())
                        {
                            this.TextBoxTitle.ReadOnly=true;
                            this.TextBoxTitle.BackColor=Color.Gray;
                            if(!Page.IsPostBack)
                            {
                                this.TextBoxTitle.Text=Session["zhuti"].ToString();
                                this.TextBoxContent.Text=rd["content"].ToString();
                            }
                        }
                        else
                        {
                            rd.Close();
                            conn.Close();
                            Response.Write("<script>"
                                +"window.alert('抱歉,你不是原作者,不能修改!');</script>");
                            Server.Transfer("subforum.aspx");
                        }
                    }
                    else
                    {
                        Response.Write("<script>window.alert('无此记录!');</script>");
                    }
                    rd.Close();
                    conn.Close();
                    break;
                }
                case "3":
                {
                    this.LabelMess.Text="发表见解";
                    this.TextBoxTitle.ReadOnly=true;
                    this.TextBoxTitle.BackColor=Color.Gray;
                    this.TextBoxTitle.Text=Session["zhuti"].ToString();
                    break;
                }
            }

        }

3 楼

private void InitializeComponent()
        {    
            this.ButtonOK.Click += new System.EventHandler(this.ButtonOK_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
        private string message;
        private string subid;


        private void ButtonOK_Click(object sender, System.EventArgs e)
        {
            string sss=this.TextBoxContent.Text.Trim();
            if(message=="0")
            {
                if(this.FindZhuTi()==true)
                {
                    Response.Write("<script>window.alert('库中已经有要发布的主题,"
                        +"无法重复发布!');</script>");
                    return;
                }
                else
                {
                    string str="insert into mainforum(title,content,author,releasetime)
values('"
                        +this.TextBoxTitle.Text.Trim()+"','"
                        +this.TextBoxContent.Text.Trim()+"','"
                        +Session["userName"].ToString()+"','"
                        +DateTime.Now.ToString()+"')";
                    ExecuteSql(str);
                    Response.Redirect("forum.aspx");
                }
            }
            if(message=="1")
            {
                string str="update mainforum set content='"+sss
                    +"',releasetime='"+DateTime.Now.ToString()
                    +"',title='"+this.TextBoxTitle.Text.Trim()
                    +"' where id="+Session["mainid"].ToString();
                ExecuteSql(str);
                Response.Redirect("subforum.aspx");
            }
            if(message=="2")
            {
                string subid=Request.QueryString["subid"];
                string str="update subforum set content='"
                    +this.TextBoxContent.Text.Trim()+"',lastmodifytime='"
                    +DateTime.Now.ToString()+"' where id="+subid;
                ExecuteSql(str);
                Response.Redirect("subforum.aspx");
            }
            if(message=="3")
            {
                if(this.FindNeiRong()==true)
                {
                    Response.Write("<script>window.alert('添加失败:"
                        +"库中已经有要添加的内容!');</script>");
                    return;
                }
                else
                {
                    string str="insert into subforum(titleid,content,lastmodifytime,author)
values('"
                        +Session["mainid"].ToString()+"','"
                        +this.TextBoxContent.Text.Trim()+"','"
                        +DateTime.Now.ToString()+"','"
                        +Session["userName"].ToString()+"')";
                    ExecuteSql(str);
                    Response.Redirect("subforum.aspx");
                }
            }

        }

4 楼

{
            SqlConnection conn=new SqlConnection(WebApplication1.MyClass.ConnString);
            conn.Open();
            SqlCommand command=new SqlCommand(sqlstr,conn);
            command.ExecuteNonQuery();
            conn.Close();

        }
        private bool FindZhuTi()
        {
            bool isFind=false;
            SqlConnection conn=new SqlConnection(WebApplication1.MyClass.ConnString);
            SqlCommand command=
                new SqlCommand("select id,title from mainforum order by title",conn);
            SqlDataReader rd=null;
            conn.Open();
            rd=command.ExecuteReader();
            while(rd.Read())
            {
                if(rd["title"].ToString().Trim()==this.TextBoxTitle.Text.Trim())
                {
                    isFind=true;
                    break;
                }
            }
            rd.Close();
            conn.Close();
            return isFind;
        }

        private bool FindNeiRong()
        {
            bool isFind=false;
            SqlConnection conn=new SqlConnection(WebApplication1.MyClass.ConnString);
            SqlCommand command=
                new SqlCommand("select id,content from subforum order by content",conn);
            SqlDataReader rd=null;
            conn.Open();
            rd=command.ExecuteReader();
            while(rd.Read())
            {
                if(rd["content"].ToString().Trim()==this.TextBoxContent.Text.Trim())
                {
                    isFind=true;
                    break;
                }
            }
            rd.Close();
            conn.Close();
            return isFind;
        }
    }

}



5 楼

运行错误



“/WebApplication1”应用程序中的服务器错误。
--------------------------------------------------------------------------------

无法将 NULL 值插入列 'id',表 'webdevelop.dbo.mainforum';该列不允许空值。
INSERT 失败。语句已终止。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该
错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SqlClient.SqlException: 无法将 NULL 值插入列 'id',表
'webdevelop.dbo.mainforum';该列不允许空值。INSERT 失败。语句已终止。

源错误:


行 222:            conn.Open();
行 223:            SqlCommand command=new SqlCommand(sqlstr,conn);
行 224:            command.ExecuteNonQuery();
行 225:            conn.Close();
行 226:


源文件: c:\documents and settings\administrator\桌面
\net\webapplication1\modifyforum.aspx.cs    行: 224

堆栈跟踪:


[SqlException: 无法将 NULL 值插入列 'id',表 'webdevelop.dbo.mainforum';该列不
允许空值。INSERT 失败。
语句已终止。]
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   WebApplication1.modifyforum.ExecuteSql(String sqlstr) in c:\documents and
settings\administrator\桌面\net\webapplication1\modifyforum.aspx.cs:224
   WebApplication1.modifyforum.ButtonOK_Click(Object sender, EventArgs e) in
c:\documents and settings\administrator\桌面
\net\webapplication1\modifyforum.aspx.cs:175
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573

6 楼

主键所在列是不允许为空值的
所以你必须要对该列插入数据,
关于ID的得到方法,你可以在查询语句之中写入,
还有就是也可以在后台代码之中以GUID的得到方法得到。
呵呵还有就是......
你很大方哈,那么多的发代码。

我来回复

您尚未登录,请登录后再回复。点此登录或注册