Results 1 to 6 of 6

Thread: Insert Picture from imagebox using vb6

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Lightbulb Insert Picture from imagebox using vb6

    Can any body tell me how can I insert picture into sql server db through vb6
    i have imagebox on form and this imagebox's picture i wanted to save in the database

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Insert Picture from imagebox using vb6

    Quote Originally Posted by yousufkhan73 View Post
    Can any body tell me how can I insert picture into sql server db through vb6
    i have imagebox on form and this imagebox's picture i wanted to save in the database
    You will have to define an "ImageBlob-Field" in your SQLServer-Table -> as a Field of Type: varbinary(max)

    If you have that Field (with the correct Type) defined in your Table,
    all you have to do with that Field (later in an ADO-Rs), is to read-out or assign VB-ByteArrays to it.

    And no, the ByteArray you will feed into that ADO-Rs-Field will not have to be "converted from an ImageBox" -
    instead what you want to feed into the Field is the ByteArray you used, to initially fill your ImageBox in the first place
    (e.g. the ByteArray you've just received from an XML-WebRequest, after the Base64-decoding of the XML-Image-Content was done).

    Olaf

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: Insert Picture from imagebox using vb6

    I tried like this
    kindlly correct where i am wrong
    Code:
    Dim strStream As ADODB.Stream
    
           Set strStream = New ADODB.Stream
            strStream.Type = adTypeBinary
            strStream.Open
           
            strStream.LoadFromFile ImgPtn.Picture
            conn.Execute ("Insert into PtnMstDtl(SELECT Bulkcolumn FROM  OPENROWSET(BULK strStream.Read,SINGLE_BLOB) IMG_DATA)")

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Insert Picture from imagebox using vb6

    Your code-snippet above does not make sense to me (you were not reading what I wrote).

    I was talking about an ADO-Recordset (not an ADO-Stream) as the "Insert-Into-DB-Helper-Object" -
    and also about a VB-ByteArray (the one you used, to fill your Image-Control at some time before)...

    Somewhere in your Code there is a line, where ImgPtn.Picture is "on the left hand side of an equal-sign".

    Code:
       ImgPtn.Picture = ...
    Let's start there ...
    Please post a few concrete preceding code-lines which come before the above Image-assignment-statement.
    (the reason behind my request is, that in all likelihood - you already *have* such an ImageBytes-ByteArray in your code,
    you just need to "remember it" in a Variable with appropriate scope, to be able to make your later happening Rs-based insert more easily).

    Olaf

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: Insert Picture from imagebox using vb6

    this are my code for getting picture from xml to imagebox

    Code:
    Dim DOM As MSXML2.DOMDocument
              Dim elemPht As MSXML2.IXMLDOMElement
                     
              With New MSXML2.DOMDocument
                      .loadXML (XMLurl)
                  Set elemPht = .getElementsByTagName("Pht").Item(0)
              End With
            'Indent printing:
              ScaleLeft = -120
              CurrentX = 0
              AutoRedraw = True
              elemPht.DataType = "bin.base64"
              ImgPtn.Picture = PicBytes.LoadPicture(elemPht.nodeTypedValue)

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Insert Picture from imagebox using vb6

    Quote Originally Posted by yousufkhan73 View Post
    this are my code for getting picture from xml to imagebox

    Code:
    Dim DOM As MSXML2.DOMDocument
              Dim elemPht As MSXML2.IXMLDOMElement
                     
              With New MSXML2.DOMDocument
                      .loadXML (XMLurl)
                  Set elemPht = .getElementsByTagName("Pht").Item(0)
              End With
            'Indent printing:
              ScaleLeft = -120
              CurrentX = 0
              AutoRedraw = True
              elemPht.DataType = "bin.base64"
              ImgPtn.Picture = PicBytes.LoadPicture(elemPht.nodeTypedValue)
    Ok, in the Declaration-Section at the top of your Form, you will have to introduce a new ByteArray-Variable (I've marked diff-changes in magenta):
    Code:
    Private ImgPtnBytes() As Byte 'define that in the declaration-section
    
    '... other Form-Code ...
    
    
      Dim DOM As MSXML2.DOMDocument
      Dim elemPht As MSXML2.IXMLDOMElement
             
      With New MSXML2.DOMDocument
              .loadXML (XMLurl)
          Set elemPht = .getElementsByTagName("Pht").Item(0)
      End With
      'Indent printing:
      ScaleLeft = -120
      CurrentX = 0
      AutoRedraw = True
      elemPht.DataType = "bin.base64"
      ImgPtnBytes = elemPht.nodeTypedValue 'store the decoded Bytes here (for later usage)
      ImgPtn.Picture = PicBytes.LoadPicture(ImgPtnBytes) 'reflect the ImageBytes in the PicBox
    
    '...
    With that in place, you will now only have to select a single Record over an ADO-Recordset from your DB-Table - and insert (or update) the ImgBytes this way:
    Code:
    Dim Rs As ADODB.Recordset
    Set Rs = SelectSingleRecordForInsertOrUpdate("MyTable")
        Rs!SomeTextField.Value = txtControl.Text
        Rs!YourImageBlobField.Value = ImgPtnBytes 
        Rs.Update
    
    
    'a helper-function which opens either an existing record (over the optional ID-Field) for Updates,
    'or a non-existing Record (when no ID is given) for Inserts (due to an automatically applied .AddNew)
    Private Function SelectSingleRecordForInsertOrUpdate(TableName As String, Optional ByVal ID as Long) As ADODB.Recordset
      Set SelectSingleRecordForInsertOrUpdate= New ADODB.Recordset
          SelectSingleRecordForInsertOrUpdate.CursorLocation = adUseClient
          SelectSingleRecordForInsertOrUpdate.Open "Select * From [" & TableName & "] Where ID=" & ID, YourCnn, adOpenStatic, adLockOptimistic
          If SelectSingleRecordForInsertOrUpdate.RecordCount = 0 Then SelectSingleRecordForInsertOrUpdate.AddNew
    End Function
    HTH

    Olaf

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