problem in using clipboard class in webapplication


I was reading an article on clipboard.

But when I am using this code on the web application I am getting runtime error.

"object reference is not set"

I am attaching the code where I am getting this error.

Can't we use it in web application

Open in new window

Who is Participating?
oobaylyConnect With a Mentor Commented:
I'm guessing that you've included a reference to System.Windows.Forms so that you are able to use the Clipboard object.

You might try calling the Clipboard methods in a Single Thread Aparment (STA).

However, you may find some nasty side effects using the clipboard in a Web App. If two users call the same method at the same time, one my place data in the clipboard, and the other retrieves that data.
Private Sub ee24297524()
    Dim td As New System.Threading.Thread(AddressOf GetClipboardText)
    td.ApartmentState = System.Threading.ApartmentState.STA
End Sub
Private Sub GetClipboardText()
    Dim b As Boolean = System.Windows.Forms.Clipboard.ContainsText()
End Sub

Open in new window

Why would you want to do this in a web application? This code and post is useful only in the context of a winforms application. If you need to access the client clipboard data then you will have to do it using javascript.
Thanks for the points, I gave an answer that would work, but I do agree with sunithnair.
I'd recommend using the Session variable, the cache or a temp file instead of the clipboard.
Where to call ee24297524() given in above comment. I getting error -
"the Current thread must be set to single thread apartment (STA) mode before
OLE calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to the
Is there is any solution?
Krishna Mohan Singh
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.