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_Inputnote : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        if (!Page.IsPostBack)
        {
            Bindnote();
        }
    }
    private void Bindnote()
    {
        SqlDataReader dr = Getnote();
        if (dr.Read())
        {
            txtNote.Text = dr["Content"].ToString();
        }
        dr.Close();
    }
    public SqlDataReader Getnote()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "SELECT Content FROM Webnote WHERE Webnote=1";
        SqlDataReader dr = null;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            dr=myCommand.ExecuteReader(CommandBehavior.CloseConnection);

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    public int Updatenote(string sContent)
    {
        int nResult = -1;
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "UPDATE Webnote SET Content='" + sContent + "' WHERE Webnote=1";
        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;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int nResult = Updatenote(txtNote.Text.ToString());
        if (nResult == 1)
        {
            lblError.Text = "发布成功！";
        }
        else
        {
            lblError.Text = "发布失败!";
        }
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }

        }
    }
}
