简体   繁体   中英

Clipboard.SetText() shows errors

I have been using a simple web form application for url shortening in asp.net. I have sucessfully made my function and when i change the url it should automatically copies to clipboard.

I have made the text value to store in variable like

   shortUrl.Text = shortURL; 

After this i am calling the Clipboard function

 clip_board(shortURL);

My function is

 public void clip_board(string shortUrl)
    {
       MessageBox.Show(shortUrl);
       Clipboard.SetText(shortUrl);

    }

Here the message box shows the shorten url but the Clipboard.SetText() showing error as

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.

How to overcome this error?

You say it's an ASP.NET application: in this case your .NET code will be running on the server, so you can't access the client's clipboard - nor can you call WinForms methods such as MessageBox.Show .

In response to comment:

But it shows the message box with exact values ...

Are you debugging using Visual Studio and the Cassini web server? In which case you might see the output of MessageBox.Show as it's running in the context of your current session. But this won't work when you deploy to IIS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM