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

public partial class rl_admins_pg_NewsDetailsShow : System.Web.UI.Page
{
    //************************************
    SqlConnection con;
    //************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "جزئیات خبر";
            if (!Page.IsPostBack)
            {
                try
                {
                    string newsDate = "", subject = "", context = "", newsPicUrl = "", lastEditDate = "";
                    int numOfVisits = 0;
                    int newsId = Int32.Parse(Request.QueryString["newsId"].ToString());
                    string conString = RamanConnectionString.LocalConnectionString();
                    con = new SqlConnection(conString);
                    con.Open();
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "select newsDate, subject, context, numOfVisits, newsPicUrl, lastEditDate FROM t_News Where newsId = " + newsId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            newsDate = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            subject = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            context = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            numOfVisits = (rdr.GetValue(3) != DBNull.Value ? rdr.GetInt32(3) : 0);
                            newsPicUrl = (rdr.GetValue(4) != DBNull.Value ? rdr.GetString(4) : "");
                            lastEditDate = (rdr.GetValue(5) != DBNull.Value ? rdr.GetString(5) : "");
                        }
                    }
                    rdr.Close();
                    lblNewsId.Text = "کد خبر : " + newsId.ToString();
                    lblNewsDate.Text = "تاریخ خبر : " + newsDate;
                    lblSubject.Text = subject;
                    lblContext.Text = context;
                    lblNumOfVisits.Text = "تعداد بازدیدها از این خبر : " + numOfVisits.ToString();
                    lblLastEditDate.Text = "آخرین بروزرسانی : " + lastEditDate;
                    string filePath = Request.PhysicalApplicationPath + @"images\";
                    if (File.Exists(filePath + newsPicUrl))
                    {
                        imgNewsImage.ImageUrl = "~/images/" + newsPicUrl;
                    }
                    else
                    {
                        imgNewsImage.ImageUrl = "~/images/nophotosquare.gif";
                    }
                    numOfVisits++;
                    cmd.CommandText = "update t_News SET numOfVisits = " + numOfVisits + " Where newsId = " + newsId;
                    cmd.ExecuteNonQuery();
                }
                catch
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch(Exception exc)
        {
        }
        finally
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
}
