Can't Copy to Clipboard

G

Guest

I have the following VB.net Class/Function that I call from an ASP.net page:

Public Class MyTestClass
Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName
As String) As String

'Create a New Instance of the HyperLink Control
Dim createHyperLink As New System.Web.UI.WebControls.HyperLink

'Create an Instance of the ClipBoard Object
Dim objClipboard As System.Windows.Forms.Clipboard

Dim sBuildHyperLink As String

Try
'Set the Properties of the HyperLink Control
createHyperLink.ID = "EmbeddedHyperLink"
createHyperLink.NavigateUrl =
"http://www.someDomain.com/docPage.aspx?x=" & fileID
createHyperLink.Text = fileName

'Build HTML HyperLink String
sBuildHyperLink = "<a href=" & createHyperLink.NavigateUrl & ">"
& createHyperLink.Text & "</a>"

'Write HyperLink to Clip Board
objClipboard.SetDataObject(sBuildHyperLink, True)

Return sBuildHyperLink

Catch ex As Exception
Throw
Finally
createHyperLink = Nothing
objClipboard = Nothing
sBuildHyperLink = Nothing
End Try


End Function
End Class

When I get to the line "objClipboard.SetDataObject(sBuildHyperLink, True)",
that writes to the Windows Clipboard I get the following error and I don’t
know why:

Server Error in '/MyTestWeb' Application.
________________________________________
The current thread must set to Single Thread Apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Threading.ThreadStateException: The current thread
must set to Single Thread Apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.

Can someone tell me what I’m doing wrong?

Thanks
 
H

Hans Kesting

I have the following VB.net Class/Function that I call from an ASP.net page:
Public Class MyTestClass
Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName
As String) As String

'Create a New Instance of the HyperLink Control
Dim createHyperLink As New System.Web.UI.WebControls.HyperLink

'Create an Instance of the ClipBoard Object
Dim objClipboard As System.Windows.Forms.Clipboard

Dim sBuildHyperLink As String

Try
'Set the Properties of the HyperLink Control
createHyperLink.ID = "EmbeddedHyperLink"
createHyperLink.NavigateUrl =
"http://www.someDomain.com/docPage.aspx?x=" & fileID
createHyperLink.Text = fileName

'Build HTML HyperLink String
sBuildHyperLink = "<a href=" & createHyperLink.NavigateUrl & ">"
& createHyperLink.Text & "</a>"

'Write HyperLink to Clip Board
objClipboard.SetDataObject(sBuildHyperLink, True)

Return sBuildHyperLink

Catch ex As Exception
Throw
Finally
createHyperLink = Nothing
objClipboard = Nothing
sBuildHyperLink = Nothing
End Try


End Function
End Class

When I get to the line "objClipboard.SetDataObject(sBuildHyperLink, True)",
that writes to the Windows Clipboard I get the following error and I don’t
know why:

Server Error in '/MyTestWeb' Application.
________________________________________
The current thread must set to Single Thread Apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Threading.ThreadStateException: The current thread
must set to Single Thread Apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.

Can someone tell me what I’m doing wrong?

Thanks

What clipboard do you expect to copy to?
This looks like a portion of a code-behind file, which is executed on
the server. At the most you can write to the clipboard of the *server*,
not that of the *client*! And even that is problematic: only the
"aspnet" user would be able to access that particular clipboard item (I
think).

Hans Kesting
 
G

Guest

Good point Hans. I do want it to copy to the Clipboard where the client is
and not the Server. Here is the code that calls my function now, but I still
get the same error:

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

'Create an Instance of the ClipBoard Object
Dim objClipBoard As System.Windows.Forms.Clipboard

'Write HyperLink to Clip Board

objClipBoard.SetDataObject((test.CreateHyperlink(CInt(TextBox1.Text),
TextBox2.Text)), True)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top