Results 1 to 16 of 16

Thread: upload and save picuter to database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108

    Unhappy upload and save picuter to database

    i got a question about the upload a pic and save it to database

    can anyone show me how to do the process of upload a picture to store it to the database. and when i need to use the picture i can retrieve from database ? can give me an example of it ? thank you ~~

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    'Try this! It works for me.

    'To load a picture from file to a picture box.

    Me.PictureBox1.Image=Image.FromFile("c:\fileName.gif")

    'To save picture to database
    'in my own case, I used a typed-dataset and
    'a Microsoft SQL Server 2000 as the datastore.
    'in the dataset the column for the photograph is
    'of type 'base64Binary' and of type 'image' in the database.



    Dim bytPhoto() As Byte
    Dim memStream As New IO.MemoryStream()

    Me.PictureBox1.Image.Save(memStream, Drawing.Imaging.ImageFormat.Gif)
    'you can specify image format of choice
    bytPhoto = memStream.ToArray()

    'you can assign the variable 'bytPhoto' to the appropriate
    'column of the dataset datarow
    'in my case


    dstEmpData.Employees.Photograph = bytPhoto
    'where dstEmpData is the dataset,
    'Employees and Photograph represents the
    'datatable and datarow respectively.

    'you can update the database as usual.


    'To display the photograph from the
    'database in a picture box try this:


    Dim memStream As New IO.MemoryStream()
    Dim bytPhoto() As Byte

    bytPhoto = row.Photograph
    memStream.Write(bytPhoto, 0, bytPhoto.Length)
    Me.PicturBox1.Image = Image.FromStream(memStream)
    memStream.Close()

    'row represents the employee row in the datatable in the dataset
    'Photograph is the column in the datarow.
    'Don't forget, I used a typed dataset.

    I HOPE IT WILL BE OF HELP

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108

    Question

    If I want to save the image in to Access database wat is the data type ?

  4. #4
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Although I've not tried it with VB.NET, with VB6 OLE Object was the suitable data type. It should still work, I believe.

    I'll try it out myself but at the moment I'm not with my machine.

    I'll like to know your progress.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108

    problem !!!

    soli to disturb again

    here is my coding to save a picture because i didnt use the dataset to store my information so how can i do ?

    ==============================================
    this is open and select image

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Reading a picture and put it in a bytearray
    If fo.ShowDialog = DialogResult.OK Then
    Dim fs As New IO.FileStream(fo.FileName, _
    IO.FileMode.Open)
    Dim br As New IO.BinaryReader(fs)
    abyt = br.ReadBytes(CInt(fs.Length))
    br.Close()
    'just to show the sample without a fileread error
    Dim ms As New IO.MemoryStream(abyt)
    Me.PictureBox1.Image = Image.FromStream(ms)
    End If

    End Sub

    =============================================
    after that when i click the save button

    Me.PictureBox1.Image.Save(memStream, Drawing.Imaging.ImageFormat.Gif)
    'you can specify image format of choice
    bytPhoto = memStream.ToArray()

    Dim sql = "Insert Into Employee (efirstname,elastname,ephone,eaddress,eaddress1,eaddress2,eemail,empusername,emppassword,picture) Values ('" & Me.empfirstname.Text & "','" & Me.emplastname.Text & "','" & Me.empphone.Text & "','" & Me.empaddress.Text & "','" & Me.empaddress1.Text & "','" & Me.empaddress2.Text & "','" & Me.empemail.Text & "','" & Me.username.Text & "','" & Me.password.Text & "', ' & bytPhoto & ')"


    from here donno i store the right things or not coz i can store the variable into the database

    ==============================================
    when i display

    Dim sql = "SELECT * FROM Employee WHERE efirstname='" & Me.EFname.Text & "' And elastname='" & Me.Elname.Text & "'"

    cmd = New OleDbCommand(sql, conn)

    Dim dr As OleDbDataReader = cmd.ExecuteReader

    While dr.Read

    Me.empfirstname.Text = dr("efirstname")
    Me.emplastname.Text = dr("elastname")
    Me.empphone.Text = dr("ephone")
    Me.empaddress.Text = dr("eaddress")
    Me.empaddress1.Text = dr("eaddress1")
    Me.empaddress2.Text = dr("eaddress2")
    Me.empemail.Text = dr("eemail")
    bytPhoto = dr("picture")

    End While

    memStream.Write(bytPhoto, 0, bytPhoto.Length)
    Me.PictureBox1.Image = Image.FromStream(memStream)
    memStream.Close()

    from here i got use the dataset and the dataset name is DataSet121 but here i cannot display coz it say me invalid parameter thanx for helping me

  6. #6
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    If you run your code in debug mode, at what line does it throw the exception and what is the exact message.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    sorry to reply u late
    it just tell me invalid parameter

  8. #8
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    why not wrap your code with the Try...Catch block. something like:

    VB Code:
    1. Try
    2.     '... your code here
    3.  
    4. Catch ex as System.Exception
    5.     MessageBox.Show(ex.Message.ToString())
    6. End Try

    By using this, you can get the stack trace of the exception that is thrown and the line number. It will help a greate deal in debugging your code.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    it just pop up a message

    Invalid Parameter Used

    than nothing already
    thanx

  10. #10
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Sorry, I made a mistake.

    Use this
    VB Code:
    1. Try
    2.  
    3.     '... your code here
    4.  
    5. Catch ex as System.Exception
    6.     MessageBox.Show(ex.ToString())
    7. End Try

    Instead of my earlier code
    VB Code:
    1. Try
    2.  
    3.     '... your code here
    4.  
    5. Catch ex as System.Exception
    6.     MessageBox.Show(ex.Message.ToString())
    7. End Try

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108

    soli to reply you late.

    here is the problem

    i paste it to the Attach file
    thanx
    Attached Images Attached Images  

  12. #12
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    At what point is the exception thrown? Is it when loading the pix from file or when displaying from database?

    Can you post DisplayEmployee.vb? If you cannot, what's the code on line 552?

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    Try

    Dim sql = "SELECT * FROM Employee WHERE efirstname='" & Me.EFname.Text & "' And elastname='" & Me.Elname.Text & "'"

    cmd = New OleDbCommand(sql, conn)

    Dim dr As OleDbDataReader = cmd.ExecuteReader

    While dr.Read

    Me.empfirstname.Text = dr("efirstname")
    Me.emplastname.Text = dr("elastname")
    Me.empphone.Text = dr("ephone")
    Me.empaddress.Text = dr("eaddress")
    Me.empaddress1.Text = dr("eaddress1")
    Me.empaddress2.Text = dr("eaddress2")
    Me.empemail.Text = dr("eemail")
    Me.bytPhoto = dr("picture")

    End While

    memStream.Write(bytPhoto, 0, bytPhoto.Length)
    Me.PictureBox1.Image = Image.FromStream(memStream)
    memStream.Close()

    dr.Close()
    conn.Close()

    Catch ex As Exception
    MessageBox.Show(ex.ToString())
    End Try

    here is the Coding part

    and the 552 is this part

    Me.PictureBox1.Image = Image.FromStream(memStream)

  14. #14
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Hi ninjax,

    if you check you insert statement, the photograph 'bytPhoto' is within single quotes. Single quotes should only be used for strings. Remove the quotes and try again.

    If your code fragment below, I have highlighted the bytPhoto segment.

    Replace this code:
    VB Code:
    1. Dim sql = "Insert Into Employee (efirstname, elastname, ephone, eaddress, eaddress1, eaddress2, eemail, empusername, emppassword, picture) Values ('" & Me.empfirstname.Text & "', '"  Me.emplastname.Text & "','" & Me.empphone.Text & "','" & Me.empaddress.Text & "','" & Me.empaddress1.Text & "','" & Me.empaddress2.Text & "','" & Me.empemail.Text & "','" & Me.username.Text & "','" & Me.password.Text & "', [B]'" & bytPhoto & "'[/B])"

    WITH THIS
    VB Code:
    1. Dim sql = "Insert Into Employee (efirstname,elastname,ephone,eaddress,eaddress1,eaddress2,eemail,empusername,emppassword,picture) Values ('" & Me.empfirstname.Text & "','" & Me.emplastname.Text & "','" & Me.empphone.Text & "','" & Me.empaddress.Text & "','" & Me.empaddress1.Text & "','" & Me.empaddress2.Text & "','" & Me.empemail.Text & "','" & Me.username.Text & "','" & Me.password.Text & ", [B]" & bytPhoto & "[/B])"


    Please, let me know if it works.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    sorry, the code that you ask me to change is got error

    error is operator & is not define type string and '1 - dimensional' array of byte

  16. #16
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    That's to tell you that with your earlier code, you were trying to convert string to image.

    I'll suggest you try using Parameters with you command object. If performance is not critical, you may use the command builder with a dataset object.

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width