471,579 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 software developers and data experts.

Can't Copy to Clipboard

SAL
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

Feb 8 '06 #1
3 3604
> 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
Feb 8 '06 #2
SAL
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(C Int(TextBox1.Text),
TextBox2.Text)), True)

End Sub

"Hans Kesting" wrote:
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

Feb 8 '06 #3
"SAL" <SA*@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
Can someone tell me what I'm doing wrong?


You're trying to get your server to write to your clients' clipboard - not
going to happen.

You can manipulate the clipboard in ASP.NET, but only through JavaScript,
and the text you're trying to copy to your clients' clipboard will need to
be sent down to the client as part of the JavaScript code...

http://www.google.com/search?sourcei...clipboard+copy
Feb 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jarrad | last post: by
3 posts views Thread by thaar al_taiey | last post: by
reply views Thread by chawes40 | last post: by
reply views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.