here is the way i store my image...
'' - string imageUploaded = "Image";
-
string savePath;
-
string saveFile;
-
-
-
conItem.Close();
-
if (fupload.HasFile)
-
{
-
savePath = Path.Combine(Request.PhysicalApplicationPath, imageUploaded);
-
saveFile = Path.Combine(savePath, itemCount + ".jpg");
-
fupload.SaveAs(saveFile);
-
}
''
Then, how i can retrieve the image from the database and display it in my form?
the image will be returned and needs to be read into a byte array, here's an example of retrieving an image from your table (data type should be image - public byte[] GetImageFromSQL(string imgName)
-
{
-
try
-
{
-
using (MemoryStream stream = new MemoryStream())
-
{
-
using (SqlConnection connection = new SqlConnection(@"YourConnectionString"))
-
{
-
using (SqlCommand command = new SqlCommand())
-
{
-
command.CommandText = "SELECT [YourImageColumn] FROM [YourTable] WHERE [YourImageNameColumn] = @img";
-
command.CommandType = System.Data.CommandType.Text;
-
command.Parameters.AddWithValue("@img", imgName);
-
command.Connection = connection;
-
command.Connection.Open();
-
-
SqlDataReader reader = command.ExecuteReader();
-
byte[] imgBytes = null;
-
while (reader.Read())
-
imgBytes = (byte[])reader[0];
-
-
return imgBytes;
-
}
-
}
-
}
-
}
-
catch(Exception ex)
-
{
-
Response.Write(ex.Message);
-
return null;
-
}
-
}
Then to display it - protected void Page_Load(object sender, EventArgs e)
-
{
-
byte[] img = GetImageFromSQL("YourImageName");
-
Response.ContentType = "image/jpeg";
-
Response.BinaryWrite(img);
-
}
Hope that helps :)
4 2760
the image will be returned and needs to be read into a byte array, here's an example of retrieving an image from your table (data type should be image - public byte[] GetImageFromSQL(string imgName)
-
{
-
try
-
{
-
using (MemoryStream stream = new MemoryStream())
-
{
-
using (SqlConnection connection = new SqlConnection(@"YourConnectionString"))
-
{
-
using (SqlCommand command = new SqlCommand())
-
{
-
command.CommandText = "SELECT [YourImageColumn] FROM [YourTable] WHERE [YourImageNameColumn] = @img";
-
command.CommandType = System.Data.CommandType.Text;
-
command.Parameters.AddWithValue("@img", imgName);
-
command.Connection = connection;
-
command.Connection.Open();
-
-
SqlDataReader reader = command.ExecuteReader();
-
byte[] imgBytes = null;
-
while (reader.Read())
-
imgBytes = (byte[])reader[0];
-
-
return imgBytes;
-
}
-
}
-
}
-
}
-
catch(Exception ex)
-
{
-
Response.Write(ex.Message);
-
return null;
-
}
-
}
Then to display it - protected void Page_Load(object sender, EventArgs e)
-
{
-
byte[] img = GetImageFromSQL("YourImageName");
-
Response.ContentType = "image/jpeg";
-
Response.BinaryWrite(img);
-
}
Hope that helps :)
Solved - Imports System.Data.SqlClient
-
Imports System.Data
-
Imports System.IO
-
-
Public Class Form1
-
Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=RestPos;Persist Security Info=True;User ID=sa;Password=password"
-
Dim con As New SqlClient.SqlConnection
-
-
'To open an image from computer
-
'-------------------------------
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
OpenFileDialog1.Title = "Please select a file"
-
OpenFileDialog1.InitialDirectory = "c:temp"
-
OpenFileDialog1.ShowDialog()
-
TextBox2.Text = OpenFileDialog1.FileName.ToString '--->To Show the file path in textbox2
-
PictureBox1.ImageLocation = TextBox2.Text '--->To show selected image in picturebox
-
End sub
-
-
'To insert selected image into database
-
'The datatype of the column in table to store image should be <image>
-
'--------------------------------------------------------------------
-
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
-
con.ConnectionString = str
-
Dim ms As New IO.MemoryStream()
-
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
-
Dim arrimage() As Byte = ms.GetBuffer
-
Dim cmd As New SqlCommand("insert into image (Emp_Image)values(@picture)", con)
-
cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrimage
-
con.Open()
-
cmd.ExecuteNonQuery()
-
con.Close()
-
End Sub
-
-
'We have successfully inserted the image into database.
-
'Now we want to Retrieve the image from database.
-
'-------------------------------------------------
-
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
-
Dim stream As New IO.MemoryStream()
-
con.Open()
-
Dim command As New SqlCommand("select Emp_Image from Image where Emp_Id='" + TextBox3.Text + "'", con) '--->You can give Emp_id instead of Textbox value.
-
Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
-
stream.Write(image, 0, image.Length)
-
con.Close()
-
Dim bitmap As New Bitmap(stream)
-
PictureBox2.Image = bitmap '--->I have used another picturebox to display image from database.
-
End Sub
-
'Thats all, You can place a linklabel below the picturebox if you to change photo and update it in database.
Regards,
Prathap
Works like magic. Thank you very much for the posting.
No problem, glad I could help :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Wayne Wengert |
last post by:
I am trying to build an ASP.NET application which includes the capability to
allow users to save pictures to a SQL Server 2000 database table and then
later to display selected photos (usually JPG...
|
by: chrispragash |
last post by:
Hello Everybody,
I have a server control that stores an image in the database as an
image datatype. I would like to display this image (may be in an image
control) along with other text in a...
|
by: RedSouljaz |
last post by:
Hi,
How to display image that was saved in database ms sql
server 2000 into picture box. The field type that I use
in database is Images
I can save to database but cannot show from database.
...
|
by: den 2005 |
last post by:
Hi everyone,
Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file, then
retrieve this image from...
|
by: Rahul |
last post by:
while writing an image ... i get the following error
my Code
-------------------------------------------------------------------
Dim imgStream As New MemoryStream()
Dim query As String
query =...
|
by: shalini328 |
last post by:
I am using ASP.net 1.1 C# and database is MS access.Can you tell me how can i display image from database
|
by: mirianCalin |
last post by:
i am doing a site for appliance center..
i need to display all the products that the company offers, but my problem is that i cant display ALL the images in my database.. the first entry on the...
|
by: AnagJohari |
last post by:
I would like to of display an the image at a proper position on a browser. I access the image from the database before doing this I stored the image in a database in a byte form. I also make a...
|
by: SafaaDalloul |
last post by:
Please I want know How I can Display Image from database randomly when the web page refresh in Asp.net by C# code
|
by: chabbs |
last post by:
Hi,
I have my form with some combo box and radio button.
And i need to display an image based on the selections.
I need to display dynamically the image when the selections are made without...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |