Upload and Save Image in BLOB Column SQL Server with Asp.Net



In this post Upload and Save Image in BLOB Column SQL Server with Asp.Net

<asp:FileUpload runat="server" ID="FileUpload1" />
<asp:Button runat="server" ValidationGroup="Details" ID="btnSubmit" Text="Upload"
    onclick="btnSubmit_Click" />
Important Namespaces
-----------------------

using System.Data;
using System.Data.SqlClient;
using System.IO;

------------------------

Upload and Save Button Click Event
---------------------------------

        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        byte[] imagecode = br.ReadBytes((Int32)fs.Length);
        string query = "Insert Into Profiledetails VALUES ('" + TextBox1.Text + "',@pic)";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@pic", imagecode);
        String mycon = "Data Source=sk-pc; Initial Catalog=StoreBinaryImage; Integrated Security=true";
        SqlConnection con = new SqlConnection(mycon);
        con.Open();
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        Label3.Text = "Image Has Been Saved Successfully with Image ID " + TextBox1.Text;
        con.Close();
        TextBox1.Text = "";

---------------------------------------

0 Comment's

Comment Form

Submit Comment