Are you searching the ASP.net code for saving images and retrieve from SQL Database? Apply the below code and run your program successfully. The below code is tested successfully. Hope you follow this code and run your program.
Design the page as require
As take two lable, two text boxes, two buttons and one image box and one fileuplod control on the page.
First of all create a table as
Create database Hollybood
Use it
Create table
(
Roll varchar(50),
Name varchar(50),
Image varbinary(max)
)
Run it
Now dubble click on the form and write the following namespace
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
And write code below public parcial class
SqlConnection sc = new SqlConnection("Data Source = .; Initial Catalog = Hollybood; Integrated Security = true;");
Note: Hollybood is database name.
Now write the code below page load event as-
FileUpload1.Attributes.Add("onpropertychange", "show");
Now go to the page and dubble click on the save button and write the following code as follow:-
FileUpload1.SaveAs(Server.MapPath("~\\pict1.jpg"));
FileStream fs = new FileStream(Server.MapPath("~\\pict1.jpg"), FileMode.Open);
Byte[] date = new byte[fs.Length];
fs.Read(date, 0, (int)fs.Length);
fs.Close();
SqlCommand cmd = new SqlCommand("insert into janu(Roll, Name, image)values(@Roll,@Name,@image)", sc);
cmd.Parameters.AddWithValue("@Roll", TextBox1.Text);
cmd.Parameters.AddWithValue("@Name", TextBox2.Text);
SqlParameter pict = new SqlParameter("@image", SqlDbType.VarBinary);
pict.Value = date;
cmd.Parameters.Add(pict);
sc.Open();
cmd.ExecuteNonQuery();
// con.Close();
sc.Close();
Response.Write(" image succeful saved");
Image1.ImageUrl = null;
Note : take care here the column name as you mention in database as same like write here it may be case sensitive.
(Note;) create a Map Path here as like
Go to view menu and open solution explorel
Wright click on path above shown on the solution explerel(E:\ AmitProgram\Amit)
Create a folder as require Here pict1
It is necessary other wise program does not run.
Now dubble click on dispalay button and write the following code as follow
SqlCommand cmd = new SqlCommand("select *from janu where roll='" + TextBox1.Text + "'", sc);
sc.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
dr.Read();
TextBox2.Text = dr["name"].ToString();
byte[] data = (byte[])dr["image"];
MemoryStream str = new MemoryStream();
str.Write(data, 0, data.Length);
Bitmap img = new Bitmap(str);
img.Save(Server.MapPath("~\\pict1.jpg"));
Image1.ImageUrl = "pict1.jpg";
//con.Close();
sc.Close();
}
else
{
Image1.ImageUrl = null;
TextBox2.Text = "";
}
Note:- janu is the name of table
Now there is very important coding
Come on the page and go to source page and write the following code below the tittle tag
<script type = "text/javascript">
funcion show()
{
document.getElementById("Image1").sec = document.getElementById("FileUpLoad1").value;
}
</script>
Note: map path is necessary so you must create it as I above direction’
Now speak the Jai maa and run the program.