Subscribe For Free Updates!

We'll not spam mate! We promise.

Sep 28, 2011

FileUpload Controle in ASP.NET Save Images in Database C# VB.NET

Views:

In this example i am Uploading Images using FileUpload Control and saving or storing them in SQL Server database in ASP.NET with C# and VB.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 Image.
3. Image Image to store image in binary format.

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














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
View source
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}

VB.NET Code
View source
01Protected Sub btnUpload_Click
02(ByVal sender As Object, ByVal e As EventArgs)
03 
04    Dim strImageName As String = txtName.Text.ToString()
05    If FileUpload1.PostedFile IsNot Nothing AndAlso
06       FileUpload1.PostedFile.FileName <> "" Then
07 
08        Dim imageSize As Byte() = New Byte
09          (FileUpload1.PostedFile.ContentLength - 1) {}
10 
11        Dim uploadedImage__1 As HttpPostedFile =
12                                 FileUpload1.PostedFile
13 
14        uploadedImage__1.InputStream.Read(imageSize, 0,
15              CInt(FileUpload1.PostedFile.ContentLength))
16 

17                                                             ' Create SQL Connection

18        Dim con As New SqlConnection()
19        con.ConnectionString =
20                 ConfigurationManager.ConnectionStrings
21                  ("ConnectionString").ConnectionString
22 

23                                                                ' Create SQL Command

24 
25        Dim cmd As New SqlCommand()
26        cmd.CommandText = "INSERT INTO Images
27           (ImageName,Image) VALUES (@ImageName,@Image)"
28        cmd.CommandType = CommandType.Text
29        cmd.Connection = con
30 
31        Dim ImageName As New SqlParameter
32                  ("@ImageName", SqlDbType.VarChar, 50)
33        ImageName.Value = strImageName.ToString()
34        cmd.Parameters.Add(ImageName)
35 
36        Dim UploadedImage__2 As New SqlParameter
37            ("@Image", SqlDbType.Image, imageSize.Length)
38        UploadedImage__2.Value = imageSize
39        cmd.Parameters.Add(UploadedImage__2)
40        con.Open()
41        Dim result As Integer = cmd.ExecuteNonQuery()
42        con.Close()
43        If result > 0 Then
44            lblMessage.Text = "File Uploaded"
45        End If
46        GridView1.DataBind()
47    End If
48End Sub

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

1 comments:

  1. (1) seems to insert filename and image twice?
    (2) txtname.text is not populated as fileupload1.filename
    (3) image data appears as a red x


    ReplyDelete

Become a Fan

visual studio learn