Insert Image into Sqlserver database by using c# asp.net :

The following steps is shows about insert a image into database by using asp.net.
First Create a Image table in sqlserver database with two fields ImageName and Image.

Then
1.Open VisualStudio 2010
2.FileàNewàWebsiteàselect Empty Website àGive name as Imageà Select Location to saveà Save
3.Select Solution àright click à Add New ItemàWebFormàGive name as ImageLoad.aspx
4.Write the following Code in ImageLoad..aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageLoad.aspx.cs" Inherits="ImageLoad" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<table class="style1">
 <tr>
    <td>
   <asp:Label ID="lbImageName" runat="server" Text="ImageName"></asp:Label>
    </td>
    <td>
     <asp:TextBox ID="txtImageName" runat="server"></asp:TextBox>
    </td>
  </tr>
   <tr>
   <td>
    <asp:Label ID="lbImage" runat="server" Text="Image"></asp:Label>
                </td>
   <td>
     <asp:FileUpload ID="FileUpload1" runat="server" />
   </td>
   </tr>
    <tr>
     <td>
<asp:Label ID="lbmsg" runat="server" Text="Label" ></asp:Label>
</td>
 <td>
 <asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click1"
                        Text="Upload" />
  </td>
  </tr>
  </table>
            
    </div>
    </form>
</body>
</html>


5.Write the following Code in ImageLoad.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class ImageLoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  protected void btnUpload_Click1(object sender, EventArgs e)
    {
       string strImageName = txtImageName.Text.ToString();

      if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {

            byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];

            HttpPostedFile uploadedImage = FileUpload1.PostedFile;

            uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

            // Create SQL Connection

            SqlConnection con = new SqlConnection("data Source=ADMIN;initial catalog=test; user id=Sa; password=123");



            // Create SQL Command
           
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "INSERT INTO Image(ImageName,Image)" + " VALUES (@ImageName,@Image)";

            cmd.CommandType = CommandType.Text;

            cmd.Connection = con;
           
            SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);

            ImageName.Value = strImageName.ToString();

            cmd.Parameters.Add(ImageName);

           
            SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);

            UploadedImage.Value = imageSize;

            cmd.Parameters.Add(UploadedImage);

            con.Open();

            int result = cmd.ExecuteNonQuery();

            con.Close();

            if (result > 0)

                lbmsg.Text = "File Uploaded";

        }
    }
}

6.See the Output

7.Then the Image will store in database with Binary Data.





15 comments:

  1. Thanks for the post!!!

    ReplyDelete
  2. its nod inserting in database...

    ReplyDelete
  3. you may find this useful:
    http://webgeek.elletis.com/how-to-enter-html-code-into-database-from-a-textbox/

    ReplyDelete
  4. thnaks ..
    your code was helpful for me..

    ReplyDelete
  5. i have to insert an image into mysql using jquery in MVC3.
    can anyone help me???????
    please.

    ReplyDelete
  6. it's not uploading image to database

    ReplyDelete
  7. Sir its two time inserting my image can u help me please

    ReplyDelete
  8. sir i want to give a particular size for images
    where i have to give

    ReplyDelete

  9. Hey, nice site you have here! Keep up the excellent work!
    Elletis Web Geek

    ReplyDelete
  10. Thank you very much...
    And i want coding for retrieve image from database,please help me...

    ReplyDelete
  11. I am getting an error like this..
    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    Source Error:


    Line 54: cmd.Parameters.Add(UploadedImage);
    Line 55:
    Line 56: con.Open();
    Line 57:
    Line 58: int result = cmd.ExecuteNonQuery();

    Source File: c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WebApplication2\WebApplication2\WebForm1.aspx.cs Line: 56

    ReplyDelete
  12. Finally i changed user id and password and its not establish the connection pls help me to solve this problem immediately

    ReplyDelete
  13. I want To uplaod image from any folder by providing virtual path means any thing else but without use of file upload

    ReplyDelete
  14. .Net Tutorial: Insert Image Into Sqlserver Database By Using C Asp.Net : >>>>> Download Now

    >>>>> Download Full

    .Net Tutorial: Insert Image Into Sqlserver Database By Using C Asp.Net : >>>>> Download LINK

    >>>>> Download Now

    .Net Tutorial: Insert Image Into Sqlserver Database By Using C Asp.Net : >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete