//upload image in table
protected void BtnSave_Click(object sender, EventArgs e)
{
string strBlogText = Convert.ToString(EdtBlogPost.Content);
string strBlogTitle = Convert.ToString(txt_title.Text);
string strBlogTag = Convert.ToString(txtTag.Text);
int CategoryID = Convert.ToInt32(ddl_category.SelectedValue);
int createdBy = Convert.ToInt32(Session["id"]);
//if(BtnSave.Text=="SAVE"||BtnSave1.Text="SAVE")
System.Web.UI.WebControls.FileUpload Country_img = (System.Web.UI.WebControls.FileUpload)FuBlogImage;
Byte[] imgByte = null;
if (FuBlogImage.HasFile && FuBlogImage.PostedFile != null)
{
HttpPostedFile File = FuBlogImage.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
int check = 0;
int check_update = 0;
switch (BtnSave.Text)
{
case "SAVE":
{
check = objBlogPost.Ins_BlogPost(strBlogTitle, strBlogText, strBlogTag, CategoryID, createdBy, imgByte, 0, 0);
break;
}
case "UPDATE":
{
if (FuBlogImage.HasFile && FuBlogImage.PostedFile != null)
check_update = objBlogPost.Ins_BlogPost(strBlogTitle, strBlogText, strBlogTag, CategoryID, createdBy, imgByte, 1, id1);
else
check_update = objBlogPost.UpdateBlog_WithoutImage(strBlogTitle, strBlogText, strBlogTag, CategoryID, createdBy, id1);
break;
}
default:
break;
}
if (check > 0 || check_update > 0)
{
Response.Redirect("BlogPost_Grid.aspx");
Session["msgOk"] = "Inserted Successfully";
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert(' Sorry!Try Again ')</script>");
}
}
// method called
public int Ins_BlogPost(string blogTitle, string blogText, string blogTag, int categoryID, int createdByID, Byte[] img, int mode, int BlogPostID)
{
int j = 0;
switch (mode)
{
case 0:
{
j = context.fn_ins_blogpost(blogTitle, blogText, blogTag, categoryID, createdByID, true, img, 0, 0);
break;
}
case 1:
{
j = context.fn_ins_blogpost(blogTitle, blogText, blogTag, categoryID, createdByID, true, img, 1, BlogPostID);
break;
}
default:
break;
}
return j;
}
//Call from row data bound
protected void gridBlogPost_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkBxSelect");
CheckBox chkBxHeader = (CheckBox)this.gridBlogPost.HeaderRow.FindControl("chkBxHeader");
//CheckBox chkBx = (CheckBox)e.Row.Cells[2].FindControl("chkBx");
chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(this,'{0}');", chkBxHeader.ClientID);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
Label labCreatedOn = (Label)e.Row.FindControl("labCreatedOn");
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.FindControl("img_Blog");
img.ImageUrl = "~/BlogHendlar.ashx?id=" + DataBinder.Eval(e.Row.DataItem, "BlogPostID");
string strUrl = "~/BlogHendlar.ashx?id=" + DataBinder.Eval(e.Row.DataItem, "BlogPostID");
Session["tableName"] = "tblBlogPost";
//DateTime date1 =Convert.ToDateTime( lst[d].CreatedOn);labCreatedOn
DateTime date1 = Convert.ToDateTime(labCreatedOn.Text);
d++;
//IFormatProvider culture = new CultureInfo("en-GB");
string dateForm = date1.ToString("dd/MM/yyyy");
labCreatedOn.Text = date1.ToString("dd/MM/yyyy");
//ImageButton btn_Edit = (ImageButton)e.Row.FindControl("BtnEdit");
//btn_Edit.CommandArgument = strUrl; // to find path of image
ImageButton imgStatus = (ImageButton)e.Row.FindControl("imgStatus");
imgStatus.CommandArgument = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "StatusID"))
+ "/" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "BlogPostID"));
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "StatusID")) == "True")
{
imgStatus.ImageUrl = "~/images/unblock.png";
imgStatus.ToolTip = "Click Here To Block";
}
else
{
imgStatus.ImageUrl = "~/images/block.png";
imgStatus.ToolTip = "Click Here To Unblock";
}
ImageButton BtnDelete = (ImageButton)e.Row.FindControl("imgbtnDelete");
BtnDelete.ImageUrl = "~/images/cross.png";
}
catch (Exception)
{
}
}
}
// Add handler page with name BlogHendlar.ashx
<%@ WebHandler Language="C#" Class="BlogHendlar" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using BlogJobPortal.BL;
using BlogJobPortal.DL;
using System.Linq;
public class BlogHendlar : IHttpHandler
{
clsBlogPost objblog = new clsBlogPost();
public void ProcessRequest(HttpContext context)
{
Int32 county_Id;
if (context.Request.QueryString["id"] != null)
county_Id = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(county_Id);
byte[] buffer = new byte[1024];
if (strm != null)
{
int byteSeq = strm.Read(buffer, 0, 1024); //4096
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 1024);
}
}//context.Response.BinaryWrite(buffer);
}
public Stream ShowEmpImage(int county_Id)
{
try
{
// object img1 = objblog.BlogImage(county_Id).Single();
string conn = Convert.ToString(ConfigurationManager.AppSettings["connectDb"]);
SqlConnection connection = new SqlConnection(conn);
//string tab=Convert.ToString(
string sql = "SELECT [Image] FROM tblBlogPost WHERE BlogPostID = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", county_Id);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
catch (Exception e)
{
return null;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
No comments:
Post a Comment