|
Thanks for friends that reply my post before....
I am binding a picture box to a path... It works fine if the path is valid. Problem comes when tha path is invalid or empty...
The code that is red highlighted is where the error come out..
Private Sub BindFields()
Try
Dim b As Binding = New Binding("Image", objDView, "CoverSample")
AddHandler b.Format, AddressOf MyPictureBox_FormatImage
[B]picBook.DataBindings.Add(b)[/B]
Catch ex As Exception
MsgBox(ex.Message, , "BindFields")
End Try
End Sub
Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
If Trim(e.Value.ToString) <> "" Then
Dim fi As New System.IO.FileInfo(e.Value.ToString)
If fi.Exists() Then
e.Value = Image.FromFile(e.Value.ToString)
isValidPath = True
End If
End If
Catch ex As Exception
MsgBox(ex.Message, , "MyPictureBox_FormatImage")
End Try
End Sub
Learning .NET
|
|
|
|
|
You didn't specify what the error was or where it occured. There was no highlighted text anywhere.
But, I see a problem with the way your trying to validate if a file exists or not. Check to see if the desired conversion if valid before you try and validate that the file is there, which could be done much simpler.
Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
' Check to see if this is a valid conversion.
If Not e.DesiredType Is GetType(Image) Then
MsgBox("FormatImage was called with the DesiredType of " & _
e.DesiredType.ToString, , "MyPictureBox_FormatImage")
Exit Sub
End If
' Check if the file exists (assumes "Imports System.IO").
' Uses the Static version of the Exists method.
Try
If Not File.Exists(e.Value.ToString) Then
MsgBox("File not found: " & e.Value.ToString, , "MyPictureBox_FormatImage")
Exit Sub
Else
' The file exists, try to create an Image object out of it.
e.Value = Image.FromFile(e.Value.ToString)
End If
Catch ex As Exception
MsgBox(ex.Message, , "MyPictureBox_FormatImage")
End Try
End Sub
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Private Sub BindFields()
Try
Dim b As Binding = New Binding("Image", objDView, "CoverSample")
AddHandler b.Format, AddressOf MyPictureBox_FormatImage
'This is the error since it binds to an empty path or invalid path
picBook.DataBindings.Add(b)
Catch ex As Exception
MsgBox(ex.Message, , "BindFields")
End Try
End Sub
Thanks for your reply
Learning .NET
|
|
|
|
|
OK. I've put together a quick Binding example. This code sits inside a form with a single PictureBox on it:
Imports System.IO
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form
' Windows Form Designer code not included...
Private myImage As myImageClass
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myImage = New myImageClass
myImage.Path = "D:\Pepper.JPG"
BindFields()
End Sub
Private Sub BindFields()
Try
Dim b As Binding = New Binding("Image", myImage, "Path")
AddHandler b.Format, AddressOf MyPictureBox_FormatImage
PictureBox1.DataBindings.Add(b)
Catch ex As Exception
MsgBox(ex.Message, , "BindFields")
End Try
End Sub
Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
' Check to see if this is a valid conversion.
If Not e.DesiredType Is GetType(Image) Then
MsgBox("FormatImage was called with the DesiredType of " & _
e.DesiredType.ToString, , "MyPictureBox_FormatImage")
Exit Sub
End If
' Check if the file exists (assumes "Imports System.IO").
' Uses the Static version of the Exists method.
Try
If Not File.Exists(e.Value.ToString) Then
MsgBox("File not found: " & e.Value.ToString, , "MyPictureBox_FormatImage")
Exit Sub
Else
' The file exists, try to create an Image object out of it.
e.Value = Image.FromFile(e.Value.ToString)
End If
Catch
End Try
Catch ex As Exception
MsgBox(ex.Message, , "MyPictureBox_FormatImage")
End Try
End Sub
End Class
Public Class myImageClass
Private m_Path As String = ""
Public Property Path() As String
Get
Return m_Path
End Get
Set(ByVal Value As String)
m_Path = Value
End Set
End Property
End Class
If the file doesn't exist, what you think should happen does. You'll get two MessageBox's, the first, from MyPictureBox_FormatImage , saying the File does not exist, and the second, from BindFields , saying Invalid cast from System.String to System.Drawing.Image. Since the file wasn't found, the Bind failed and was appropriately caught by the BindFields exception handler.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I have tried your code, it can catch the exception. Two message box will display, but when i using your code in my app, it doesn't work. It only display, "File not found" message box. and then it will have an error in "PictureBox1.DataBindings.Add(b)" cause it bind to an empty path. This one doesn't display message box. The error is not caught although i have using try catch function between the "PictureBox1.DataBindings.Add(b)"
Why the code doesn't work for my app, but it works ok in your form?
Please share your idea.
Thanks a bunch...
Learning .NET
|
|
|
|
|
mythinky wrote:
Why the code doesn't work for my app, but it works ok in your form?
Because you can't just copy and paste my code into yours and expect it to work. The example is just that, an example. Your have to study it to see why it works, then go back over your code and make sure that your doing the same thing. You can adapt my code to yours, but you can't just copy and paste it.
Without seeing exactly what you've done, there is no way I can tell you why mine works and yours doesn't.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I have tried your way you doing. Of course i don't copy and paste, cause it won't suitable for my app. The problem is binding to an empty path although i have catched the exception...
Thanks...
Learning .NET
|
|
|
|
|
You might want to double check your code in BindFields() . This is where the InvalidCastException is going to be thrown by the Bindings.Add() method and caught by your Try/Catch. If this code is not correct, you'll have a problem binding to an empty string.
I'd need to see your current code to see what you missed. There is no reason that that exception wouldn't be caught.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
This is part of the frmBookDetails class.
<background color="yellow">
Imports System.Data
Imports System.Data.OleDb
Imports System.DBNull
Imports System.IO
Imports System.Drawing
Public Class frmBookDetails
Inherits System.Windows.Forms.Form
Dim objConLib As OleDbConnection = New OleDbConnection(sConnectDBLib)
Dim objDAdp As OleDbDataAdapter
Dim objDSet As DataSet
Dim objDView As DataView
Dim objCMngr As CurrencyManager
Dim objDataAdp As OleDbDataAdapter
Dim objDataSet As DataSet
Dim response As MsgBoxResult
Dim isValidPath, isNeedToClose As Boolean
Dim isValidBookTitle, isValidBookAuthor, isValidPublisher, isValidYearPublished As Boolean
Dim isValidEdition, isValidCourseBook As Boolean
Dim isBookIDChanged, isBookTitleChanged, isBookAuthorChanged, isPublisherChanged, isYearPublishedChanged As Boolean
Dim isEditionChanged, isCourseBookChanged, isCategoryChanged As Boolean
Windows Form Designer Generated Code
Private Sub FillDataSetAndView()
Try
objDView = Nothing
objDSet = New DataSet()
objConLib.Open()
objDAdp = New OleDbDataAdapter()
objDAdp.SelectCommand = New OleDbCommand("[GETBOOKDETAILS]", objConLib)
objDAdp.SelectCommand.CommandType = CommandType.StoredProcedure
objDAdp.Fill(objDSet, "BOOKDETAILS")
objDView = New DataView(objDSet.Tables("BOOKDETAILS"))
objCMngr = CType(Me.BindingContext(objDView), CurrencyManager)
objCMngr.Position = objCMngr.Count - 1
objDataAdp = New OleDbDataAdapter()
objDataAdp.SelectCommand = New OleDbCommand("SELECT * FROM BOOKCATEGORY", objConLib)
objDataAdp.SelectCommand.CommandType = CommandType.Text
objConLib.Close()
objDSet = Nothing
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "FillDataSetAndView")
End Try
End Sub
Private Sub ClearBindFields()
txtBookID.DataBindings.Clear()
txtBookTitle.DataBindings.Clear()
txtBookAuthor.DataBindings.Clear()
txtCategory.DataBindings.Clear()
txtPublisher.DataBindings.Clear()
txtYearPublished.DataBindings.Clear()
txtEdition.DataBindings.Clear()
txtCourseBook.DataBindings.Clear()
txtOriginalOwned.DataBindings.Clear()
txtCopiesOwned.DataBindings.Clear()
txtCopiesLoan.DataBindings.Clear()
txtCopiesShelves.DataBindings.Clear()
txtCopiesSold.DataBindings.Clear()
picBook.DataBindings.Clear()
End Sub
Private Sub BindFields()
Try
txtBookID.DataBindings.Add("Text", objDView, "BookID")
txtBookTitle.DataBindings.Add("Text", objDView, "Title")
txtBookAuthor.DataBindings.Add("Text", objDView, "Author")
txtCategory.DataBindings.Add("Text", objDView, "Category")
'cmbCategory.DataBindings.Add("Text", objDView, "Category")
txtPublisher.DataBindings.Add("Text", objDView, "Publisher")
txtYearPublished.DataBindings.Add("Text", objDView, "YearLastPub")
txtEdition.DataBindings.Add("Text", objDView, "Edition")
txtCourseBook.DataBindings.Add("Text", objDView, "CourseBook")
txtOriginalOwned.DataBindings.Add("Text", objDView, "NoOriginalOwned")
txtCopiesOwned.DataBindings.Add("Text", objDView, "NoCopyOwned")
txtCopiesLoan.DataBindings.Add("Text", objDView, "NoCopyonLoan")
txtCopiesShelves.DataBindings.Add("Text", objDView, "NoCopyInShelves")
txtCopiesSold.DataBindings.Add("Text", objDView, "NoCopySoldorLost")
'Dim b As Binding = New Binding("Image", objDView, "CoverSample")
'AddHandler b.Format, AddressOf MyPictureBox_FormatImage
'picBook.DataBindings.Add(b)
Catch ex As InvalidCastException
MsgBox(ex.Message, , "BindFields")
End Try
End Sub
Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
' Check to see if this is a valid conversion.
If Not e.DesiredType Is GetType(Image) Then
MsgBox("FormatImage was called with the DesiredType of " & _
e.DesiredType.ToString, , "MyPictureBox_FormatImage")
Exit Sub
End If
' Check if the file exists (assumes "Imports System.IO").
' Uses the Static version of the Exists method.
Try
If Not File.Exists(e.Value.ToString) Then
MsgBox("File not found: " & e.Value.ToString, , "MyPictureBox_FormatImage")
Exit Sub
Else
' The file exists, try to create an Image object out of it.
e.Value = Image.FromFile(e.Value.ToString)
End If
Catch
End Try
Catch ex As Exception
MsgBox(ex.Message, , "MyPictureBox_FormatImage")
End Try
End Sub
Learning .NET
|
|
|
|
|
mythinky wrote:
'Dim b As Binding = New Binding("Image", objDView, "CoverSample")
'AddHandler b.Format, AddressOf MyPictureBox_FormatImage
'picBook.DataBindings.Add(b)
In your code, it looks like you're trying to Bind the picturebox Image property to a column called "CoverSample" in a DataView object. I hope this CoverSample column returns the complete path to an image file?
You can check this by setting a Breakpoint on the following line (highlighted):
Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
' Check to see if this is a valid conversion.
If Not e.DesiredType Is GetType(Image) Then
MsgBox("FormatImage was called with the DesiredType of " & _
Then, when the app reaches this point, you can look at the values in 'e' to see what you're getting. As far as I can tell, this code will work. You'll still get the exception if you try to Bind to an empty filename, but your code is catching that and putting up a MsgBox. Correct? If you don't want to see the MessageBox's, remove them, or better yet, comment them out.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Yeah, I am binding to a column called "CoverSample" in a DataView object.
Let's say the CoverSample contains an empty path, then the 'e' value will be "" (means empty).
There are no error occurs, but when it reach the
"picBook.DataBindings.Add(b)", error will occurs, and the error won't be caught even if i have try to catch the error. How?
The following code should have no comments, the previous code i post, the code was commented out.
Dim b As Binding = New Binding("Image", objDView, "CoverSample")
AddHandler b.Format, AddressOf MyPictureBox_FormatImage
picBook.DataBindings.Add(b)
Learning .NET
|
|
|
|
|
What's the exact error? I can't reproduce the problem. Copy and paste it into here...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
This is the error:
An unhandled exception of type 'System.InvalidCastException' occured in mscorlib.dll
Addtional information: Inavalid cast from System.String to String.Drawing.Image
This error occur on the following code:
Dim oFrmBookDetails = New frmBookDetails()
oFrmBookDetails.MdiParent = Me
oFrmBookDetails.Show() 'This is where the error occur. 'I know it from highlighted code when the error occurs.
Try
Dim b As Binding = New Binding("Image", objDView, "CoverSample")
AddHandler b.Format, AddressOf MyPictureBox_FormatImage
picBook.DataBindings.Add(b)
Catch ex As InvalidCastException
MsgBox(ex.Message, , "BindFields")
End Try
Every time i set the breakpoint, if i press F8, it will step into "Dim b As Binding...", then "AddHandler..." without going to "Adrressof MyPictureBox_FormatImage", after that "picBook.DataBindings.Add(b)", then step into "oFrmBookDetails.Show()". When showing, it execute the "MyPictureBox_FormatImage" procedure.
I think it should be execute the "MyPictureBox_FormatImage" first, then step into "oFrmBookDetails.Show()". How come it step into "oFrmBookDetails.Show()" first, then going into "MyPictureBox_FormatImage" procedure.
Learning .NET
|
|
|
|
|
Now we know where the error is actually occuring. You might want to wrap the .Show() line inside it's own Try/Catch block. I don't know if your going to get the results you expect or not. Also, is it possible that something else on the form is throwing the InvalidCastException ?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Sorry, cause of the language barrier between us, i don't get ur mean. I already put the .Show() inside the Try/Catch but the code is in the other class (in Lib.class in my code). It's not the same class with the BookDetails.class. Do u mean i need to put the .Show() inside the BookDetails.class?
I already try to catch the InvalidCastException when try to binding the picture, u mean i need to catch the InvalidCastException of the other control? is it ur mean?
I only get one catch the InvalidCastException in my BookDetails.class.
Sorry for my language ability. Thanks
Learning .NET
|
|
|
|
|
Every time i set the breakpoint, if i press F8, it will step into "Dim b As Binding...", then "AddHandler..." without going to "Adrressof MyPictureBox_FormatImage", after that "picBook.DataBindings.Add(b)", then step into "oFrmBookDetails.Show()". When showing, it execute the "MyPictureBox_FormatImage" procedure.
I think it should be execute the "MyPictureBox_FormatImage" first, then step into "oFrmBookDetails.Show()". How come it step into "oFrmBookDetails.Show()" first, then going into "MyPictureBox_FormatImage" procedure.
I mean, put a Try/Catch block around the .Show() you have here:
... then step into "oFrmBookDetails.Show()".
The binding format will not get executed until the .Show() method of the form that is hosting the control is executed. This is when your error is occuring.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I have put it,it work okay, two message box will displayed as your form1 application. But other problem raise, after formating the binding picture, the form doesn't load correctly. After &qt oFrmBookDetails.Show() &qt executed, it doesn't call frmBookDetails_load. But if i am not formating the binding picture, it will execute the frmBookDetails_load after it execute the &qt oFrmBookDetails.Show() &qt. Is there any other code I need to put to the formBookDetail?
Thanks. Please help me!!!
Learning .NET
|
|
|
|
|
Helo, I have a problem when sending email from VB.Net application, the error message is
"The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for destinationemail@address.com". What is actually is the problem? How can i get out from this problem? Anyone have any idea, please reply. Thanks in advance....
------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
try
Dim objEmailMessage As System.Web.Mail.MailMessage
Dim objSMTPServer As System.Web.Mail.SmtpMail
objSMTPServer.SmtpServer = "mycomputername"
objEmailMessage = New System.Web.Mail.MailMessage
With objEmailMessage
.To = "destinationemail@address.com"
.From = txtFrom.Text
.Subject = "Great Job!"
.Body = "Want all my money?"
End With
objSMTPServer.Send(objEmailMessage)
catch exc as exception
response.write(exc.innerexception.innerexception.message)
end try
End Sub
Learning .NET
|
|
|
|
|
The problem is that it looks like your using your local computer's SMTP server to send this. Most ISP's won't let you do this and will not relay mail for you. Instead, try using your ISP's SMTP server address in the objSMTPServer.SmtpServer line:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim objEmailMessage As System.Web.Mail.MailMessage
Dim objSMTPServer As System.Web.Mail.SmtpMail
objSMTPServer.SmtpServer = "smtp.myISP.com"
objEmailMessage = New System.Web.Mail.MailMessage
With objEmailMessage
.To = "destinationemail@address.com"
.From = txtFrom.Text
.Subject = "Great Job!"
.Body = "Want all my money?"
End With
objSMTPServer.Send(objEmailMessage)
Catch exc as exception
Response.Write(exc.InnerException.InnerException.Message)
End Try
End Sub
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks a lot to all ur guys' reply.
I have successfully sending email. I am using my ISP's SMTP server. I am happy with it.
Thanks
Learning .NET
|
|
|
|
|
You already posted the same question in the General Discussion Forums! Don't cross post.
Aaron Eldreth
TheCollective4.com
My Articles
While much is too strange to be believed,
Nothing is too strange to have happened.
- T. Hardy
|
|
|
|
|
How to add an icon to right click menu when selecting a file or folder.
Installer is going to do this kind of stuff or I can just set it up in
registry? I already defined Directory/Shell/MyApp/Command to add context menu
but i don't know how to add icon.
Shin
|
|
|
|
|
Hi
I have problem the same as you. But I want to know which items You put these keys(HKEY_ CLASSES_ROOT or HKEY_LOCAL_MACHINE)???
Thanks
tramdtt
|
|
|
|
|
|
I have tried but its only correct with several file type!!!
tramdtt
|
|
|
|
|