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 free.bbs
{
	/// <summary>
	/// Summary description for fabiaowenzhang.
	/// </summary>
	public class fabiaowenzhang : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label cuowu;
		protected System.Web.UI.WebControls.TextBox biaoti;
		protected System.Web.UI.WebControls.DropDownList leibie;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.TextBox neirong;
		protected System.Web.UI.WebControls.Button tijiao;
		protected System.Web.UI.WebControls.Button Button1;
	    int shu1,shu2,shu3,shu4;
		protected System.Data.SqlClient.SqlConnection myconn;
		protected System.Data.SqlClient.SqlDataAdapter mycommand;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				if(Request.Cookies["free_huiyuan"].Value=="dengchu")
					Response.Redirect("error.aspx");
				TextBox1.Text=Request.Cookies["free_huiyuan"].Value;
			}
			catch
			{
				Response.Redirect("error.aspx");
			}
			myconn=new SqlConnection(Session["str_connection"].ToString());
			string sqlstr="select neirong  from ziliao_kind ";
			mycommand=new SqlDataAdapter(sqlstr,myconn);
			DataSet myset=new DataSet();
			myconn.Open();
			mycommand.Fill(myset,"leixing");
			if (!IsPostBack) 
			{
				for(int i=0;i<=myset.Tables[0].Rows.Count-1;i++)
				{
					leibie.Items.Add(myset.Tables["leixing"].Rows[i]["neirong"].ToString());
				}
			}
			myconn.Close();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.tijiao.Click += new System.EventHandler(this.tijiao_Click);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void tijiao_Click(object sender, System.EventArgs e)
		{
			string sqlstr="select id  from ziliao_kind where neirong='"+leibie.SelectedValue.Trim().ToString()+"'";
			SqlCommand mycomm=new SqlCommand(sqlstr,myconn);
			SqlDataReader myreader;
			myconn.Open();
			myreader=mycomm.ExecuteReader();
			
			string lei;
			if(myreader.Read())
			{
				lei=myreader.GetValue(0).ToString();
				myreader.Close();
				if(biaoti.Text==""||neirong.Text=="")
				{
					Response.Write("<script>alert('标题、内容都不能为空！')</script>");
				}
				else
				{
					DateTime sytime=DateTime.Now;
					sqlstr="INSERT article (biaoti,chuqu,neirong,shijian,leixing,counter,flag) VALUES (@biaoti,@chuqu,@neirong,@shijian,@leixing,1,0)";
					mycomm=new SqlCommand(sqlstr,myconn);
					mycomm.Parameters.Add(new SqlParameter("@biaoti", SqlDbType.NVarChar, 50));
					mycomm.Parameters["@biaoti"].Value =htmlencode(biaoti.Text);

					mycomm.Parameters.Add(new SqlParameter("@chuqu", SqlDbType.NVarChar, 30));
					mycomm.Parameters["@chuqu"].Value = htmlencode(TextBox1.Text);

					mycomm.Parameters.Add(new SqlParameter("@neirong", SqlDbType.NText));
					mycomm.Parameters["@neirong"].Value =htmlencode(neirong.Text.ToString());

					mycomm.Parameters.Add(new SqlParameter("@shijian", SqlDbType.DateTime, 8));
					mycomm.Parameters["@shijian"].Value =sytime;

					mycomm.Parameters.Add(new SqlParameter("@leixing", SqlDbType.Char, 2));
					mycomm.Parameters["@leixing"].Value = htmlencode(lei);
					
					try 
					{
						mycomm.ExecuteNonQuery();
						biaoti.Text="";
						TextBox1.Text="";
						neirong.Text="";

						if(mycomm.ExecuteNonQuery()==1)
						{				
					
							string strsle="select jifen,wenzhangshu,jinqian,meili from huiyuan WHERE yonghuming='"+Request.Cookies["free_huiyuan"].Value+"'";
							mycomm=new SqlCommand(strsle,myconn);
							SqlDataReader myread=mycomm.ExecuteReader();
							try
							{
								if(myread.Read())
								{
									shu1=(int)myread.GetInt32(0)+15;
									shu2=(int)myread.GetInt32(1)+1;
									shu3=(int)myread.GetInt32(2)+10;
									shu4=(int)myread.GetInt32(3)+2;
								}
								myread.Close();

							}

							catch
							{ 
								myread.Close();
							}
							string strupd="UPDATE huiyuan SET jifen="+shu1+",wenzhangshu="+shu2+",jinqian="+shu3+",meili="+shu4+" WHERE yonghuming='"+Request.Cookies["free_huiyuan"].Value+"'";
							mycomm=new SqlCommand(strupd,myconn);
							mycomm.ExecuteNonQuery();
						}
						Response.Write("<script>alert('已添加记录！')</script>");

						
					}
					catch (SqlException ee )
					{
						if (ee.Number == 2627)
							Response.Write("<script>alert('错误：已存在具有相同主键的记录！')</script>");
						else
							Response.Write("<script>alert('错误：未能添加记录，请确保正确填写了字段！')</script>");
					}	
				}
				myconn.Close();
			}
			else
			{
				myreader.Close();
				myconn.Close();
			}
		}
		private string htmlencode(string str)
		{
			str=Server.HtmlEncode(str);
			str=str.Replace("\r\n","<br>");
			char ch;
			ch=(char)32;
			str=str.Replace(ch.ToString(),"");
			return str;
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("default.aspx");
		}
	}
}
