1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to insert picture...

Discussion in 'C#' started by alun7, Jun 29, 2007.

  1. #1
    how do i upload a picture path into the databae and display it out ??? pls help me. .thanks
     
    alun7, Jun 29, 2007 IP
  2. N_F_S

    N_F_S Active Member

    Messages:
    2,475
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #2
    so you need to insert into db picture path or actually upload a picture via a form ?
     
    N_F_S, Jun 30, 2007 IP
  3. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #3
    when uploading it you are presumably storing them in a predefined folder and therefore simply store the path relative to the root in the db with the file name or alternatively if they are all in a single location then there is only a need to store the filename as the path will be the same in all cases.
     
    AstarothSolutions, Jun 30, 2007 IP
  4. yugolancer

    yugolancer Well-Known Member

    Messages:
    320
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #4
    storing path into the database is pretty easy task. You store the path as any other string value. Your database field should be varchar(xx) for MSSQL or text datatype if you are using MSAccess as backend.
    means INSERT INTO table1 (Path) VALUES ('" & txtImagePath.Text & "') would be just fine ... assuming that txtImagePath holds the path value.
    Also displaying an image according the path stored into db is very simple:
    
    Dim thePath as String
    strSQL = "SELECT Path FROM Table1 WHERE ..."
    {...} 
    thePath = commandobject.ExecuteScalar()
    
    'show the image inside the pictureBox
    Me.PictureBox.Image = Image.FromFile(thePath)
    
    Code (markup):
    HTH
    Regards ;)

    p.s. please notice that this is VB.NET code so if you need C# version i would be glad to provide you the C# code too.
     
    yugolancer, Jul 7, 2007 IP
  5. yugolancer

    yugolancer Well-Known Member

    Messages:
    320
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Btw, i forgot to mention that you can also store the image entirely too.
    You can store the file as Binary large object (BLOB) storage in database.
    For example:

    Private Sub File2OleDbBlob(ByVal SourceFilePath As String) 
            Try 
                oledbcon.Open() 
                strSQL = "INSERT INTO Users (UserPhoto) VALUES(@BLOBData)" 
                Dim fs As New System.IO.FileStream(SourceFilePath, IO.FileMode.Open, IO.FileAccess.Read) 
                Dim b(fs.Length() - 1) As Byte 
                fs.Read(b, 0, b.Length) 
                fs.Close() 
                cmd = New OleDbCommand(strSQL, oledbcon) 
                Dim P As New OleDbParameter("@BLOBData", OleDbType.LongVarBinary, b.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, b) 
                cmd.Parameters.Add(P) 
                cmd.ExecuteNonQuery() 
            Catch ex As Exception 
                MsgBox(ex.Message) 
            Finally 
                oledbcon.Close() 
            End Try 
        End Sub
    Code (markup):
    It's very easy to read the image back and display in the pictureBox too ... but as you probably don't need this way i will avoid to show you how.

    Regards :)
     
    yugolancer, Jul 7, 2007 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    To insert into a blob, you need to convert to byte array (as in byte()). I also advise doing it via Stored PRoc
     
    ccoonen, Jul 7, 2007 IP
  7. yugolancer

    yugolancer Well-Known Member

    Messages:
    320
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Ccoonen,
    Actually I don't see any difference if you do that via SP's ... the issue about Inserting BLOB data is that in this way you can dramatically increase the size of your database. You're better off storing the path(s) to the DB.

    Regards ;)
     
    yugolancer, Jul 8, 2007 IP
  8. Aztral

    Aztral Well-Known Member

    Messages:
    344
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    125
    #8
    I agree with Yugo. I been doin this stuff awhile...storing pics in db is just not worth the effort.
     
    Aztral, Jul 9, 2007 IP