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

public partial class manage_Inputproduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        if (!Page.IsPostBack)
        {
           
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string title = txtTitle.Text.ToString().Trim();
        string content = FreeTextBox1.Text.ToString();
        string author = txtAuthor.Text.ToString();
        string chuchu = drpChuchu.SelectedValue.ToString();
        string leibie = DrpLeibie.SelectedValue.ToString();
        if (title.Length < 1 || content.Length < 1)
        {
            lblError.Text = "标题和内容不能为空，请认真填写！";
            return;
        }
        int nResult = Insertnews(title, content, author, chuchu, leibie);
        if (nResult == 1)
        {
            lblError.Text = "新闻添加成功！";
            txtAuthor.Text = "";
            txtTitle.Text = "";
            FreeTextBox1.Text = "";
        }
        else
        {
            lblError.Text = "添加失败！";
        }
    }
    public int Insertnews(string sTitle, string sContent, string sAuthor,string sChuchu,string sLeibie)
    {
        int nResult = -1;
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "INSERT INTO Mynews(Title,Content,Author,Chuchu,Leibie,Createdate) VALUES('" +
            sTitle + "','" +
            sContent + "','" +
            sAuthor + "','" +
            sChuchu + "','" +
            sLeibie + "',GetDate())";
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            nResult = myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return nResult;
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }

        }
    }
}
