Displaying Picture in dataBound Form.

Rat

Well-known member
Joined
Aug 2, 2005
Messages
72
Location
Somewhere between 0 and 1
Programming Experience
1-3
I have done all the databinding for the textboxes for the program attached below .. but i'm stuck at the last part ..

Suppose, the user wants to add a image for the Product Information which is being displayed .. the image/filename should go into that particular row's "FileName" and "Image" column ... First, the user clicks for browse .. selects the file, and then clicks save .. and all of the above mentioned goes into action .. I have no clue on how to go about doing the saving part ..

The "FileName" column of type : Text
and the "Image" colum is of type : OLE Object

Thanks, in advance.

EDIT : I forgot one thing, when the program is started again, and if there happens to be a image for the particular product, which was saved before ... it should show.

Anyone ?
 

Attachments

  • Scr.JPG
    Scr.JPG
    39.6 KB · Views: 60
Last edited by a moderator:
Ok if this is a local database ie your not trying to uploafd the file you dont need to save you jusy need to tell it where the image is and update the record or save the record then it should work
 
Tripic said:
Ok if this is a local database ie your not trying to uploafd the file you dont need to save you jusy need to tell it where the image is and update the record or save the record then it should work

Yes, it's a local database ... how would I go about doing that ?
 
Just take a look at this post, http://www.vbdotnetforums.com/showpost.php?p=9777&postcount=3 actually get the attached project and see how it works with image paths. Finally how to insert (in your case update as records is already done) the path into DB: i.e. join this code to the save button (before pass ofdfile to the hiden textbox named say myPath If ofd.ShowDialog = DialogResult.Ok then myPath.text = ofd.file

later add this to the save button:
{...}
strSQL = "UPDATE myTABLE SET imagePath='" & myPath.Text & "' WHERE ID=" & clientID.Text & ""
{...}
I hope you got the idea now

Cheers ;)
 
kulrom said:
Just take a look at this post, http://www.vbdotnetforums.com/showpost.php?p=9777&postcount=3 actually get the attached project and see how it works with image paths. Finally how to insert (in your case update as records is already done) the path into DB: i.e. join this code to the save button (before pass ofdfile to the hiden textbox named say myPath If ofd.ShowDialog = DialogResult.Ok then myPath.text = ofd.file

later add this to the save button:
{...}
strSQL = "UPDATE myTABLE SET imagePath='" & myPath.Text & "' WHERE ID=" & clientID.Text & ""
{...}
I hope you got the idea now

Cheers ;)

Okay bro, thanks ... the "Save" issue is settled now..

Let's say, when i'm browsing through the database, and a certain product doesn't have a image added yet, how do i do a check to make sure that the DisplayPicture() doesn't throw an exception .. maybe, if i could check i can put in a "EMPTY.jpg" or something ... How do i check for a empty column for that corresponding product ?

I get the following error, when I click next to proceed on to the next record, which doesn't have a image. The code for DisplayPicture() is as follows ...

VB.NET:
Private Sub DisplayImage()
		Try

 strPic=DsProducts2.Tables("Products").Rows(cmCounter.Position).Item("FileName")
			    pbDisplay.Image = Image.FromFile(strPic)


		Catch ex As Exception
		    MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, "Exception Message")
		End Try
	End Sub
 

Attachments

  • scr.JPG
    scr.JPG
    41.7 KB · Views: 53
Last edited:
kulrom said:
before all retrieve the path to the textbox1 and say on_changedText event add image to the PictureBox if textBox1 is empty then
{...}
Me.PictureBox1.Image = Nothing
Else
Me
.PictureBox1.Image = Image.FromFile(textBox1.text)

I hope you got me ... Cheers ;)

I don't get you bro .. :(

Because, now i've binded the dataset to txtPath, so when there is a image in that associated row, it shows .. if there isn't a image, it doesn't show ... but the problem now is, it crashes when it goes over to a row without a image.
 
You need to check that the value of the Filename is not DbNull.Value or an empty string.
VB.NET:
Private Sub DisplayImage()
    Dim strPic As String
    Try
        Dim PicPath As Object = _
          DsProducts2.Tables("Products").Rows(cmCounter.Position).Item("FileName")
        If Not PicPath Is System.DBNull.Value AndAlso Not PicPath Is String.Empty Then
            pbDisplay.Image = Image.FromFile(PicPath.ToString)
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Exception Message", MessageBoxButtons.OK, _
              MessageBoxIcon.Error)
    End Try
End Sub
Please try to use more descriptive titles for your future posts. It makes searches simpler and also helps search engine awareness. Thanks.
 
Paszt dude,
I don't think so that you need to check the path is DBNull. Maybe it's better if you simply fetch the path like me.TextBox1.text = objread("path) & "" 'the double quotes guaranty that there wouldn't be an exception but rather an empty string. ANd finally you just stick for the textbox and check if it's empty or not and take an action respectively.
I'm saying this cuz checking DBNull is wasting your resources. Btw, how are you? I haven't seen you for ages ... busy again?

Cheers ;)
 
Back
Top