Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i want to upload a file from three tier architecture.

My architecture:
Contract Class Library :
C#
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 :
C#
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 :

C#
public void InsertImage(LeaveManagmentSystem.Contract.UploadCategory objUploadCategory)
      {
          using (UploadRepositoryCategory obj = new UploadRepositoryCategory())
          {
              obj.InsertImage(objUploadCategory);
          }
      }


Reference Are
C#
[Contract]
[Data]<contract>
[Business]<Data,Contract>
[Project][Business,Data]


My Codes are for upload a images inside the grid.
On Button Click:
C#
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:
C#
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(); 
 
} 



C#
<asp:TemplateField>
 <ItemTemplate>
    <asp:Image ID="Image1" runat="server" ImageUrl='<%#    "Handler.ashx?id=" + Eval("Img_Id")  %>' />
 </ItemTemplate>
</asp:TemplateField>

C#
GridView1.DataSource = FetchAllImagesInfo();
GridView1.DataBind();

C#
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
Posted
Updated 8-May-12 17:14pm
v2
Comments
Ganesan Senthilvel 8-May-12 16:29pm    
What is the expectation from us?
Sergey Alexandrovich Kryukov 8-May-12 16:51pm    
Where is your question?
--SA
Shahin Khorshidnia 8-May-12 17:21pm    
3 Tiers?! You mean 3 seperated computers or 3 logical layers?
TRK3 8-May-12 18:28pm    
And why do you insist on a 3 tier architecture for something that uploads images?

Is this project going to live long enough that you expect to change one of the layers and leave the other two in place?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900