i want to upload a file from three tier architecture.
My architecture:
Contract Class Library :
public int ing_id { get; set; }
public byte image_content { get; set;}
public string Image_Type { get; set;}
public bool Image_Size { get; set;}
Data Class Library :
public static string ConnectionString
{
get { return ConfigurationManager.ConnectionStrings["SGMTConnectionString"].ConnectionString; }
}
public UploadRepositoryCategory()
{
_datacontex = new LeaveManagmentSystem.Data.DataAccess.DataAccessDataContext(ConnectionString);
}
public DataAccess.DataAccessDataContext _datacontex;
public void Dispose()
{
_datacontex.Dispose();
}
Business Class Library :
public void InsertImage(LeaveManagmentSystem.Contract.UploadCategory objUploadCategory)
{
using (UploadRepositoryCategory obj = new UploadRepositoryCategory())
{
obj.InsertImage(objUploadCategory);
}
}
Reference Are
[Contract]
[Data]<contract>
[Business]<Data,Contract>
[Project][Business,Data]
My Codes are for upload a images inside the grid.
On Button Click:
if (FileUpload1.PostedFile != null
&& FileUpload1.PostedFile.FileName != "")
{
byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
SqlConnection myConnection = new SqlConnection("Your Connection String");
SqlCommand storeimage = new SqlCommand("INSERT INTO ImageGallery "
+"(Image_Content, Image_Type, Image_Size) "
+" values (@image, @imagetype, @imagesize)", myConnection);
storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value
= FileUpload1.PostedFile.ContentType;
storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value
= FileUpload1.PostedFile.ContentLength;
myConnection.Open();
storeimage.ExecuteNonQuery();
myConnection.Close();
}
Handler:
public void ProcessRequest (HttpContext context)
{
SqlConnection myConnection = new SqlConnection("YourConnectionString");
myConnection.Open();
string sql = "Select Image_Content, Image_Type from ImageGallery where Img_Id=@ImageId";
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = context.Request.QueryString["id"];
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.ContentType = dr["Image_Type"].ToString();
context.Response.BinaryWrite((byte[])dr["Image_Content"]);
dr.Close();
myConnection.Close();
}
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?id=" + Eval("Img_Id") %>' />
</ItemTemplate>
</asp:TemplateField>
GridView1.DataSource = FetchAllImagesInfo();
GridView1.DataBind();
public DataTable FetchAllImagesInfo())
{
string sql = "Select * from ImageGallery";
SqlDataAdapter da = new SqlDataAdapter(sql, "Your Connection String");
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
Friends this code will work properly if i write this behind the page where are control are exist
But I want to write this code in proper way like Contract=>Data=>Business and then i want to connect this to the direct page