Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,218,304 members, 8,037,519 topics. Date: Thursday, 26 December 2024 at 11:02 AM

Vb.net Code To Display Image In Picturebox From Sql Server - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Vb.net Code To Display Image In Picturebox From Sql Server (54862 Views)

Post Ur Vb 6.0 Questions Here / Help me convert this Vb6.0 code To Vb.net / Using Next/prev Button To Display Array Elements (2) (3) (4)

(1) (Reply) (Go Down)

Vb.net Code To Display Image In Picturebox From Sql Server by salamsy(m): 10:59am On Oct 21, 2009
pls house,

i need a vb.net code to retreive image from sql server using vb.net.I want to display the image using picturebox.excerpt from my sql query;

mycmd = New SqlCommand("select picture from biodata where id='" & TextBox2.Text & "'"wink
Re: Vb.net Code To Display Image In Picturebox From Sql Server by iGuru1(m): 1:28pm On Oct 21, 2009
Hey man, i like this language.
Can you pls do me a favor to tell me where you learnt it from.
Re: Vb.net Code To Display Image In Picturebox From Sql Server by Kobojunkie: 3:46pm On Oct 21, 2009
Well. . . . you are sort of on the right track but you need to learn some basics on accessing databases and retrieving information @Poster. You also need to know how to access information contained in resultsets returned to you. May I suggest you spend at least 8 hours focusing on ADO.NET data retrieval? I mean we could try to write your code for you over the next couple of days. But I think you can get better information if you put your head into a good chapter on ADO.NET, DataReaders/DataSets, DataTables, DataRows, DataColumns, SqlCommand and related objects.
Re: Vb.net Code To Display Image In Picturebox From Sql Server by ABAboi: 11:03pm On Nov 22, 2009
Its actually a straight forward process. Follow the following steps. However the following codes basically scans ur computer for .JPEG file extensions and adds them direct to ur SQLSERVER. But ofcourse u can decide to import the image in a picture box control prior saving it in the database.
Create a table named "ImageData"Its attributess are

Name (varChar50)
Picture  (Image)
CreateDate(varChar20)--'NOT REALLY NECCESSARY

Now in your Form,
You will need  2 buttons named btnSave and BtnSearch, textbox (txtName)

For the clicked event of btnSearch, add these codes
Dim pictureLocation As String
        Dim a As New OpenFileDialog
        a.Filter = ("Image files| *.JPG"wink
        pictureLocation = a.FileName
       
            a.ShowDialog()
            Using conn As New System.Data.SqlClient.SqlConnection("[s]Data Source=DENSMAN-PC\SQLEXPRESS;Initial Catalog=aba;Integrated Security=True[/s]"wink 'Insert ur connection string here
                conn.Open()

                Using cmd As New SqlClient.SqlCommand("Insert Into ImageData(Name, CreateDate, Picture) Values (@Name, @CreateDate, @Picture)", conn)
                    cmd.Parameters.Add(New SqlClient.SqlParameter("@Name", SqlDbType.VarChar)).Value = txtName.Text
                    cmd.Parameters.Add(New SqlClient.SqlParameter("@CreateDate", SqlDbType.VarChar)).Value = DateTime.Today
                    cmd.Parameters.Add(New SqlClient.SqlParameter("@Picture", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
                    cmd.ExecuteNonQuery()
                End Using


            End Using
       
When you are done with the following, i can tell u other ways to modify and optimise your programme, then i will give u the second part once u get this working. Cheers
Re: Vb.net Code To Display Image In Picturebox From Sql Server by Omosilade: 6:32pm On Nov 24, 2009
Hi Ababoi,

Weldone. But i need second part of the code, and how to modify and optimise the code. Please, let the second part follow the trent you used in the first part. I need it urgently. demola4infotekk@yahoo.com
Re: Vb.net Code To Display Image In Picturebox From Sql Server by ABAboi: 9:08pm On Nov 24, 2009
Omosilade:

Hi Ababoi,

Weldone. But i need second part of the code, and how to modify and optimise the code. Please, let the second part follow the trent you used in the first part. I need it urgently. demola4infotekk@yahoo.com
other ways you can modify the code includes searching and displaying an image using the values of a textbox.
The second part of the code is to display the image from the database (assuming that you have actually created the imageData table and contain values). You will need
picturebox, image1,
textbox , textbox1
then
under the button to display the image ::
'Read an image
Using conn As New System.Data.SqlClient.SqlConnection("[s]Data Source=DENSMAN-PC\SQLEXPRESS;Initial Catalog=aba;Integrated Security=True[/s]"wink 'insert ur connection string
conn.Open()
Using cmd As New SqlClient.SqlCommand("Select Name, CreateDate, Picture From ImageData", conn)
Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
Using dt As New DataTable
dt.Load(dr)
Dim row As DataRow = dt.Rows(0)
Dim sName As String = Convert.ToString(row("Name"wink)
Dim dtCreateDate As DateTime = Convert.ToDateTime(row("CreateDate"wink)
Using ms As New IO.MemoryStream(CType(row("Picture"wink, Byte()))

Dim img As Image = Image.FromStream(ms)
Image1.Image = img
End Using
TextBox1.Text = ("Loaded image " + sName)
End Using
End Using
End Using
End Using

Goodluck try it and if not working reply and show ur error. Else notify when it is working

End Sub
Re: Vb.net Code To Display Image In Picturebox From Sql Server by dotzok: 11:06pm On Nov 25, 2009
Salamsy aint sure these guys really understand your needs, perhaps I, too. 88 hours with ADO.NET might not fetch u this. There are 2 ways:

1. You can save/retrieve image directly into/from a table in ur SQL db (the image must have been converted to binary type).

2. You can also save the path or filename of the JPEG images as normal strings (Varchar); this is much easier and looks like what ur querry was driving at.

Option 1 is more advanced and seems a little more complicated; but it's actually the real life thing. There has been arguments about which is better from professionals' point of view and they raise sensible points.

Have it at the back of ur mind that saving image in ur database contributes immensely to the rapid growth of ur DB size.

And, Is ur app an Enterprise one or get-me-on?

Regards
Re: Vb.net Code To Display Image In Picturebox From Sql Server by tashiduks: 1:23pm On Feb 12, 2012
hi ABAboi ,

Thanks for your coding which really helped me. Continuation to your post, i would like to request you to kindly include a code for SAVE button, since you have included this command in SEARCH button. Can you please add a code for SEARCH button to load the file to picture box and SAVE button to save the information to TABLE.

Thanks,
Re: Vb.net Code To Display Image In Picturebox From Sql Server by koladata(m): 7:04pm On Sep 26, 2013
Thanks alot for this code, it really helped.
But can you please give us code to delete, it won't allow the usual delete method i'm used to, the delete code keeps ti ming out probably because of the picture involved now. Please i would appreciate if you could give a delete code. Thank you sir.
Re: Vb.net Code To Display Image In Picturebox From Sql Server by Nobody: 2:59pm On Nov 05, 2015
ABAboi:

other ways you can modify the code includes searching and displaying an image using the values of a textbox.
The second part of the code is to display the image from the database (assuming that you have actually created the imageData table and contain values). You will need
picturebox, image1,
textbox , textbox1
then
under the button to display the image ::
'Read an image
Using conn As New System.Data.SqlClient.SqlConnection("[s]Data Source=DENSMAN-PC\SQLEXPRESS;Initial Catalog=aba;Integrated Security=True[/s]"wink 'insert ur connection string
conn.Open()
Using cmd As New SqlClient.SqlCommand("Select Name, CreateDate, Picture From ImageData", conn)
Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
Using dt As New DataTable
dt.Load(dr)
Dim row As DataRow = dt.Rows(0)
Dim sName As String = Convert.ToString(row("Name"wink)
Dim dtCreateDate As DateTime = Convert.ToDateTime(row("CreateDate"wink)
Using ms As New IO.MemoryStream(CType(row("Picture"wink, Byte()))

Dim img As Image = Image.FromStream(ms)
Image1.Image = img
End Using
TextBox1.Text = ("Loaded image " + sName)
End Using
End Using
End Using
End Using

Goodluck try it and if not working reply and show ur error. Else notify when it is working

End Sub

Ababoi u r 2 much, tumbs up 4 ur codes.
Pls I hav bin looking 4 a way 2 to link an invoice app to sql server database so as manage my small business inventory. I' ll be glad if u will help me with that, tanx in advance.
Re: Vb.net Code To Display Image In Picturebox From Sql Server by Nobody: 3:10pm On Nov 05, 2015
dotzok:
Salamsy aint sure these guys really understand your needs, perhaps I, too. 88 hours with ADO.NET might not fetch u this. There are 2 ways:

1. You can save/retrieve image directly into/from a table in ur SQL db (the image must have been converted to binary type).

2. You can also save the path or filename of the JPEG images as normal strings (Varchar); this is much easier and looks like what ur querry was driving at.

Option 1 is more advanced and seems a little more complicated; but it's actually the real life thing. There has been arguments about which is better from professionals' point of view and they raise sensible points.

Have it at the back of ur mind that saving image in ur database contributes immensely to the rapid growth of ur DB size.

And, Is ur app an Enterprise one or get-me-on?

Regards

Kudoz 2 ur contributions. Pls I'm havin challenges creating and managing my small business inventory system. I' ll be most grateful if u aid me on how to go about creating invoice and linking it to my SQL server database. Tnx
Re: Vb.net Code To Display Image In Picturebox From Sql Server by King1982: 8:28am On Dec 26, 2019
Please I need someone who can help out with ID card with barcode software such that the details of the ID card owner can be gotten if the Card is scanned.you must be base in porthacourt,it's urgent.contact me..07035812808
Re: Vb.net Code To Display Image In Picturebox From Sql Server by Thenaijaitguy: 5:12pm On Dec 27, 2019
King1982:
Please I need someone who can help out with ID card with barcode software such that the details of the ID card owner can be gotten if the Card is scanned.you must be base in porthacourt,it's urgent.contact me..07035812808

Chatting you already .

(1) (Reply)

Connecting To A Database Using Visual Basic / Game Development: Join The Team / For Computer Science Students

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 33
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.