Friday, 13 September 2013

FileUpload Save Images In Database In ASP.NET

This example explains how to Save Images In Sqlserver Database In Asp.Net Using File Upload Control.
I am Uploading Images using FileUpload Control and saving or storing them in SQL Server database in ASP.NET with C# and VB.NET.
FileUpload Save Images In Database In ASP.NET

Database is having a table named Images with three columns.
1. ID Numeric Primary key with Identity Increment.
2. ImageName Varchar to store Name of picture.
3. Image column to store image in binary format. 

After uploading and saving images in database, pics are displayed in GridView.


To know how to display images in gridview from database, read my post mentioned below
Display Images In GridView From DataBase in ASP.NET C# VB.NET

Html markup of the page look like
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server" Width="95px">
</asp:TextBox>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Label ID="lblMessage" runat="server">
</asp:Label>
<asp:Button ID="btnUpload" runat="server" 
            OnClick="btnUpload_Click" Text="Upload"/>
</div>
</form>

Write this code in Click Event of Upload Button

C# Code behind

01protected void btnUpload_Click(object sender, EventArgs e)
02{
03 string strImageName = txtName.Text.ToString();
04 if (FileUpload1.PostedFile != null &&
05     FileUpload1.PostedFile.FileName != "")
06  {
07   byte[] imageSize = new byte
08                 [FileUpload1.PostedFile.ContentLength];
09  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
10  uploadedImage.InputStream.Read
11     (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
12 
13 // Create SQL Connection
14  SqlConnection con = new SqlConnection();
15  con.ConnectionString = ConfigurationManager.ConnectionStrings
16                         ["ConnectionString"].ConnectionString;
17 
18 // Create SQL Command
19 
20 SqlCommand cmd = new SqlCommand();
21 cmd.CommandText = "INSERT INTO Images(ImageName,Image)" +
22                   " VALUES (@ImageName,@Image)";
23 cmd.CommandType = CommandType.Text;
24 cmd.Connection = con;
25 
26 SqlParameter ImageName = new SqlParameter
27                     ("@ImageName", SqlDbType.VarChar, 50);
28 ImageName.Value = strImageName.ToString();
29 cmd.Parameters.Add(ImageName);
30 
31 SqlParameter UploadedImage = new SqlParameter
32               ("@Image", SqlDbType.Image, imageSize.Length);
33 UploadedImage.Value = imageSize;
34 cmd.Parameters.Add(UploadedImage);
35 con.Open();
36 int result = cmd.ExecuteNonQuery();
37 con.Close();
38 if (result > 0)
39 lblMessage.Text = "File Uploaded";
40 GridView1.DataBind();
41 }
42}

Hover Effects for GridView Rows Using CSS

The GridView HTML Code

The HTML code for the GridView in this article will looks like:
<asp:GridView ID="gvHover" BackColor="WhiteSmoke" runat="server" AutoGenerateColumns="False"
              GridLines="Vertical" CssClass="grid-view" OnRowCreated="gvHover_RowCreated">
   <Columns>
      <asp:BoundField HeaderText="n" DataField="n">
         <HeaderStyle Width="25px" />
         <ItemStyle Width="25px" />
      </asp:BoundField>
      <asp:BoundField HeaderText="sqrt(n)" DataField="sqrtn">
         <HeaderStyle Width="150px" />
         <ItemStyle Width="150px" />
      </asp:BoundField>
      <asp:BoundField HeaderText="qbrt(n)" DataField="qbrtn">
         <HeaderStyle Width="150px" />
         <ItemStyle Width="150px" />
      </asp:BoundField>
   </Columns>
</asp:GridView>

Styling the GridView

In order to style the Gridview, attach a CSS class to it, like so:
<asp:GridView ... CssClass="grid-view" ... > ... </asp:GridView>

Styling the GridView’s Header Row, Normal Row, and Alternate Row

In order to style the GridWiew’s header, normal, and alternate rows, attach the CSS classes to these rows through theRowCreated event of the GridView, as:
//Add CSS class on header row.
if (e.Row.RowType == DataControlRowType.Header)
   e.Row.CssClass = "header";

//Add CSS class on normal row.
if (e.Row.RowType == DataControlRowType.DataRow && 
          e.Row.RowState == DataControlRowState.Normal)
   e.Row.CssClass = "normal";

//Add CSS class on alternate row.
if (e.Row.RowType == DataControlRowType.DataRow && 
          e.Row.RowState == DataControlRowState.Alternate)
   e.Row.CssClass = "alternate";

The CSS Classes

Below are the CSS classes that have been used above to style the GridView and its header, normal, and alternate rows:
.grid-view
{
   padding: 0;
   margin: 0;
   border: 1px solid #333;
   font-family: "Verdana, Arial, Helvetica, sans-serif, Trebuchet MS";
   font-size: 0.9em;
}

.grid-view tr.header
{
   color: white;
   background-color: #FF5600;
   height: 25px;
   vertical-align: middle;
   text-align: center;
   font-weight: bold;
}

.grid-view tr.normal
{
   color: black;
   background-color: #FDC64E;
   height: 25px;
   vertical-align: middle;
   text-align: center;
}

.grid-view tr.alternate
{
   color: black;
   background-color: #D59200;
   height: 25px;
   vertical-align: middle;
   text-align: center;
}

Adding a Hover Effect to the GridView rows

Finally, to apply the hover effect to the GridView rows, the following CSS is used:
.grid-view tr.normal:hover, .grid-view tr.alternate:hover
{
   background-color: white;
   color: black;
   font-weight: bold;
}
Note that the hover effect has been applied to the normal and alternate rows only, not on the header row. You can also use different color schemes for the normal and alternate rows separately, for the hover effect.

Using the CSS Classes

Put all the corresponding CSS classes in a stylesheet and give its reference on the web page’s head section, as:
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />