ACTIVATES AND ADD ARTICLES
Today we will see how to make article page and to shoe on the webpage only after activation (this can be done by the admin of the website).
Step1:THE CODE DESIGN:
We will create the simple design textboxes to enter necessary
details for the articles :
<asp:Label ID="Label2"
runat="server"
Font-Bold="True"
Font-Italic="True"
Font-Size="Larger" ForeColor="#0033CC"
Text="ADD ARTICLES /
NEWS / UPDATES" BorderColor="#000066" BorderStyle="Inset"
Width="448px"></asp:Label>
<hr style="height: -12px"/>
<p>
<asp:Label ID="Label1" runat="server" Text="ADD TITLE : " ForeColor="#3399FF"></asp:Label>
<asp:TextBox ID="Titletxt" runat="server" Width="152px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Titletxt" ErrorMessage="TITLE NEEDED TO BE ADDED">*</asp:RequiredFieldValidator>
<p>
<asp:Label ID="Label3" runat="server" Text="ADD DESCRIPTION :"
ForeColor="#3399FF"></asp:Label>
<asp:TextBox ID="DEsctxt" runat="server" TextMode="MultiLine"></asp:TextBox>
<p>
<p>
<asp:Label ID="Label4" runat="server" Text="ADD LOCATION :" ForeColor="#3399FF"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p>
<p>
<asp:Label ID="Label5" runat="server" Text="ADD IMAGE :" ForeColor="#3399FF"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<p>
<p>
<asp:Label ID="Label6" runat="server" Text="ADD IMAGE TITLE :"
ForeColor="#3399FF"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<p>
<p>
<asp:Button ID="Button1"
runat="server"
BorderColor="#003399"
BorderStyle="Inset" Font-Bold="True" ForeColor="#000099"
onclick="Button1_Click" Text="SUBMIT" ToolTip="Click here to submit" OnClientClick="return
validate();" />
<p>
THE CODE BEHIND :
Now we will save the details in the database:
DATABASE/TABLE STRUCTUTE:
NewsInfo(Title,
description, Location, ImageName,ImagePath, image, ImageTitle,
imagesize,UploadedDate,UploadedBy,IsActive)
This
IsActive define the article activation.
Here i am using sql database to store details:
try
{
string
Title = Titletxt.Text;
string
description = DEsctxt.Text.ToString();
string
location = TextBox1.Text.ToString();
string
imagetitle = TextBox2.Text.ToString();
DBAccess
obj1 = new DBAccess();
string
imagename = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Save
images into Images
folderhttp://localhost:51045/demosite/ImageStorage/ArctileImages/
FileUpload1.SaveAs(Server.MapPath("ImageStorage/ArctileImages/" +
imagename));
//getting
length of uploaded file
int
imagesize = FileUpload1.PostedFile.ContentLength;
//create
a byte array to store the binary image data
byte[]
imgbyte = new byte[imagesize];
//store
the currently selected file in memeory
HttpPostedFile
img = FileUpload1.PostedFile;
//set the
binary data
img.InputStream.Read(imgbyte, 0,
imagesize);
if
(Session["GroupCode"].ToString()
== "ADMIN")
{
string
insert = obj1.Execute("INSERT INTO
NewsInfo(Title, description, Location, ImageName,ImagePath, image, ImageTitle,
imagesize,UploadedDate,UploadedBy,IsActive)VALUES ('" + Title + "', '" + description + "', '" + location + "', '" + imagename + "', '" + "ImageStorage/ArctileImages/"
+ imagename + "','', '" +
imagetitle + "', '" + imagesize + "', GETDATE(),'" + Session["username"].ToString() + "',1)");
if
(insert == "Success")
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alertmessage", "javascript:alert('" + Title + "News/Update/Articles has been successfull uploaded!!!!')",
true);
}
}
else
{
string
insert = obj1.Execute("INSERT INTO
NewsInfo(Title, description, Location, ImageName,ImagePath, image, ImageTitle,
imagesize,UploadedDate,UploadedBy,IsActive)VALUES ('" + Title + "', '" + description + "', '" + location + "', '" + imagename + "', '" + "ImageStorage/ArctileImages/"
+ imagename + "','', '" +
imagetitle + "', '" + imagesize + "', GETDATE(),'" + Session["username"].ToString() + "',0)");
if
(insert == "Success")
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alertmessage", "javascript:alert('" + Session["UserName"].ToString() + " :Your News/Update/Articles has been Saved! Successfully and gone for
verification...')", true);
}
}
catch (Exception ex)
{
// CreateLogFiles
Err = new CreateLogFiles();
// Err.ErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);
}
}
Now
if
(Session["GroupCode"].ToString()
== "ADMIN")
line signifies that if user logged is admin itself then no
need for validation(activation) is required the article is saved and displayed
on site immediately .
it is done by comparing with the session variable ...so you
need to add the session variable information at the time of login.
SO THE ARTICLE DETAILS ARE ADDED IN THE DATABASE AND NOW WE
WILL DEVELOP AN ACTIVATION PAGE FOR THE ARTICLE:
IF YOU HAVE DISCOVER THIS IS DONE BY ISACTIVE COLUMN
in the TABLE
When it is admin then this field is set as 1
automatically ,otherwise we are setting it s 0.
So this is the magic band for the code.
FOR ACTIVATION WE NEE DTO CREATE
ANOTHER PAGE:
THE CODE DESIGN:
Before activating anything you need to look into the matter
of that article i.e you need to validate he article so we will display the
article information first and then only will activate or deactivate it.`
<div id="main" style="height:100%; width:100%; overflow:auto">
<asp:Label ID="Label2"
runat="server"
Text="ActiVate
User Added Articles" Font-Bold="true" Font-Italic="true" ForeColor="Maroon"></asp:Label>
<p></center>
<asp:Label ID="Label3" runat="server" Text="Select Article to Activate"></asp:Label>
:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="19px" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
DataTextField="NameTitle" DataValueField="ArctileId"
Width="299px">
<asp:ListItem>SELECT
ARCTILE/NEWS/UPDATES</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="LinkButton19" runat="server" Font-Bold="True"
Font-Underline="True" ForeColor="Blue" PostBackUrl="~/ActivateArticle.aspx">Refersh List</asp:LinkButton>
</p>
<p>
</p>
<p>
<asp:Label ID="desclab" runat="server" Text="Description About Article :"
Visible="False"></asp:Label>
</p>
<p>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="661px" Visible="False">
<Columns>
<asp:ImageField HeaderText="Image" DataImageUrlField="ImagePath" ControlStyle-Width="100px" ControlStyle-Height="100px" ItemStyle-Width="100px"
ItemStyle-Height="100px"/>
<asp:BoundField HeaderText = "Title" DataField="Title"
/>
<asp:BoundField HeaderText = "description"
DataField="description"
/>
<asp:BoundField HeaderText = "Location"
DataField="Location"
/>
<asp:BoundField HeaderText = "UploadedDate"
DataField="UploadedDate"
/>
<asp:BoundField HeaderText = "UploadedBy"
DataField="UploadedBy"
/>
</Columns>
</asp:GridView>
</p>
<p>
<asp:Button ID="Button1"
runat="server"
ForeColor="#000099"
onclick="Button1_Click"
onclientclick="return confirm('ARE YOU SURE YOU WANT TO Confirm THE
Article?');"
Text="ACTIVATE" Width="107px" />
<asp:Button ID="Button2" runat="server" ForeColor="#000099"
onclientclick="return confirm('ARE YOU SURE YOU WANT TO REJECT THE
Article?');"
Text="REJECT " onclick="Button2_Click" />
<p>
</div>
THE CODE BEHIND PAGE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
public partial
class ActivateArticle
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
try
{
this.SmartNavigation
= true;
if
(!(Boolean)Session["IsLogged"])
{
Response.Redirect("Login.aspx");
}
if
(Session["GroupCode"].ToString()
== "ADMIN")
{
//
Response.Redirect("Adminpage.aspx");
}
else
{
Response.Redirect("Login.aspx");
}
getarctile();
}
catch (Exception ex)
{
Response.Redirect("Login.aspx");
}
if
(DropDownList1.Items.Count.Equals(1))
{
DBAccess
obj1 = new DBAccess();
GridView1.Visible = true;
string
arctileid = DropDownList1.SelectedValue.ToString();
GridView1.DataSource =
obj1.Execute2DataTable("SELECT [ArctileId],
[Title], [description], [Location], [UploadedDate], [ImagePath],[uploadedby]
FROM [NewsInfo] WHERE [ArctileId] = " + arctileid + "");
GridView1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs
e)
{
desclab.Visible = true;
DBAccess
obj1 = new DBAccess();
GridView1.Visible = true;
string
arctileid = DropDownList1.SelectedValue.ToString();
GridView1.DataSource =
obj1.Execute2DataTable("SELECT [ArctileId],
[Title], [description], [Location], [UploadedDate], [ImagePath],[uploadedby]
FROM [NewsInfo] WHERE [ArctileId] = " + arctileid + "");
GridView1.DataBind();
//
SelectCommand="SELECT [ArctileId], [Title], [description], [Location],
[UploadedDate], [ImageID] FROM [NewsInfo] WHERE ([ArctileId] =
@ArctileId)">
}
public void getarctile()
{
DBAccess
obj1 = new DBAccess();
DropDownList1.DataSource =
obj1.Execute2DataTable("Select
ArctileId,(UploadedBy +' : '+Title) as NameTitle FROM NewsInfo WHERE (Isactive
= 0)");
DropDownList1.DataBind();
}
protected void Button1_Click(object
sender, EventArgs e)
{
DBAccess
obj1 = new DBAccess();
string
arctileid = DropDownList1.SelectedValue.ToString();
string
success = obj1.Execute("update NewsInfo set
Isactive=1 where ArctileId=" + arctileid + "");
if
(success == "Success")
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alertmessage", "javascript:alert('" + arctileid + " : ArctileId Activated Successfully and posted on
portalWebsite...')", true);
}
try
{
string
username = GridView1.Rows[0].Cells[5].Text.ToString();
DataSet
ds = new DataSet();
using
(SqlConnection con = new SqlConnection("Data Source=192.168.1.154;User ID=sa;Password =sa
;Initial Catalog=KSNDMCWEB"))
{
con.Open();
SqlCommand
cmd = new SqlCommand("SELECT Username,email FROM KSNDMCUSERS Where
username='" + username + "' ",
con);
SqlDataAdapter
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
if
(ds.Tables[0].Rows.Count > 0)
{
MailMessage
Msg = new MailMessage();
//
Sender e-mail address.
string
emailid = ds.Tables[0].Rows[0]["EMAIL"].ToString();
Msg.From = new MailAddress(emailid);
//
Recipient e-mail address.
Msg.To.Add(emailid);
Msg.Subject = "Arctile/News/Update ";
Msg.Body = "Hi, " + username + " <br/>Your Arctile has been Accepted!!thanks
for your valuable notion . <br/>";
Msg.IsBodyHtml = true;
//
your remote SMTP server IP.
SmtpClient
smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("ksndmcdc@gmail.com", "idbi@password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg
= null;
}
else
{
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
public void getusername()
{
DBAccess
obj1 = new DBAccess();
DropDownList1.DataSource =
obj1.Execute2DataTable("Select
ArctileId,UploadedBy FROM NewsInfo WHERE (Isactive = 0)");
DropDownList1.DataBind();
}
protected void Button2_Click(object
sender, EventArgs e)
{
try
{
string
username = GridView1.Rows[0].Cells[5].Text.ToString();
DataSet
ds = new DataSet();
using
(SqlConnection con = new SqlConnection("Data Source=192.168.1.154;User ID=sa;Password =sa
;Initial Catalog=KSNDMCWEB"))
{
con.Open();
SqlCommand
cmd = new SqlCommand("SELECT Username,email FROM KSNDMCUSERS Where
username='" + username + "' ",
con);
SqlDataAdapter
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
if
(ds.Tables[0].Rows.Count > 0)
{
MailMessage
Msg = new MailMessage();
//
Sender e-mail address.
string
emailid = ds.Tables[0].Rows[0]["EMAIL"].ToString();
Msg.From = new MailAddress(emailid);
//
Recipient e-mail address.
Msg.To.Add(emailid);
Msg.Subject = "Arctile/News/Update REJECTED";
Msg.Body = "Hi, " + username + " <br/>Your Arctile has been
rejected,<br/>it is due to the image uploaded or content quality.
<br/>";
Msg.IsBodyHtml = true;
// your
remote SMTP server IP.
SmtpClient
smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("ksndmcdc@gmail.com", "idbi@password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg
= null;
Response.Write("<script>alert('the arctile has been rejected
and mail has been send successfully to the crossponding user!!!') </Script>");
DBAccess
obj1 = new DBAccess();
string
arctileid = DropDownList1.SelectedValue.ToString();
string
success = obj1.Execute("delete from NewsInfo
where ArctileId=" +
arctileid + "");
if
(success == "success")
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alertmessage", "javascript:alert('" + arctileid + " : ArctileId hs been rejected and deleted permanently...')",
true);
}
}
else
{
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
}
In this coed we are
sending the mail to the arctile owner about the acceptance or rejectance of the
arctile.
THE logic behind
activating is by setting the IsActive filed in the database 1 ,and thus
displaying the arctiles on the webpage which have isactive as 1.
No comments :
Post a Comment