4 Replies - 7269 Views - Last Post: 23 April 2012 - 07:27 AM Rate Topic: -----

#1 hosny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 12

how to store image and retreive it from sql server 2005 with vb.net

Posted 22 April 2012 - 06:57 AM

Hi,

Could you please enlighten me with steps of how to overcome an error problem which really makes me stuck at all.
Well, am using vb.net 2005 and sql server...in vb.net, i was using picturebox.text to capture image from the user.

Am also using Bindings and tableadapters to generate the information from sql server..my server acts as designated server which lots of computers are using my server database...the sql server allows remote computers to share the database i have it in my server.

As far i did for my best, i created a table consisting 12 fields. 3 of them are images...their data types are images.

in vb.net, i have a save button which acts two multitasking functions..so i put the code for store image in the save button:1. task:

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
Private Sub addfile()
        Try
            Dim ms As New MemoryStream
            Legal_PhotoPictureBox.Image.Save(ms, Legal_PhotoPictureBox.Image.RawFormat)
            Dim photoimage() As Byte = ms.GetBuffer
            Dim signatureimage As Byte = ms.GetBuffer
            ms.close()
            Me.CustomersTableAdapter.Insert(Customer_IDTextBox.Text, FnameTextBox.Text, Other_nameTextBox.Text, Date_Of_BirthDateEdit.Text, Mobile_TelephoneTextBox.Text, Home_TelephoneTextBox.Text, Physical_AddressTextBox.Text, Postal_AddressTextBox.Text, EmailTextBox.Text, GenderComboBox.Text, CityComboBox.Text, NationalityComboBox.Text, PicturePictureBox.Image, signatureimage, Physical_Marks_signs_TextBox.Text, L_DocumentsComboBox.Text, photoimage, User_processorTextBox.Text)

        Catch ex As Exception

        End Try


2. second task for save button:
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CustomerRegisterDataSet)


Therefore, am getting error messages if i remove one of the fields in the me.customerstableadapter.insert(customerID.text,....blah...upto....photoimage)...

Anyway, how can i shorten that insert table from the code...since the 2 task for save button saves all the information barring images, how can i remove others and put only the image in that customerttabledapter.insert(photoimage)

Or if you have another cool codes which suits my issue, please post it.

Or help me another easier methods of saving and retrieving images from sql server database with vb.net


Please note:
I have got a table consists of more than 12 fields, so methods which only save the image in that table only would really be useful.

Thanks so much for reading my thread...and sorry for spelling error.

Best Regards,
Hosny

This post has been edited by Atli: 22 April 2012 - 07:50 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: how to store image and retreive it from sql server 2005 with vb.net

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8954
  • View blog
  • Posts: 33,563
  • Joined: 12-June 08

Re: how to store image and retreive it from sql server 2005 with vb.net

Posted 22 April 2012 - 08:57 AM

The image datatype is being/has been depreciated. Use a varbinary(max) data type... and from there just take your image - convert it to a binary array... save it... to pull it out just operate in the reverse. I have a snippet that shows you how to do this..
Was This Post Helpful? 0
  • +
  • -

#3 hosny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 12

Re: how to store image and retreive it from sql server 2005 with vb.net

Posted 22 April 2012 - 11:28 PM

View Postmodi123_1, on 22 April 2012 - 08:57 AM, said:

Use a varbinary(max) data type... and from there just take your image - convert it to a binary array... save it... to pull it out just operate in the reverse.


Hi,

Could you please explain it how to convert in to binary and save it in the database...and of course how to retreive it from the database.

Well, like i said that am using Bindings and tableadapters to store and retreive information from database.
So, i have a form in vb called customers. it has a database called custeomers in database which consists of CusetomerID, Fname, othername, age, telephone, address, city, nationality, gender, picture, anotherpicture.

So based on the issue i have in vb.net and sql server, how do you do this? OR could you please edit something applying to my scenario?...

In vb.net, am using pictureboxes to assign the images in database, so do i remove it?

Please am totally naive at this point and am begging you to help me out of this.

For example, your example goes here and i would like to apply my scenario telling me what to do?

Dim myByteArray() As Byte = Nothing

Dim sLocation As String = "inset location here"
Dim oFileStream As New FileStream(sLocation, FileMode.Open)

Try
myByteArray = New Byte(oFileStream.Length - 1) {}
oFileStream.Position = 0
oFileStream.Read(myByteArray, 0, oFileStream.Length - 1)
Catch ex As Exception
MsgBox(ex.Message)
Finally
oFileStream.Dispose()
End Try
Was This Post Helpful? 0
  • +
  • -

#4 hosny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 12

Re: how to store image and retreive it from sql server 2005 with vb.net

Posted 22 April 2012 - 11:34 PM

Please note that the database am using acts as server and it allows several computers to store and retrieve their information of applicatoin program from database..

So, how would you insert location in the code you mentioned?I mean, you said that :Provide an absolute file path to your file you want to convert to a byte array.

In my case where everycomputers takes his own information from everywhere and putting into my database, what would you do?


Please i need help
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8954
  • View blog
  • Posts: 33,563
  • Joined: 12-June 08

Re: how to store image and retreive it from sql server 2005 with vb.net

Posted 23 April 2012 - 07:27 AM

Quote

Could you please explain it how to convert in to binary and save it in the database...

That snippet should have shown you how to get a byte array.. then it's a matter of passing it into a plain-jane method that takes variables and inserts them into a db. The binary array isn't anything more spooky than an integer or a string!

Quote

and of course how to retreive it from the database.

We have quite a few great db interaction tutorials here!
OleDb Basics in VB.Net Rate Topic

A Really Simple Database Create a Database using Access & VB.net Express 2008

Quote

Well, like i said that am using Bindings and tableadapters to store and retreive information from database.

Okay then you'll have to deviate from that for a bit to get this done.

Quote

OR could you please edit something applying to my scenario?...

I wouldn't dare think about depriving you of this learning experience and just HANDING you the code!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1